xxxxxxxxxx
160
let serial;// variable to hold an instance of the serialport library
let portName = '/dev/cu.usbmodem101';// fill in your serial port name here
let outByte = 0;// for.usbming data
let handpose;
let video;
let hands = [];
function preload() {
handpose = ml5.handpose();
}
function setup() {
serial = new p5.SerialPort(); // make a new instance of the serialport library
serial.on('error', serialError); // callback for errors
serial.open(portName); // open a serial port
createCanvas(640, 480);
video = createCapture(VIDEO);
video.size(width, height);
video.hide();
handpose.detectStart(video, gotHands);
}
function gotHands(results) {
hands = results;
// console.log(hands);
}
function draw() {
image(video, 0, 0, width, height);
drawShapes();
fingerCheck();
}
function drawShapes() {
fill('red');
rect(0, 0, width/4, height/4);
fill('yellow');
rect(width/4, 0, width/4, height/4);
fill('green');
rect(width/2, 0, width/4, height/4);
fill('blue');
rect(3*width/4, 0, width/4, height/4);
}
function fingerCheck() {
if(hands.length > 0) {
let index = hands[0].index_finger_tip;
fill('black');
circle(index.x, index.y, 20);
if(index.y <= height/4) {
if(index.x >= 0 && index.x < width/4) {
outByte = 1;
}
else if(index.x >= width/4 && index.x < width/2) {
outByte = 2;
}
else if(index.x >= width/2 && index.x < 3*width/4) {
outByte = 3;
}
else if(index.x >= 3*width/4 && index.x < width) {
outByte = 4;
}
}
else {
outByte = 0;
}
console.log('outByte: ', outByte)
serial.write(outByte);
}
}
function serialError(err) {
console.log('Something went wrong with the serial port. ' + err);
}
//Sets up the serial connection
function keyPressed() {
if (key == " ") {
setUpSerial();
}
}
function readSerial(data) {
if(data != null) {
//Sends the light ON/OFF messages for all the lights to the arduino
let sendToArduino = outByte;
writeSerial(sendToArduino);
}
}
/*
Arduino Code:
int redPin = 2;
int yellowPin = 4;
int greenPin = 6;
int bluePin = 8;
void setup() {
pinMode(redPin, OUTPUT); // sets the pin as output
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600); // initialize serial communications
}
void loop() {
if (Serial.available() > 0) { // if there's serial data available
int inByte = Serial.read(); // read it
if (inByte == 1) {
digitalWrite(redPin, HIGH);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
else if (inByte == 2) {
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
else if (inByte == 3) {
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
}
else if (inByte == 4) {
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
}
else {
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
}
}
*/