Memorial day is 05/25/2015 – Orders will ship out on May 26th. There will not be any deliveries or shipping on Monday. Any order placed after 11am ET on Friday May 22nd will not start to ship out until Tuesday May 26th.

RF M4 Momentary Switch and Software Debounce
Moderators: adafruit_support_bill, adafruit

RF M4 Momentary Switch and Software Debounce

by joshuakane on Wed May 08, 2013 5:28 am

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 | TOGGLE FULL SIZE

#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(); 
  }
}

joshuakane
 
Posts: 215
Joined: Sat Apr 13, 2013 4:40 pm

Re: RF M4 Momentary Switch and Software Debounce

by adafruit_support_bill on Wed May 08, 2013 6:58 am

Code: Select all | TOGGLE FULL SIZE
    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 | TOGGLE FULL SIZE
    readingA = digitalRead(inPinA);

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

adafruit_support_bill
 
Posts: 37526
Joined: Sat Feb 07, 2009 10:11 am

Re: RF M4 Momentary Switch and Software Debounce

by joshuakane on Wed May 08, 2013 7:16 am

Thanks Bill!

I will implement that change!

joshuakane
 
Posts: 215
Joined: Sat Apr 13, 2013 4:40 pm