Go Down

### Topic: Array problem , 3buttons and three leds (Read 1 time)previous topic - next topic

#### ArduinoPat

##### Feb 03, 2013, 01:09 pm
Hello,

I tryed the next sketch:

Code: [Select]
// 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
Patrick ,The Netherlands

#### holmes4

#1
##### Feb 03, 2013, 01:12 pm
Code: [Select]
for(int index = 0; index < 4; index++)

Wrong, array number from 0 so you have indexes 0,1 and 2.

Mark

#### ArduinoPat

#2
##### Feb 03, 2013, 01:31 pm
Many thanks for quick reply Mark,

I have change the sketych and try this , but unfortunately it not works either,
Code: [Select]
// 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,
Patrick ,The Netherlands

#### holmes4

#3
##### Feb 03, 2013, 01:40 pm
val and old_val also need to be arrays!.

Mark

#### ash901226

#4
##### Feb 03, 2013, 01:43 pm
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.

#### ash901226

#5
##### Feb 03, 2013, 01:50 pm
debounce and StateChange detection will do wonders for your program

#### ArduinoPat

#6
##### Feb 03, 2013, 01:58 pm
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?

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

#7
##### Feb 03, 2013, 02:01 pm
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

#8
##### Feb 03, 2013, 02:05 pm
int val = 0; becomes int val[3] = {0,0,0};

Mark

#### ash901226

#9
##### Feb 03, 2013, 02:14 pm
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()
{
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

#### ArduinoPat

#10
##### Feb 03, 2013, 02:36 pm
Many thanks ash,

But i tryed to connect more swithes at this sketch but i make a mess of this ;-((

Code: [Select]
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++)

{
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,
Patrick ,The Netherlands

#### ash901226

#11
##### Feb 03, 2013, 03:56 pm
try making this part as an array too
Code: [Select]

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

Go Up

Please enter a valid email to subscribe