Go Down

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

ash901226

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

ArduinoPat

Hello all,

Quote
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.

Quote
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

ash901226

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.

holmes4

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

Mark

ash901226

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()
{
  pinMode(Switch,INPUT);
  digitalWrite(Switch,HIGH);
  pinMode(Led,OUTPUT);
}

void loop()
{
  int CurrentSwitch = digitalRead(Switch);
  if (CurrentSwitch != LastSwitchDebounce)
  {
    LastDebounceTime = millis();
  }
  if ((millis() - LastDebounceTime) > DebounceDelay)
  {
    if (CurrentSwitch != LastSwitchState)
    {
      if (CurrentSwitch == LOW)
      {
        LedState = !LedState;
      }
    }
    LastSwitchState=CurrentSwitch;
  }
  digitalWrite(Led,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