xxxxxxxxxx
108
// variables for live photo capture
let videoLive;
let buttonSnap; // this should go to arduino
//let photoSensorValue = 0; // value from photosensor
//let xPosition = 0; // X-axis position of the ellipse
//let xMinimum = 25; // min limit of ellipse on X-axis
//let xMaximum = 550; // max limit of ellipse on X-axis
function setup() {
createCanvas(600, 400);
textSize(20);
// live photo capture
createCanvas(320, 240);
background(51);
videoLive = createCapture(VIDEO); //access live webcam
videoLive.size(320, 240); //change the size to 320 x 240
buttonSnap = createButton('snap'); //create a button called "snap"
buttonSnap.mousePressed(takesnap); //when the button is pressed, call the function called "takesnap"
}
function takesnap() {
image(videoLive, 0, 0); //draw the image being captured on webcam onto the canvas at the position (0, 0) of the canvas
}
function draw() {
background(230, 230, 250);
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
// map values from photo sensor
// onto the X-axis position of the ellipse
//xPosition = map(photoSensorValue, 0, 1023, xMinimum, xMaximum);
// draw an ellipse
//ellipse(xPosition, 240, 50, 50);
// print the current values
//text("xPosition = " + str(xPosition), 20, 50);
}
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
function readSerial(data) {
if (data != null) {
// make sure there is actually a message
// split the message
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 1) {
// only store values here
// do everything with those values in the main draw loop
photoSensorValue = fromArduino[0];
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = 1 + "\n";
writeSerial(sendToArduino);
}
}
//Arduino Code
/*
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
// start the handshake
while (Serial.available() <= 0) {
digitalWrite(LED_BUILTIN, HIGH); // on/blink while waiting for serial data
Serial.println("0,0"); // send a starting message
delay(300); // wait 1/3 second
digitalWrite(LED_BUILTIN, LOW);
delay(50);
}
}
void loop() {
// wait for data from p5 before doing something
while (Serial.available()) {
digitalWrite(LED_BUILTIN, HIGH); // led on while receiving data
int isMoving = Serial.parseInt();
if (Serial.read() == '\n') {
int photoSensorValue = analogRead(A0);
delay(5);
Serial.println(photoSensorValue);
}
}
digitalWrite(LED_BUILTIN, LOW);
}
*/