Skip to content

Instantly share code, notes, and snippets.

Created May 12, 2015 16:33
Show Gist options
  • Save anonymous/50d2c9665ed41994e502 to your computer and use it in GitHub Desktop.
Save anonymous/50d2c9665ed41994e502 to your computer and use it in GitHub Desktop.
Turning LED on / off virtually using processing
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
int sensorData; // Data received from the serial port with 1,2,3,4 framing numbers filtered out
int low = 0;
int med = 0;
int high = 0;
float value1 =0;
float value2 =0;
int byte1 = 0;
int byte2 = 0;
int byte3 = 0;
int byte4 = 0;
int up_low = 0;
int up_high = 0;
int down_low = 0;
int down_high = 0;
float x = 640; // Circle parameters
float y = 400;
float radius = 100;
PImage img1;
PImage img2;
void setup()
{
size(1280, 760);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
myPort = new Serial(this, "/dev/tty.usbserial-A9024P2C", 9600);
img1 = loadImage("ledoff.jpg"); // Take images that were imported to the sketch before
img2 = loadImage("ledon.jpg");
}
void draw()
{
while (myPort.available() > 0) { // If data is available
byte1 = byte2;
byte2 = byte3;
byte3 = byte4;
byte4 = up_low;
up_low = up_high;
up_high = down_low;
down_low = down_high;
down_high = myPort.read();
if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)){ // Filter out the framing numbers: 1,2,3,4
float up_value = 256*up_high + up_low;
float down_value = 256*down_high + down_low;
value1 = (up_value + (1023 - down_value))/2.0;
println("VALUE1 IS " + value1); //print to the screen
}
if ( value1 >= 0 && value1 < 1700 ) { // If nothing touching step response sensor
image(img2, 0, 0); // show image with LED off
} else if ( value1 > 1700 ) { // If you touch step response sensor and increase values received,
image(img1, 0, 0); // show image with LED on
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment