Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
// the setup routine runs once when you press reset:
int threshold = 1; // threshold value is found experimentally
int leftsensor = 0;
int rightsensor = 0;
int thereport = 0;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int leftsensor = analogRead(A0);
// read the input on analog pin 2:
int rightsensor = analogRead(A2);
// test section - printing direct sensor readouts
//Serial.print(leftsensor);
//Serial.print("-");
//Serial.println(rightsensor);
thereport = 0; // zero out value to be reported to pde
if (rightsensor > threshold){
thereport = 2;
}
if (leftsensor > threshold){
thereport = thereport+1;
}
// and now send it over tty
Serial.println(thereport);
delay(500); // delay in between reads for stability
}
import processing.serial.*;
import processing.video.*;
Movie movie;
String[] inSensor = new String[1];
Serial myPort;
int boxes = 0; // this variable (reported by arduino over serial port)
// is a binary state switch
// 00 - both boxes closed
// 01 - left box open
// 10 - right box open
// 11 - both boxes open
boolean sketchFullScreen() {
return true;} // going full screen 720p
void setup() {
size(1280, 720);
// Initialize the Serial object and print serial ports
// println(Serial.list()); // this is for debug only - delete after
myPort = new Serial(this, "/dev/tty.usbmodem411", 9600);
background(0); //black video background
movie = new Movie(this, "swimming.mov");
movie.loop(); //movie should loop
}
void movieEvent(Movie m) {
m.read();
if (movie.available() == true) {
movie.read();
}
}
void draw() {
String inString = myPort.readStringUntil('\n');
// fetch and process sensor readout bitmap from Arduino
if (inString != null){
inString = trim(inString);
boxes = int(inString);
}
// this section is a test - using mouse X coordinate to simulate arduino comm
// to be replaced by actual arduino sensor data
//if (mouseX > width *.25) boxes = 1;
//if (mouseX > width *.5) boxes = 2;
//if (mouseX > width *.75) boxes = 3;
image(movie, 0, 0, width, height); //display movie playback
if (boxes == 0) { // both boxes closed, draw both blockers
fill(0);
rect(500, 0, 360, 720); // left black rectangle
rect(840, 0, 360, 720); // right black rectangle
}
if (boxes == 1) { // left box open
fill(0);
rect(840, 0, 360, 720); // right black rectangle
}
if (boxes == 2) { // right box open
fill(0);
rect(500, 0, 360, 720); // left black rectangle
}
}
void mainloop() { // we could just loop the draw call but this seems neater
while(true) {
draw();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.