2 LED toggle with 3 buttons

Hi everyone,
Here is my small project description: Use 3 buttons to toggle 2 LED's (Green and Yellow) in the following sequence: (at start no LED is lit) press button 1 to turn on the Green LED, press button 2 to turn on the Yellow LED and turn off the Green LED, when the button 3 is pressed will turn off all the LED's regardless witch one is lit.
Parts used: a controller (Trinket 5v from ADA) 2 LED's (each with 330 Ohm resistor) and 3 buttons (each with 1kOhm resistor on negative)

Since I am not a programmer at all but playing with Arduino here is only the pseudo code I have from various sources on internet:

const int LED1=0;
const int LED2=1;
const int Buttton1=2;
const int Buttton2=3;
const int Buttton3=4;
boolean state = true; // declare variable state as boolean
int val=0;

void setup()
{
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(Button1,INPUT);
  pinMode(Button2,INPUT);
  pinMode(Button3,INPUT);
 }
void loop()
{
  val=digitalRead(Button1);
  delay(100); // Software debouncing

if(val==HIGH)

{state=!state; // Complimenting the status of LED
  digitalWrite(LED1,state);
  digitalWrite(LED2,!state);
}

}

Any help on the code will be appreciated, thanks!
Catalin Anesia

I don't see any reading of the 2nd & 3rd buttons.
You have the basics for 1 button down. Do a little thinking & add the 2nd & 3rd.

Thanks,
I still do something wrong for sure since I is not everything clear in my head but this is what I put together further:

const int LED1=0;
const int LED2=1;

const int Buttton1=2;
const int Buttton2=3;
const int Buttton3=4;

boolean state1 = true; // declare variable state1 for button1 as boolean
boolean state2 = true; // declare variable state2 for button2 as boolean
boolean state2 = true; // declare variable state3 for button3 as boolean

int val1=0; // button1
int val2=0; // button2
int val3=0; // button3

void setup()
{
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(Button1,INPUT);
  pinMode(Button2,INPUT);
  pinMode(Button3,INPUT);
 }

void loop()

