RF M4 Momentary Switch and Software Debounce

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

RF M4 Momentary Switch and Software Debounce

Post by joshuakane »

Hello:

I have a sketch set up to poll for the change in state and then take an action based on that. The input is received via the RF M4 Momentary Switch.

What I am trying to accomplish is that if you push the button an action occurs. Push button "A" and the attached LED's turn off then back on. What I am finding is that when I press the button the action runs, and then when I release the button the action runs a second time. So for each button press the lights turn off then on, then it is repeated a second time. Here is the code I am using, can you let me know if this is by design, or if I am doing something incorrectly?

When I monitor it in Serial Monitor what I see is that the loop is run once as long as I have my finger on the button, as soon as I release the button it registers as a second state change.

Thanks!

Code: Select all


#include <Wire.h>
#include <SPI.h>
#include <Adafruit_WS2801.h>

#define SLEDS 7

int power = SLEDS;
int counter = 0;
//
// LEDS
//

int hStripDataPin  = 6;    // Yellow wire on Adafruit Pixels Strip 2 Stasis 
int hStripClockPin = 3;    // Green wire on Adafruit Pixels Strip 2 Stasis

// Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row
Adafruit_WS2801 stripS = Adafruit_WS2801(SLEDS,sStripDataPin,sStripClockPin);  //Strip for the Stasis Unit

//
// RF Pins
//


int inPinA = 2;         // the number of the input pin (RX) - Button "A"

int counterA = 0;       // how many times we have seen new value    

int readingA;           // the current value read from the input pin A

int current_stateA = LOW;    // the debounced input value


// the following variable is a long because the time, measured in milliseconds,
// will quickly become a bigger number than can be stored in an int.
long timeA = 0;         // the last time the output pin was sampled

int debounce_count = 2; // number of millis/samples to consider before declaring a debounced input

void setup()
{
  int i;
  pinMode(inPinA, INPUT);
 
  // Startup the LED Strip
  stripS.begin();
 
 // Update the strip, to start they are all 'off'
  stripS.show();
  InitStasis();   //Initializing the stasis unit cycling from Red to Green

}
void loop(){
  if(millis() != timeA)
  {
    readingA = digitalRead(inPinA);

    if(readingA != current_stateA)
    {
      current_stateA = readingA;
      Serial.println("Stasis Cycle");
      PowerDown();
      delay(1000);
      PowerUp();
    }    
    timeA = millis();  
  }
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: RF M4 Momentary Switch and Software Debounce

Post by adafruit_support_bill »

Code: Select all

    readingA = digitalRead(inPinA);

    if(readingA != current_stateA)
    {
      current_stateA = readingA;
That code is triggering for every state change. If you only want to perform the action on the positive-going state changes, you can add a nested if:

Code: Select all

    readingA = digitalRead(inPinA);

    if(readingA != current_stateA)
    {
      current_stateA = readingA;
      if (current_stateA == HIGH)
      {
         // do stuff
 

User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Re: RF M4 Momentary Switch and Software Debounce

Post by joshuakane »

Thanks Bill!

I will implement that change!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”