Loading...
Pages: [1]   Go Down
Author Topic: need help adding debounce to my code  (Read 223 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello all i need  help adding a debounce to this part of my code
Code:
        digital_state0[count] = digitalRead(mux_buttonPin[0]); //set mux0 input val for each digitalstate value
        digital_state1[count] = digitalRead(mux_buttonPin[1]); //set mux1 input val for each digitalstate value
        digital_state2[count] = digitalRead(mux_buttonPin[2]); //set mux2 input val for each digitalstate value
        digital_state3[count] = digitalRead(mux_buttonPin[3]); //set mux3 input val for each digitalstate value
 


here is the full code of the program

Code:

int mux_buttonPin[4] = {5,6,7,8};

int buttonState0 = LOW;
int lastButtonState0 = LOW;

int buttonState1 = LOW;
int lastButtonState1 = LOW;

int buttonState2 = LOW;
int lastButtonState2 = LOW;

int buttonState3 = LOW;
int lastButtonState3 = LOW;



int  digital_state0 [7]  = {
  LOW};//set aside input values for mux


int  digital_state1 [7]  = {
  LOW};//set aside input values for mux


int  digital_state2 [7]  = {
  LOW};//set aside input values for mux


int  digital_state3 [7]  = {
  LOW};//set aside input values for mux


int r0 = 0;      //value select pin at the 4051 (s0)
int r1 = 0;      //value select pin at the 4051 (s1)
int r2 = 0;      //value select pin at the 4051 (s2)

int row = 0;     // storeing the bin code
int count = 0;    // just a count
int  bin [] = {
  000, 1, 10, 11, 100, 101, 110, 111};//bin = binär, some times it is so easy

void setup(){
  for (int i=0; i<=7; i++) {

    pinMode(digital_state0[i], INPUT); //mux0 inputs
    pinMode(digital_state1[i], INPUT); //mux1 inputs
    pinMode(digital_state2[i], INPUT); //mux2 inputs
    pinMode(digital_state3[i], INPUT); //mux3 inputs
  }

  pinMode(2, OUTPUT);    // s0
  pinMode(3, OUTPUT);    // s1
  pinMode(4, OUTPUT);    // s2
  pinMode(mux_buttonPin[0], INPUT);     // mux0 read pin //digital
pinMode(mux_buttonPin[1], INPUT);     // mux1 read pin //digital
pinMode(mux_buttonPin[2], INPUT);     // mux2 read pin //digital
pinMode(mux_buttonPin[3], INPUT);     // mux3 read pin //digital

  Serial.begin(9600);
}
void loop () {

  for (count=0; count<=7; count++) {
    row = bin[count];     
    r0 = row & 0x01;
    r1 = (row>>1) & 0x01;
    r2 = (row>>2) & 0x01;
    digitalWrite(2, r0);
    digitalWrite(3, r1);
    digitalWrite(4, r2);
    //Serial.println(bin[count]); //print current bin
 
   
        digital_state0[count] = digitalRead(mux_buttonPin[0]); //set mux0 input val for each digitalstate value
        digital_state1[count] = digitalRead(mux_buttonPin[1]); //set mux1 input val for each digitalstate value
        digital_state2[count] = digitalRead(mux_buttonPin[2]); //set mux2 input val for each digitalstate value
        digital_state3[count] = digitalRead(mux_buttonPin[3]); //set mux3 input val for each digitalstate value
 
    //Serial.println(digital_state0[count]); // print digital_state0 for debug
  //Serial.println(digital_state1[count]); // print digital_state1 for debug
  //Serial.println(digital_state2[count]); // print digital_state2 for debug
  //Serial.println(digital_state3[count]); // print digital_state3 for debug

    buttonState0 = digital_state0[count];
   
    buttonState1 = digital_state1[count];
   
    buttonState2 = digital_state2[count];
   
    buttonState3 = digital_state3[count];
   
   
    //MUX0
    if (buttonState0 != lastButtonState0) {
      // if the state has changed, increment the counter
      if (buttonState0 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX0 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX0 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState0 = buttonState0;
   
   
     //MUX1
    if (buttonState1 != lastButtonState1) {
      // if the state has changed, increment the counter
      if (buttonState1 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX1 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX1 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState1 = buttonState1;
   
   
 //MUX2
    if (buttonState2 != lastButtonState2) {
      // if the state has changed, increment the counter
      if (buttonState2 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX2 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX2 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState2 = buttonState2;

//MUX3   
    if (buttonState3 != lastButtonState3) {
      // if the state has changed, increment the counter
      if (buttonState3 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX3 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX3 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState3 = buttonState3;
   
   
   
  }
  delay (10);  //delay 10 for read between values

}


here is the scimatics
http://i48.tinypic.com/qsthms.jpg

thank you in advance its driving me crazy i dont know were or how to implement the debounce
Logged

Queens, New York
Offline Offline
Edison Member
*
Karma: 27
Posts: 1539
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do a search in the project guidance forum. Debounce multiple buttons/switches
Logged

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i found this code will it work?
Code:
const long debounceDelay = 20; //The button debounce time

// Button = pin number to read
int checkButton(int Button) {
  int buttonState = digitalRead(Button);    // Read button
  if (buttonState == HIGH) {                // If button pressed then wait a bit to allow for debounce
    long Time = millis();
    while ((millis() - Time) < debounceDelay) {   
    }
    buttonState = digitalRead(Button);      // Read button again
    return buttonState;                     // Return button state
  }
  else {
    return LOW;
  }
}
Logged

Queens, New York
Offline Offline
Edison Member
*
Karma: 27
Posts: 1539
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It will for one button, but there is another code that does it for multiple buttons.
http://arduino.cc/forum/index.php/topic,139805.0.html
Logged

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i was thinking i could just implement it in a loop for 4 times returning diffrent values like this but ill keep looking
Logged

Queens, New York
Offline Offline
Edison Member
*
Karma: 27
Posts: 1539
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The link I gave you, is from my understanding, is what you're looking for.
Logged

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry i didnt see the link before im trying to extract the debounceing part of his code is it more then just
Code:
boolean debounce(int thisButton) {
  boolean current = digitalRead(swiPin[thisButton]);
  if (currentButtonVal[thisButton] != current) {
    delay(5);
    current = digitalRead(swiPin[thisButton]);
  }
  currentButtonVal[thisButton] = current;  // store new state for your pin
  return current;
}
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is this enough for it to function the debounce?
Code:
int swiPin [] = {
  5,6,7,8};
boolean currentButtonVal[3];

void setup(){
  for(int x = 0; x<3; x++){
    currentButtonVal[x] = true;
  }
  Serial.begin(9600);
}




boolean debounce(int thisButton) {
  boolean current = digitalRead(swiPin[thisButton]);
  if (currentButtonVal[thisButton] != current) {
    delay(5);
    current = digitalRead(swiPin[thisButton]);
  }
  currentButtonVal[thisButton] = current;  // store new state for your pin
  return current;
}

void loop(){


  Serial.println(debounce(0));
  Serial.println(debounce(1));
  Serial.println(debounce(2));
  Serial.println(debounce(3));


}
Logged

Queens, New York
Offline Offline
Edison Member
*
Karma: 27
Posts: 1539
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, look at Nick Gammon's code on page 3, its a lot, but it works. And it has a test you can do too.

If you want, you can also take the debounce code in your Arduino examples, and convert it to use arrays and the use of a FOR loop.
« Last Edit: January 11, 2013, 02:43:07 pm by HazardsMind » Logged

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

origionally i tryed using the debouncing libary but i dont know if that would work heres the code with at

Code:
#include <Debounce.h>
int mux_buttonPin[4] = {5,6,7,8};
Debounce debouncer0 = Debounce( 20 , mux_buttonPin[0] );
Debounce debouncer1 = Debounce( 20 , mux_buttonPin[1] );
Debounce debouncer2 = Debounce( 20 , mux_buttonPin[2] );
Debounce debouncer3 = Debounce( 20 , mux_buttonPin[3] );

   
int buttonState0 = LOW;
int lastButtonState0 = LOW;

int buttonState1 = LOW;
int lastButtonState1 = LOW;

int buttonState2 = LOW;
int lastButtonState2 = LOW;

int buttonState3 = LOW;
int lastButtonState3 = LOW;



int  digital_state0 [7]  = {
  LOW};//set aside input values for mux


int  digital_state1 [7]  = {
  LOW};//set aside input values for mux


int  digital_state2 [7]  = {
  LOW};//set aside input values for mux


int  digital_state3 [7]  = {
  LOW};//set aside input values for mux


int r0 = 0;      //value select pin at the 4051 (s0)
int r1 = 0;      //value select pin at the 4051 (s1)
int r2 = 0;      //value select pin at the 4051 (s2)

int row = 0;     // storeing the bin code
int count = 0;    // just a count
int  bin [] = {
  000, 1, 10, 11, 100, 101, 110, 111};//bin = binär, some times it is so easy
void setup(){
  for (int i=0; i<=7; i++) {

    pinMode(digital_state0[i], INPUT); //mux0 inputs
    pinMode(digital_state1[i], INPUT); //mux1 inputs
    pinMode(digital_state2[i], INPUT); //mux2 inputs
    pinMode(digital_state3[i], INPUT); //mux3 inputs
  }

  pinMode(2, OUTPUT);    // s0
  pinMode(3, OUTPUT);    // s1
  pinMode(4, OUTPUT);    // s2
  pinMode(mux_buttonPin[0], INPUT);     // mux0 read pin //digital
pinMode(mux_buttonPin[1], INPUT);     // mux1 read pin //digital
pinMode(mux_buttonPin[2], INPUT);     // mux2 read pin //digital
pinMode(mux_buttonPin[3], INPUT);     // mux3 read pin //digital

  Serial.begin(9600);
}
void loop () {

  for (count=0; count<=7; count++) {
    row = bin[count];     
    r0 = row & 0x01;
    r1 = (row>>1) & 0x01;
    r2 = (row>>2) & 0x01;
    digitalWrite(2, r0);
    digitalWrite(3, r1);
    digitalWrite(4, r2);
    //Serial.println(bin[count]); //print current bin
    debouncer0.update(); // Update the debouncer
    debouncer1.update(); // Update the debouncer
    debouncer2.update(); // Update the debouncer
    debouncer3.update(); // Update the debouncer
   
        digital_state0[count] = digitalRead(debouncer0.read()); //set mux0 input val for each digitalstate value
        digital_state1[count] = digitalRead(debouncer1.read()); //set mux1 input val for each digitalstate value
        digital_state2[count] = digitalRead(debouncer2.read()); //set mux2 input val for each digitalstate value
        digital_state3[count] = digitalRead(debouncer3.read()); //set mux3 input val for each digitalstate value
    //Serial.println(digital_state0[count]); // print digital_state0 for debug
  //Serial.println(digital_state1[count]); // print digital_state1 for debug
  //Serial.println(digital_state2[count]); // print digital_state2 for debug
  //Serial.println(digital_state3[count]); // print digital_state3 for debug

    buttonState0 = digital_state0[count];
   
    buttonState1 = digital_state1[count];
   
    buttonState2 = digital_state2[count];
   
    buttonState3 = digital_state3[count];
   
   
    //MUX0
    if (buttonState0 != lastButtonState0) {
      // if the state has changed, increment the counter
      if (buttonState0 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX0 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX0 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState0 = buttonState0;
   
   
     //MUX1
    if (buttonState1 != lastButtonState1) {
      // if the state has changed, increment the counter
      if (buttonState1 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX1 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX1 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState1 = buttonState1;
   
   
 //MUX2
    if (buttonState2 != lastButtonState2) {
      // if the state has changed, increment the counter
      if (buttonState2 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX2 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX2 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState2 = buttonState2;

//MUX3   
    if (buttonState3 != lastButtonState3) {
      // if the state has changed, increment the counter
      if (buttonState3 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX3 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX3 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState3 = buttonState3;
   
   
   
  }
  delay (10);  //delay 10 for read between values

}
Logged

Queens, New York
Offline Offline
Edison Member
*
Karma: 27
Posts: 1539
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Edit: Your code can be greatly simplified with the correct use of arrays, but try this. All I did was modify the example code.

Code:
/*
 Debounce
 This example code is in the public domain.
 http://www.arduino.cc/en/Tutorial/Debounce
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin[] = {2,3,4,5};
const int ledPins[] =  {8,9,10,11};      // the number of the LED pin

int MAX_PINS = 4;
int i = 0;
int counter=0;
int reading[4];

//byte ledState = HIGH;

int buttonState[4];             // the current reading from the input pin
int lastButtonState[] = {LOW,LOW,LOW,LOW};   // the previous reading from the input pin
long lastDebounceTime[] = {0,0,0,0};  // the last time the output pin was toggled

long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  for(i = 0; i <= MAX_PINS; i++){
    pinMode(buttonPin[i], INPUT);
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  // read the state of the switch into a local variable:
  for(counter = 0; counter <= MAX_PINS; i++)
  {
    reading[counter] = digitalRead(buttonPin[counter]);
    if (reading[counter] != lastButtonState[counter]) {
    lastDebounceTime[counter] = millis();
    }

    if ((millis() - lastDebounceTime[counter]) > debounceDelay) {
      buttonState[counter] = reading[counter];
    }

    digitalWrite(ledPins[counter], buttonState[counter]);
    lastButtonState[counter] = reading[counter];
  }
}


 
« Last Edit: January 11, 2013, 03:46:32 pm by HazardsMind » Logged

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the counter value never goes any higher then 0 in that example?
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

alright im almost there heres what i have
Code:
// constants won't change. They're used here to
// set pin numbers:
const int mux_buttonPin[] = {5,6,7,8};


int MAX_PINS = sizeof(mux_buttonPin)/2-1;
int counter=0;
int reading[sizeof(mux_buttonPin)-1];
int buttonState[sizeof(mux_buttonPin-1)];             // the current reading from the input pin
int lastButtonState[] = {LOW,LOW,LOW,LOW};   // the previous reading from the input pin
long lastDebounceTime[] = {0,0,0,0};  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers



int buttonState0 = LOW;
int lastButtonState0 = LOW;

int buttonState1 = LOW;
int lastButtonState1 = LOW;

int buttonState2 = LOW;
int lastButtonState2 = LOW;

int buttonState3 = LOW;
int lastButtonState3 = LOW;



int  digital_state0 [7]  = {
  LOW};//set aside input values for mux


int  digital_state1 [7]  = {
  LOW};//set aside input values for mux


int  digital_state2 [7]  = {
  LOW};//set aside input values for mux


int  digital_state3 [7]  = {
  LOW};//set aside input values for mux


int r0 = 0;      //value select pin at the 4051 (s0)
int r1 = 0;      //value select pin at the 4051 (s1)
int r2 = 0;      //value select pin at the 4051 (s2)

int row = 0;     // storeing the bin code
int count = 0;    // just a count
int  bin [] = {
  000, 1, 10, 11, 100, 101, 110, 111};//bin = binär, some times it is so easy

void setup(){
   for(int i = 0; i <= MAX_PINS; i++){
    pinMode(mux_buttonPin[i], INPUT);// mux read pin
  }
  for (int i=0; i<=7; i++) {

    pinMode(digital_state0[i], INPUT); //mux0 inputs
    pinMode(digital_state1[i], INPUT); //mux1 inputs
    pinMode(digital_state2[i], INPUT); //mux2 inputs
    pinMode(digital_state3[i], INPUT); //mux3 inputs
  }

  pinMode(2, OUTPUT);    // s0
  pinMode(3, OUTPUT);    // s1
  pinMode(4, OUTPUT);    // s2
  pinMode(mux_buttonPin[0], INPUT);     // mux0 read pin //digital

  Serial.begin(9600);
}
void loop () {

  for (count=0; count<=7; count++) {
    row = bin[count];     
    r0 = row & 0x01;
    r1 = (row>>1) & 0x01;
    r2 = (row>>2) & 0x01;
    digitalWrite(2, r0);
    digitalWrite(3, r1);
    digitalWrite(4, r2);
    //Serial.println(bin[count]); //print current bin
 
   
        digital_state0[count] = digitalRead(mux_buttonPin[0]); //set mux0 input val for each digitalstate value
        digital_state1[count] = digitalRead(mux_buttonPin[1]); //set mux1 input val for each digitalstate value
        digital_state2[count] = digitalRead(mux_buttonPin[2]); //set mux2 input val for each digitalstate value
        digital_state3[count] = digitalRead(mux_buttonPin[3]); //set mux3 input val for each digitalstate value
 
 
 
 
 
 
   // read the state of the switch into a local variable:
  for(int counter = 0; counter <= MAX_PINS; counter++)
 
  {
    reading[counter] = digitalRead(mux_buttonPin[counter]);
    if (reading[counter] != lastButtonState[counter]) {
    lastDebounceTime[counter] = millis();
    }

    if ((millis() - lastDebounceTime[counter]) > debounceDelay) {
      buttonState[counter] = reading[counter];
    }
// Serial.println(counter);
    Serial.println(buttonState[counter]);
   
    lastButtonState[counter] = reading[counter];
  }
 
 
 
 
 
 
 
 
    //Serial.println(digital_state0[count]); // print digital_state0 for debug
  //Serial.println(digital_state1[count]); // print digital_state1 for debug
  //Serial.println(digital_state2[count]); // print digital_state2 for debug
  //Serial.println(digital_state3[count]); // print digital_state3 for debug

    buttonState0 = digital_state0[count];
   
    buttonState1 = digital_state1[count];
   
    buttonState2 = digital_state2[count];
   
    buttonState3 = digital_state3[count];
   
   
    //MUX0
    if (buttonState0 != lastButtonState0) {
      // if the state has changed, increment the counter
      if (buttonState0 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX0 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX0 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState0 = buttonState0;
   
   
     //MUX1
    if (buttonState1 != lastButtonState1) {
      // if the state has changed, increment the counter
      if (buttonState1 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX1 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX1 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState1 = buttonState1;
   
   
 //MUX2
    if (buttonState2 != lastButtonState2) {
      // if the state has changed, increment the counter
      if (buttonState2 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX2 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX2 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState2 = buttonState2;

//MUX3   
    if (buttonState3 != lastButtonState3) {
      // if the state has changed, increment the counter
      if (buttonState3 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX3 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX3 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState3 = buttonState3;
   
   
   
  }
  delay (10);  //delay 10 for read between values

}



heres the part that confused me

Code:
        digital_state0[count] = digitalRead(mux_buttonPin[0]); //set mux0 input val for each digitalstate value
        digital_state1[count] = digitalRead(mux_buttonPin[1]); //set mux1 input val for each digitalstate value
        digital_state2[count] = digitalRead(mux_buttonPin[2]); //set mux2 input val for each digitalstate value
        digital_state3[count] = digitalRead(mux_buttonPin[3]); //set mux3 input val for each digitalstate value
 
 
 
 
 
 
   // read the state of the switch into a local variable:
  for(int counter = 0; counter <= MAX_PINS; counter++)
 
  {
    reading[counter] = digitalRead(mux_buttonPin[counter]);
    if (reading[counter] != lastButtonState[counter]) {
    lastDebounceTime[counter] = millis();
    }

    if ((millis() - lastDebounceTime[counter]) > debounceDelay) {
      buttonState[counter] = reading[counter];
    }
// Serial.println(counter);
    Serial.println(buttonState[counter]);
   
    lastButtonState[counter] = reading[counter];
  }
Logged

Queens, New York
Offline Offline
Edison Member
*
Karma: 27
Posts: 1539
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I made an error in my code, it was this "for(counter = 0; counter <= MAX_PINS; i++)" But change it to "for(counter = 0; counter <= MAX_PINS; counter++)"
It works with multiple buttons, I just tried it and it works perfectly.

EDIT.

Also,
 
Code:
digital_state0[count] = digitalRead(mux_buttonPin[0]); //set mux0 input val for each digitalstate value
  digital_state1[count] = digitalRead(mux_buttonPin[1]); //set mux1 input val for each digitalstate value
  digital_state2[count] = digitalRead(mux_buttonPin[2]); //set mux2 input val for each digitalstate value
  digital_state3[count] = digitalRead(mux_buttonPin[3]); //set mux3 input val for each digitalstate value

Isn't any different from "reading[counter] = digitalRead(mux_buttonPin[counter]);"

« Last Edit: January 11, 2013, 07:04:08 pm by HazardsMind » Logged

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Pages: [1]   Go Up
Print
 
Jump to: