Use 2 leds and 2 buttons using arduino and processing

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);
    }
  } 
}
1 Like

The calls to analogWrite() are not necessary for switching on or off only. They will also override your commands since they are called inside the loop() function.

You may find it easier to send bytes [or characters if using Serial.print()] from Processing instead of writing an integer.
An ‘int’ is made of four bytes and can break the sequence on the Arduino side.

Personally, I would use (and have used) characters for sending commands this way.

As an example, for an ON command you could use

Serial.print('A');

and for OFF

Serial.print('a');

This would give control over 26 pins or options without much effort.

Arduino side:

if (Serial.available() >= 1){
  rx = Serial.read();

  // If you have many options then a case structure
  //   would be better here
  if ('A' == rx){
    digitalWrite(LED1, HIGH);
  }else if ('a' == rx){
    digitalWrite(LED1, LOW);
  }
  ...
1 Like

@animebrowny Please format your code. Edit your post, select your code and hit the </> button in the toolbar of the post box.

Great you check that both buttons are working with the first led. Can you check the second led? Does it work the same? Or is it toasted? I burn LEDs all the time, so it is a good habit to manually test LEDs to ensure they work.

Please check these two codes as you could find them helpful:

https://forum.processing.org/two/discussion/14004/sending-a-numeri
https://forum.processing.org/two/discussion/14006/example-code-controlling-electric-motors-with-a-controlp5-knob-and-adafruit-motor-shield-boards#latestcal-variable-from-processing-to-arduino

is this an exercise or do you have a specific purpose? As I understand, you only want one and only one LED on at a time?

Kf

1 Like