Go Down

Topic: Button array debounce (Read 558 times) previous topic - next topic

aigore

Hi everyone, I'm developing an Arduino project for an installation for a kids audience and need some coding help.
It is supposed to be an interactive carpet interfaced with Millumin software that will run several videos projected on this carpet.
I'd like to use some buttons in specific places to trigger different medias and make the installation variable.
The kids will trigger the buttons and make specific events start.

The buttons are simple buttons connected with a resistor and each button is connected to a digital pin of the board.

I succeded on making the buttons work with the arduino and interface the arduino with the software Millumin that uses it's libraries to talk with the board.

I'd like to add to the code some kind of debounce, so the buttons can be triggered only after a specific time if they were triggered before. I mean that if button n° 1 is pressed, it can be triggered again after eg.10 sec, but in the mean while the others could be triggered.
I'd like that every button beheaves this way.

I compiled this code without debounce
Code: [Select]

/*
  streaming.ino
  Created by Anomes, 2016.
  Creative Commons Attribution-NonCommercial 4.0 International Public License
*/

#include <Arduino.h>
#include <MilluminStream.h>
 
  // First define pins array
int pushButton[] = {
  2, 3, 4, 5, 6
};

int pinCount = 5;

////////////////////////////////////////////////////////////////////////////////
// Initialisation
void setup()
{
  // We setup MilluminStream
  MilluminStream::setup();
  // pin Digital array as input to stream its sensor value
   for (int thisPin = 0; thisPin < pinCount; thisPin++)
  pinMode(pushButton[thisPin], INPUT_PULLUP);
}



////////////////////////////////////////////////////////////////////////////////
// Loop
void loop()
{

  // First, we need to update MilluminStream
  MilluminStream::update();

  for (int thisPin = 0; thisPin < pinCount; thisPin++)
  // Then, we stream pin Digital array
  MilluminStream::sendDigitalForID(pushButton[thisPin]); // Add the value of D3 pin to the frame
 // delay(3000);
  // Stream a custom value
 // uint16_t variable = random(0, 255);
 // MilluminStream::sendVariableForID(0, variable); // Add a custom value to the frame

  // Finally we send just one frame with all our values
  MilluminStream::sendFrame();

}


It works fine and I read each button state.

Then I tried to implement debounce in this way

Code: [Select]

/*
  streaming.ino
  Created by Anomes, 2016.
  Creative Commons Attribution-NonCommercial 4.0 International Public License
*/

#include <Arduino.h>
#include <MilluminStream.h>

// First define pins array
int pushButton[] = {
  2, 3, 4, 5, 6
};

int pinCount = 5;
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

////////////////////////////////////////////////////////////////////////////////
// Initialisation
void setup()
{
  // We setup MilluminStream
  MilluminStream::setup();
  // pin Digital array as input to stream its sensor value
  for (int thisPin = 0; thisPin < pinCount; thisPin++)
    pinMode(pushButton[thisPin], INPUT_PULLUP);
}



////////////////////////////////////////////////////////////////////////////////
// Loop
void loop()
{
  // read the state of the switch into a local variable:
  int reading = digitalRead(pushButton[thisPin]);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // First, we need to update MilluminStream
      MilluminStream::update();

      for (int thisPin = 0; thisPin < pinCount; thisPin++)
        // Then, we stream pin Digital array
        MilluminStream::sendDigitalForID(pushButton[thisPin]); // Add the value of Digital pin to the frame

      // Finally we send just one frame with all our values
      MilluminStream::sendFrame();

    }
  }
}



But it doesn't work and compiles with an error.

I'm not a programmer and my understanding of code is quite limited.
Can anybody please help me?
Many thanks
aigore


Go Up