# Use 2 leds and 2 buttons using arduino and processing

**URL:** <https://discourse.processing.org/t/use-2-leds-and-2-buttons-using-arduino-and-processing/1020>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [June 16, 2018, 11:24pm UTC](https://discourse.processing.org/t/use-2-leds-and-2-buttons-using-arduino-and-processing/1020 "2018-06-16T23:24:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![animebrowny](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@animebrowny](https://discourse.processing.org/u/animebrowny)\
**Post date:** [June 16, 2018, 11:24pm UTC](https://discourse.processing.org/t/use-2-leds-and-2-buttons-using-arduino-and-processing/1020/1 "2018-06-16T23:24:14Z")

</div>

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:

```auto
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:

```auto
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);
    }
  } 
}

```

---

<div class="post-metadata">

**Author:** ![noahbuddy](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@noahbuddy](https://discourse.processing.org/u/noahbuddy)\
**Post date:** [June 17, 2018, 12:27am UTC](https://discourse.processing.org/t/use-2-leds-and-2-buttons-using-arduino-and-processing/1020/2 "2018-06-17T00:27:36Z")

</div>

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](https://processing.org/reference/char.html) 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);
  }
  ...
```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 19, 2018, 1:39am UTC](https://discourse.processing.org/t/use-2-leds-and-2-buttons-using-arduino-and-processing/1020/3 "2018-06-19T01:39:42Z")

</div>

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

> [@animebrowny](#):
>
> The first led is working on both buttons in processing

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/14004/sending-a-numeri)  
> [Example code: Controlling Electric Motors with a controlP5 knob and Adafruit Motor Shield boards - Processing 2.x and 3.x Forum](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
