Pages: [1]   Go Down
Author Topic: Array problem , 3buttons and three leds  (Read 671 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 49
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I tryed the next sketch:

Code:
// 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 Offline
Edison Member
*
Karma: 50
Posts: 2199
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  for(int index = 0; index < 4; index++)

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

Mark
Logged

Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 49
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Many thanks for quick reply Mark,

I have change the sketych and try this , but unfortunately it not works either,
Code:
// 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 Offline
Edison Member
*
Karma: 50
Posts: 2199
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

val and old_val also need to be arrays!.

Mark
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 49
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Patrick ,The Netherlands

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Edison Member
*
Karma: 50
Posts: 2199
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Mark
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

try this code but change all the variable to become array

Code:
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 Offline
Newbie
*
Karma: 1
Posts: 49
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Many thanks ash,

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

Code:
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 Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

try making this part as an array too
Code:
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

Pages: [1]   Go Up
Jump to: