Go Down

Topic: Array problem , 3buttons and three leds (Read 799 times) previous topic - next topic


a few more advice,
debounce and StateChange detection will do wonders for your program


Hello all,

if Switch1 is press,LED1 light up, press the Switch1 again, LED1 turn off?
if Switch2 is press,LED2 light up, press the Switch2 again, LED2 turn off?
if Switch3 is press,LED3 light up, press the Switch3 again, LED3 turn off?

Yes this is the meaning about this sketch.

how do you connect the wire for the switches and LEDs?
do you have a pull-up/down for your switches?

These buttons are pull-down connected,

How can i make the Val and val_old as index ??

Greetz , ArduinoPat
Patrick ,The Netherlands


you could choose to make 2 array for Val and OldVal respectively or if you know how to use Struct in C++ you could make 1 array to hold both value.


int val = 0; becomes int val[3] = {0,0,0};



try this code but change all the variable to become array

Code: [Select]

uint8_t Switch = 2;
uint8_t Led =13;

boolean LedState =LOW;
int SwitchDebounce;
int LastSwitchState=HIGH;
int LastSwitchDebounce=LOW;

unsigned long LastDebounceTime = 0;
unsigned long DebounceDelay = 50;

void setup()

void loop()
  int CurrentSwitch = digitalRead(Switch);
  if (CurrentSwitch != LastSwitchDebounce)
    LastDebounceTime = millis();
  if ((millis() - LastDebounceTime) > DebounceDelay)
    if (CurrentSwitch != LastSwitchState)
      if (CurrentSwitch == LOW)
        LedState = !LedState;
  LastSwitchDebounce = CurrentSwitch;

for one switch it does the work well to make for input internal pull up, debounce and State Change detection. and it does what you want for one Switch, if you need it to work for 3 modify this to suit ur need

Go Up