{
  val1=digitalRead(Button1);
  delay(100); // Software debouncing

if(val1==HIGH)

{state1=!state1; // Complimenting the status of LED
  digitalWrite(LED1,state1);
  digitalWrite(LED2,!state1);

{
  val2=digitalRead(Button2);
  delay(100); // Software debouncing

if(val2==HIGH)

{state2=!state2; // Complimenting the status of LED
  digitalWrite(LED1,state1);
  digitalWrite(LED2,!state2);
}

{
  val3=digitalRead(Button3);
  delay(100); // Software debouncing

if(val3==HIGH)

{state3=!state3; // Complimenting the status of LED
  digitalWrite(LED1,state3);
  digitalWrite(LED2,!state3);
}
}
}
}

Looking good.
You have some duplication here:

boolean state1 = true; // declare variable state1 for button1 as boolean
boolean state2 = true; // declare variable state2 for button2 as boolean
boolean state2 = true; // declare variable state3 for button3 as Boolean

Need a state3.

So something like

const byte Led1=0;
const byte Led2=1;
const byte Buttton1=2;
const byte Buttton2=3;
const byte Buttton3=4;

byte state = 0; // state of leds, 0 = off, 1 = Led1, 2 = Led2


void setup()
{
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(Button1,INPUT);
  pinMode(Button2,INPUT);
  pinMode(Button3,INPUT);
}

void loop()
{
  //read the buttons
  if(digitalRead(Button1)){
    state = 1;
  }
  else if(digitalRead(Button2)){
    state = 2;
  }
  else if(digitalRead(Button3)){
    state = 0;
  }
  
  //set the leds
  digitalWrite(Led1, state == 1);
  digitalWrite(Led2, state == 2);
}

I don't know about the Trinket but pin0 and pin1 are used for serial on a Arduino Uno/Nano/Mega etc

You can leave out the pull down resistors if you want. Just place the switch between the pin and GND (instead of the pin and 5V like you have now) and enable the internal pullups. (Think the Trinket has them as well.) Only thing is, the button now reads high when not pressed and low when pressed, so inverted. But that's an easy fix

const byte Led1=0;
const byte Led2=1;
const byte Buttton1=2;
const byte Buttton2=3;
const byte Buttton3=4;

byte state = 0; // state of leds, 0 = off, 1 = Led1, 2 = Led2


void setup()
{
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(Button1,INPUT_PULLUP);
  pinMode(Button2,INPUT_PULLUP);
  pinMode(Button3,INPUT_PULLUP);
}

void loop()
{
  //read the buttons
  if(!digitalRead(Button1)){
    state = 1;
  }
  else if(!digitalRead(Button2)){
    state = 2;
  }
  else if(!digitalRead(Button3)){
    state = 0;
  }
  
  //set the leds
  digitalWrite(Led1, state == 1);
  digitalWrite(Led2, state == 2);
}

And if I where you I would read up on arrays :wink: See Gammon Forum : Electronics : Microprocessors : Arduino programming traps, tips and style guide

Thank you both!
I do the reading regarding the arrays right now since I do not have the hardware with me at this time to actually do the code run and see the marvell ...

Ill keep you posted later on the night ...

Thanks!

PS. do I need to rewire and remove my resistors to make your code work? or I can keep the wiring as is?

Catalin Anesia

The first code you can use as is. So with external pull down resistors.

The second code is when you rewired it to get rid of the resistors. You can see that because I enable the internal pull ups

  pinMode(Button1,INPUT_PULLUP);
  pinMode(Button2,INPUT_PULLUP);
  pinMode(Button3,INPUT_PULLUP);

And i inverted the button reading by placing a ! in front

if(!digitalRead(Button1)){

catalinanesia:
Thank you both!
I do the reading regarding the arrays right now since I do not have the hardware with me at this time to actually do the code run and see the marvell …

No need to let that hold you up any!!

https://123d.circuits.io

Hi again,
I am simulating on 123D Circuits but unfortunately I do have the following errors:

In function 'void setup()':
13:11: error: 'LED1' was not declared in this scope
14:11: error: 'LED2' was not declared in this scope
15:11: error: 'Button1' was not declared in this scope
16:11: error: 'Button2' was not declared in this scope
17:11: error: 'Button3' was not declared in this scope
 In function 'void loop()':
23:19: error: 'Button1' was not declared in this scope
26:24: error: 'Button2' was not declared in this scope
29:24: error: 'Button3' was not declared in this scope

And here is the code set up on Arduino Uno R3 in 123D circuits:

const byte Led1=2;
const byte Led2=3;
const byte Buttton1=4;
const byte Buttton2=5;
const byte Buttton3=6;

byte state = 0; // state of leds, 0 = off, 1 = Led1, 2 = Led2


void setup()
{
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(Button1,INPUT_PULLUP);
  pinMode(Button2,INPUT_PULLUP);
  pinMode(Button3,INPUT_PULLUP);
}

void loop()
{
  //read the buttons
  if(!digitalRead(Button1)){
    state = 1;
  }
  else if(!digitalRead(Button2)){
    state = 2;
  }
  else if(!digitalRead(Button3)){
    state = 0;
  }
  
  //set the leds
  digitalWrite(Led1, state == 1);
  digitalWrite(Led2, state == 2);
}

The debugger makes this lines red:

pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(Button1,INPUT_PULLUP);
pinMode(Button2,INPUT_PULLUP);
pinMode(Button3,INPUT_PULLUP);

if(!digitalRead(Button1)){
else if(!digitalRead(Button2)){
else if(!digitalRead(Button3)){

Any ideas why? looks like the pins are setted properly in code ...

Uau I am blind :frowning:
There has been some typo ... it works finally!

Thanks everyone :slight_smile: