Hi everyone,
I’m kind of stuck on something. I am trying to work 2 leds using arduino with 2 buttons in processing. The goal is to link 1 button to one led.
Now the problem. I have tried several things, but i cannot get the second led to work. The first led is working on both buttons in processing.
Can someone help me to figure out what i did wrong?
ARDUINO CODE:
int LED1 = 10;
int LED2 = 11;
int val1 = 0;
int val2 = 0;
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
Serial.begin(9600);
}
void loop() {
analogWrite(LED1, LOW);
analogWrite(LED2, LOW);
if (Serial.available() >1){
val1 = Serial.read();
val2 = Serial.read();
}
if (val1 >10 && val2 ==0){
digitalWrite(LED1, HIGH);
}else{
digitalWrite(LED1, LOW);
}
if (val2 >10 && val1 ==0){
digitalWrite(LED2, HIGH);
}else{
digitalWrite(LED2, LOW);
}
}
PROCESSING CODE:
import processing.serial.*;
Serial myPort;
void setup() {
size(640, 360);
background(100);
noStroke();
String portName = Serial.list() [1];
myPort = new Serial(this, portName, 9600);
}
int value1 = 0;
int value2 = 0;
void draw() {
if (myPort.available()>0){
}
fill(value1);
rect(150, 50, 50, 50);
fill(value2);
rect(50, 210, 50, 50);
println("VALUE1_"+value1);
println("VALUE2_"+value2);
}
void mousePressed() {
if (mouseX > 149 && mouseX < 200 && mouseY > 49 && mouseY < 101) {
if (value1 == 0){
value1 = 255;
myPort.write(value1);
}else{
value1 = 0;
myPort.write(value1);
}
}
if (mouseX > 49 && mouseX < 100 && mouseY > 209 && mouseY < 250) {
if (value2 == 0){
value2 = 255;
myPort.write(value2);
}else {
value2 = 0;
myPort.write(value2);
}
}
}