Controlling multiple components with one button

Hi there,

I currently have this code -

int switchPin = 2; // switch is connected to pin 2 int led1Pin = 8; int led2pin = 9;

int val; // variable for reading the pin status int val2; // variable for reading the delayed status int buttonState; // variable to hold the button state int Mode = 0; // What mode is the light in? boolean modeChanged = false; const int NUM_MODES = 4;

void setup() { pinMode(switchPin, INPUT); // Set the switch pin as input pinMode(led1Pin, OUTPUT); pinMode(led2pin, OUTPUT); buttonState = digitalRead(switchPin); // read the initial state }

void loop(){ val = digitalRead(switchPin); // read input value and store it in val delay(10); // 10 milliseconds is a good amount of time val2 = digitalRead(switchPin); // read the input again to check for bounces if (val == val2) { // make sure we got 2 consistant readings! if (val != buttonState) { // the button state has changed! if (val == LOW) { // check if the button is pressed Mode++; if (Mode >= NUM_MODES) { Mode = 0; } modeChanged = true; } } buttonState = val; // save the new state in our variable }

if (modeChanged) { modeChanged = false;

// Now do whatever the lightMode indicates switch(Mode) { case 0: digitalWrite(led1Pin, LOW); digitalWrite(led2pin, LOW); break;

case 1: digitalWrite(led1Pin, HIGH); digitalWrite(led2pin, LOW); break;

case 2: digitalWrite(led1Pin, LOW); digitalWrite(led2pin, HIGH);

case 3: digitalWrite(led1Pin, HIGH); digitalWrite(led2pin, HIGH); } } }

Which is supposed to set off a series of LEDs in a sequence and when the button is pressed and then off when pressed again, however, this seems to only turn on one LED without the button being used and I'm unsure why - any help would be awesome ,thanks

Which is supposed to set off a series of LEDs in a sequence

You might have some idea what this means, but I sure don't. An LED (pin) is either off or on. There is nothing sequential about that.

when the button is pressed and then off when pressed again

That looks like two states - pressed once and pressed twice. Your code has 4 states. Please explain the discrepancy.

What do your Serial.print() statements tell you is happening? Why not?

Are your LEDs wired to be active HIGH or active LOW? Are you using current limiting resistors? Please post a schematic diagram.

Do you have pull-up or pulldown resistor for your switch?

Case 2 misses a break statement.

As I read this code I expect this to happen (assuming your debounce works as intended): start up (Mode == 0): LED1 off, LED2 off. Button press (Mode == 1): LED1 on, LED2 off. Button press (Mode == 2): LED1 off, LED2 on, but then due to the missing break that becomes LED1 on, LED2 on. Button press (Mode == 3): LED1 on, LED2 on. Button press (Mode == 0): LED1 off, LED2 off. Etc.

Hi there,

Sorry about the late reply - basically what is supposed to happen is the LEDs will all be off until the button is pressed and then once the button is pressed they will go off in a sequence - similar to a traffic light system if that makes sense

No... How can they ever go off in sequence (what sequence?) when they're off already?

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks... Tom... :)

chloselfesteem:
Hi there,

Sorry about the late reply - basically what is supposed to happen is the LEDs will all be off until the button is pressed and then once the button is pressed they will go off in a sequence - similar to a traffic light system if that makes sense

  • detecting debounced button *change of state * sets a latch

  • latch enables a free running (self-resetting) timer

  • when timer is done a counter (the switch argument) is incremented and timer resets

  • switch enables/disables desired LED

  • when counter is done (reaches preset) the counter and latch are reset and system awaits next button press