Netherlands
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« on: February 03, 2013, 07:09:20 am » |
Hello, I tryed the next sketch: // 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 ? Greetz , ArduinoPat
|
|
|
|
|
Logged
|
Patrick ,The Netherlands
|
|
|
|
Poole, Dorset, UK
Offline
God Member
Karma: 8
Posts: 669
|
 |
« Reply #1 on: February 03, 2013, 07:12:53 am » |
for(int index = 0; index < 4; index++)
Wrong, array number from 0 so you have indexes 0,1 and 2. Mark
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #2 on: February 03, 2013, 07:31:56 am » |
Many thanks for quick reply Mark, 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 ;-(( Regards ArduinoPat,
|
|
|
|
|
Logged
|
Patrick ,The Netherlands
|
|
|
|
Poole, Dorset, UK
Offline
God Member
Karma: 8
Posts: 669
|
 |
« Reply #3 on: February 03, 2013, 07:40:06 am » |
val and old_val also need to be arrays!.
Mark
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #4 on: February 03, 2013, 07:43:06 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #5 on: February 03, 2013, 07:50:38 am » |
a few more advice, debounce and StateChange detection will do wonders for your program
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #6 on: February 03, 2013, 07:58:21 am » |
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
|
|
|
|
|
Logged
|
Patrick ,The Netherlands
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #7 on: February 03, 2013, 08:01:39 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Poole, Dorset, UK
Offline
God Member
Karma: 8
Posts: 669
|
 |
« Reply #8 on: February 03, 2013, 08:05:10 am » |
int val = 0; becomes int val[3] = {0,0,0};
Mark
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #9 on: February 03, 2013, 08:14:59 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #10 on: February 03, 2013, 08:36:22 am » |
Many thanks ash, But i tryed to connect more swithes at this sketch but i make a mess of this ;-(( int Switch [] = {2,3,4}; int Led [] = {11,12,13};
boolean LedState =LOW; int SwitchDebounce; int LastSwitchState=HIGH; int LastSwitchDebounce=LOW;
unsigned long LastDebounceTime = 0; unsigned long DebounceDelay = 50;
void setup() { for (int index = 0; index < 4; index++) pinMode(Switch[index],INPUT); digitalWrite(Switch[index],HIGH); pinMode(Led[index],OUTPUT); }
void loop() { for (int index = 0; index < 4; index++) { int CurrentSwitch = digitalRead(Switch[index]); if (CurrentSwitch != LastSwitchDebounce) { LastDebounceTime = millis(); } if ((millis() - LastDebounceTime) > DebounceDelay) { if (CurrentSwitch != LastSwitchState) { if (CurrentSwitch == LOW) { LedState = !LedState; } } LastSwitchState=CurrentSwitch; } digitalWrite(Led[index],LedState); LastSwitchDebounce = CurrentSwitch; } }
Your sketch is workin well , as you told !! thanks for this ! Regards ArduinoPat,
|
|
|
|
|
Logged
|
Patrick ,The Netherlands
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #11 on: February 03, 2013, 09:56:31 am » |
try making this part as an array too boolean LedState =LOW; int SwitchDebounce; int LastSwitchState=HIGH; int LastSwitchDebounce=LOW; unsigned long LastDebounceTime = 0;
because all this can only hold info for one switch, more then that you need a way to hold all that info
|
|
|
|
|
Logged
|
|
|
|
|
|