Created
December 21, 2017 19:44
-
-
Save abachman/4eccad005661786f4d6f4b1aacef2226 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adafruit IO Digital Input Example | |
// Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-digital-input | |
// | |
// Adafruit invests time and resources providing this open source code. | |
// Please support Adafruit and open source hardware by purchasing | |
// products from Adafruit! | |
// | |
// Written by Todd Treece for Adafruit Industries | |
// Copyright (c) 2016 Adafruit Industries | |
// Licensed under the MIT license. | |
// | |
// All text above must be included in any redistribution. | |
/************************** Configuration ***********************************/ | |
// edit the config.h tab and enter your Adafruit IO credentials | |
// and any additional configuration needed for WiFi, cellular, | |
// or ethernet clients. | |
#include "config.h" | |
/************************ Example Starts Here *******************************/ | |
#define BUTTON_PIN 5 | |
#define LED_PIN 4 | |
// 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); | |
pinMode(LED_PIN, OUTPUT); // LED to indicate current button status | |
// 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(); | |
digitalWrite(LED_PIN, current ? HIGH : LOW); | |
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sir,
I have tried this code, working like a charm. But how to add 3 more digital inputs(like digital_1,digital_2,digital_3) to feed and get 3 digital outputs (like led_1,led_2,led_3) from feed? Thanks in advance!