Hey All,
I've been playing with Feather Huzzah and have successfully been uploading temperature data to io.adafruit.com.
Now I want to add a button to control an LED pin. I want to be able to control the pin from a physical button in the real world as well from io.adafruit.com I've been able to accomplish pieces of this by starting with the adafruit io examples
adafruitio_06_digital_in
// digital pin 5
#define BUTTON_PIN 5
// button state
bool current = false;
bool last = false;
// set up the 'digital' feed
AdafruitIO_Feed *digital = io.feed("digital");
void setup() {
// set button pin as an input
pinMode(BUTTON_PIN, INPUT);
// start the serial connection
Serial.begin(115200);
// wait for serial monitor to open
while(! Serial);
// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();
// wait for a connection
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}
// we are connected
Serial.println();
Serial.println(io.statusText());
}
void loop() {
// io.run(); is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
// io.adafruit.com, and processes any incoming data.
io.run();
// grab the current state of the button.
// we have to flip the logic because we are
// using a pullup resistor.
if(digitalRead(BUTTON_PIN) == LOW)
current = true;
else
current = false;
// return if the value hasn't changed
if(current == last)
return;
// save the current state to the 'digital' feed on adafruit io
Serial.print("sending button -> ");
Serial.println(current);
digital->save(current);
// store last button state
last = current;
}
adafruitio_07_digital_out
/************************ Example Starts Here *******************************/
// digital pin 5
#define LED_PIN 5
// set up the 'digital' feed
AdafruitIO_Feed *digital = io.feed("digital");
void setup() {
// set led pin as a digital output
pinMode(LED_PIN, OUTPUT);
// start the serial connection
Serial.begin(115200);
// wait for serial monitor to open
while(! Serial);
// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();
// set up a message handler for the 'digital' feed.
// the handleMessage function (defined below)
// will be called whenever a message is
// received from adafruit io.
digital->onMessage(handleMessage);
// wait for a connection
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}
// we are connected
Serial.println();
Serial.println(io.statusText());
}
void loop() {
// io.run(); is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
// io.adafruit.com, and processes any incoming data.
io.run();
}
// this function is called whenever an 'digital' feed message
// is received from Adafruit IO. it was attached to
// the 'digital' feed in the setup() function above.
void handleMessage(AdafruitIO_Data *data) {
Serial.print("received <- ");
if(data->toPinLevel() == HIGH)
Serial.println("HIGH");
else
Serial.println("LOW");
// write the current state to the led
digitalWrite(LED_PIN, data->toPinLevel());
}
I was able to mash those two sketches together and get them working. The problem is the physical button is coded to be momentary, I think I need to debounce the button to send a single 1 or 0 to io.adafruit.com and the ledPin.
I looked at the debounce example but I haven't been able to encorporate that functionality.
How can I combine the above sketches that provides a debounced button?
Thanks
Rich
