I found a processing sketch and an arduino sketch that turned off and on an LED. I am trying to expand the sketches so that I can turn off and on multiple LEDs.
In the following processing code I tried to change the port.write('L'); port.write('H'); to port.write('H1'); port.write('H2'); and so on but the Arduino sketch did not work. I then changed the port.write('L'); port.write('H'); to port.write("L1"); port.write("H1"); but I could not figure out how to change the Arduino sketch.
So I guess I'm asking is how can I change the Processing and Arduino Sketches to get the Processing to send out L1 or H1 to turn LED1 on or off,
L2 or H2 to turn LED2 on or OFF.
//Turn LED on and off if buttons pressed where
//H = on (high) and L = off (low)
if(mousePressed) {
if(rect1.pressed()) { //OFF button
currentcolor = rect1.basecolor;
port.write('L');
} else if(rect2.pressed()) { //ON button
currentcolor = rect2.basecolor;
port.write('H');
}
}
if(mousePressed) {
if(rect3.pressed()) { //OFF button
currentcolor = rect3.basecolor;
port.write('L');
} else if(rect4.pressed()) { //ON button
currentcolor = rect4.basecolor;
port.write('H');
}
}
if(mousePressed) {
if(rect5.pressed()) { //OFF button
currentcolor = rect5.basecolor;
port.write('L');
} else if(rect6.pressed()) { //ON button
currentcolor = rect6.basecolor;
port.write('H');
}
}
if(mousePressed) {
if(rect7.pressed()) { //OFF button
currentcolor = rect7.basecolor;
port.write('L');
} else if(rect8.pressed()) { //ON button
currentcolor = rect8.basecolor;
port.write('H');
}
}
This is the Arduino sketch
char val; // variable to receive data from the serial port
int ledpin1 = 10; // LED connected to pin 10 (on-board LED)
int ledpin2 = 6; // LED connected to pin 6 (on-board LED)
int ledpin3 = 7; // LED connected to pin 7 (on-board LED)
int ledpin4 = 8; // LED connected to pin 8 (on-board LED)
void setup() {
pinMode(ledpin1, OUTPUT); // pin 10 (on-board LED) as OUTPUT
pinMode(ledpin2, OUTPUT); // pin 6 (on-board LED) as OUTPUT
pinMode(ledpin3, OUTPUT); // pin 7 (on-board LED) as OUTPUT
pinMode(ledpin4, OUTPUT); // pin 8 (on-board LED) as OUTPUT
Serial.begin(115200); // start serial communication at 115200bps
}
void loop() {
if( Serial.available() ) // if data is available to read
{
val = Serial.read(); // read it and store it in 'val'
}
if( val == 'H' ) // if 'H' was received
{
digitalWrite(ledpin1, HIGH); // turn ON the LED
}
if( val == 'L' ) // if 'H' was received
{
digitalWrite(ledpin1, LOW); // turn OFF the LED
}
if( val == 'H' ) // if 'H' was received
{
digitalWrite(ledpin2, HIGH); // turn ON the LED
}
if( val == 'L' ) // if 'H' was received
{
digitalWrite(ledpin2, LOW); // turn OFF the LED
}
if( val == 'H' ) // if 'H' was received
{
digitalWrite(ledpin3, HIGH); // turn ON the LED
}
if( val == 'L' ) // if 'H' was received
{
digitalWrite(ledpin3, LOW); // turn OFF the LED
}
if( val == 'H' ) // if 'H' was received
{
digitalWrite(ledpin4, HIGH); // turn ON the LED
}
if( val == 'L' ) // if 'H' was received
{
digitalWrite(ledpin4, LOW); // turn OFF the LED
}
delay(100); // wait 100ms for next reading
}