2 buttons and 3 leds

Can you help me with my Arduino that controls the output of 3 LEDs which uses 2 buttons as an input. The output will be based on the following conditions:

  1. All LEDs are high when both buttons are closed(pressed).
  2. All LEDs are low when both buttons are open(unpressed).
  3. The LEDs will be high one at time from left to right when only the first button is closed(pressed).
  4. The LEDs will be high one at time from right to left when only the second button is closed(pressed).

Two questions:
When do we have to hand in our assignment?
What has this got to do with the bootloader?

Another question:
What code have you written so far ?

void setup() {
pinMode (9, INPUT); // button
pinMode (8, INPUT); //button
pinMode (3, OUTPUT); //led1
pinMode (2, OUTPUT); //led2
pinMode (1, OUTPUT); //led3
}

void loop{
int button1 = digitalRead (9);
int button2 = digitalRead (8);

Don't do that - it'll make debugging harder.

Unless your LEDs are taking street drugs, that doesn't make much sense. Outputting a HIGH value to the LED pin can turn it on or off, depending on how you connected it.

1 Like

Hello
Do you have any experience in programing and in the design of finite state machines?
If not, than start with to study the Input-Processing-Output model and take piece of paper and a pencil and design the desired program structure before coding.

that's definitely code so far - you did not get a chance to write the closing bracket I see :slight_smile:

I would suggest you start by the beginning - small steps.

  • handle LEDs (on, off)
  • handle buttons (detect press, release, handle bouncing)
  • read about state machines

You'll need to clarify what both buttons pressed means. From your arduino viewpoint, they will likely be depressed not at the same time but one after the other (possibly a few ms in between the two presses ➜ so you probably need some code to allow for that small lag)

void setup() {
pinMode (9, INPUT); // button
pinMode (8, INPUT); //button
pinMode (3, OUTPUT); //led1
pinMode (2, OUTPUT); //led2
pinMode (1, OUTPUT); //led3
}

void loop{
int button1 = digitalRead (8);
int button2 = digitalRead (9);

if(button1 && button2 == HIGH)  // 2 buttons are              pressed. 3 leds are HIGH (ON)
2 both buttons unpressed LOW(CLOSE)

{
     digitalWrite(3, HIGH);
     digitalWrite(2, HIGH);
     digitalWrite(1, HIGH);
}
else
     digitalWrite(3, LOW);
     digitalWrite(2, LOW);
     digitalWrite(1, LOW);

if(button1 == HIGH)  // button1 pressed only Led turns on Left to Right
{
     digitalWrite(3, HIGH);
     delay(300);
     digitalWrite(2, HIGH);
     delay(300);
     digitalWrite(1, HIGH);
}
if(button2 == HIGH)  // button2 pressed only Led turns on Right to Left
{
     digitalWrite(3, HIGH);
     delay(300);
     digitalWrite(2, HIGH);
     delay(300);
     digitalWrite(1, HIGH);
}

I used proteus to create the circuit and and just add the program file from arduino

if you call that a solution you are in for some disappointment if you hold both buttons down

And it works according to your specifications? Have you tested it?

The compiler doesn´t like your sketch.
What happened ?

I don't think this does what you think:

if(button1 && button2 == HIGH)

without braces this doesn't do what you think:

else
     digitalWrite(3, LOW);
     digitalWrite(2, LOW);
     digitalWrite(1, LOW);

and to be correct (although it works by chance because HIGH is 1), it should be

if ((button1 == HIGH) && (button2 == HIGH)) {
  ...
} else {
  ...
}

but there are so many other approximations...

Hi, @paaaper
Welcome to the forum.

What model Arduino are you using?
Have you programmed the controller to see if your code works?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.