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