// test 3 BUTTON , with 3 LEDS , Array.
// One Push Button , LED = ON and stay on !!
// After push the button , LED = OFF and stay off !!
// ArduinoPat
int LED [] = {11,12,13}; // the pin for the LED
int BUTTON[] = {7,8,9}; // the input for the BUTTON
int val = 0; // val will be used to store the state
// of the BUTTON pin
int old_val = 0; // this variable stores the previous
// value of "val"
int state = 0; // 0 = LED off and 1 = LED on
void setup()
{
for(int index = 0; index < 4; index++)
{
pinMode(LED[index], OUTPUT); // tell Arduino LEDS is an output
pinMode(BUTTON[index], INPUT); // tell Arduino BUTTON is an input
digitalWrite(BUTTON[index],HIGH); // Pull-up Resistor
}
}
void loop(){
for(int index = 0; index < 4; index)
{
int val = digitalRead(BUTTON[index]); // Against bouncing
if ((val == HIGH) && (old_val == LOW)) // Against bouncing
{
state = 1 - state; // Against bouncing
delay(10); // Against bouncing
}
old_val = val; // val is now old, let's store it
if (state == 1) {
digitalWrite(LED[index], HIGH); // turn LED ON
}
else
{
digitalWrite(LED[index], LOW); // turn LED OFF
}
}
}
The meaning of this sketch is :
3 buttons , and 3 LEDs
push button one LED one is ON , and stay on ,after push again this button the LED goes off
Push button two LED two is ON , and stay on , after push again this button the LED goes off
Push button three LED three is ON , and stay on , after push again this button the LED goes off
also i have place a little sketch against bouncing during push the button,i think this is the problem it will not work ??
unfortunately this will not work,
Every button i push nothing happens.
Maybe someone can advice me what iám doing wrong ?
I have change the sketych and try this , but unfortunately it not works either,
// test 3 BUTTON , with 3 LEDS , Array.
// One Push Button , LED = ON and stay on !!
// After push the button , LED = OFF and stay off !!
// ArduinoPat
int LED [] = {11,12,13}; // the pin for the LED
int BUTTON[] = {7,8,9}; // the input for the BUTTON
int val = 0; // val will be used to store the state
// of the BUTTON pin
int old_val = 0; // this variable stores the previous
// value of "val"
int state = 0; // 0 = LED off and 1 = LED on
void setup()
{
for(int index = 0; index < 3; index++)
{
pinMode(LED[index], OUTPUT); // tell Arduino LEDS is an output
pinMode(BUTTON[index], INPUT); // tell Arduino BUTTON is an input
digitalWrite(BUTTON[index],HIGH); // Pull-up Resistor
}
}
void loop(){
for(int index = 0; index < 3; index)
{
int val = digitalRead(BUTTON[index]); // Against bouncing
if ((val == HIGH) && (old_val == LOW)) // Against bouncing
{
state = 1 - state; // Against bouncing
delay(10); // Against bouncing
}
old_val = val; // val is now old, let's store it
if (state == 1) {
digitalWrite(LED[index], HIGH); // turn LED ON
}
else
{
digitalWrite(LED[index], LOW); // turn LED OFF
}
}
}
Only by push the button connect at pin 7 will activate output pin 10,
the other pins will do nothing ;-((
how do you connect the wire for the switches and LEDs?
do you have a pull-up/down for your switches?
what you want your program to do are:
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?
you have set all the Switch and LED in 2 array respectively
but you forget to put an array towards SwitchState and LedState.
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?
try this code but change all the variable to become array
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