|
| 1 | +// Adafruit IO Analog In Example |
| 2 | +// |
| 3 | +// Adafruit invests time and resources providing this open source code. |
| 4 | +// Please support Adafruit and open source hardware by purchasing |
| 5 | +// products from Adafruit! |
| 6 | +// |
| 7 | +// Written by Todd Treece for Adafruit Industries |
| 8 | +// Copyright (c) 2016 Adafruit Industries |
| 9 | +// Licensed under the MIT license. |
| 10 | +// |
| 11 | +// All text above must be included in any redistribution. |
| 12 | + |
| 13 | +/************************** Configuration ***********************************/ |
| 14 | + |
| 15 | +// edit the config.h tab and enter your Adafruit IO credentials |
| 16 | +// and any additional configuration needed for WiFi, cellular, |
| 17 | +// or ethernet clients. |
| 18 | +#include "config.h" |
| 19 | + |
| 20 | +/************************ Example Starts Here *******************************/ |
| 21 | + |
| 22 | +// analog pin 0 |
| 23 | +#define PHOTOCELL_PIN A0 |
| 24 | + |
| 25 | +// photocell state |
| 26 | +int current = 0; |
| 27 | +int last = -1; |
| 28 | + |
| 29 | +// set up the 'analog' feed |
| 30 | +AdafruitIO_Feed *analog = io.feed("analog"); |
| 31 | + |
| 32 | +void setup() { |
| 33 | + |
| 34 | + // start the serial connection |
| 35 | + Serial.begin(115200); |
| 36 | + |
| 37 | + // wait for serial monitor to open |
| 38 | + while(! Serial); |
| 39 | + |
| 40 | + // connect to io.adafruit.com |
| 41 | + Serial.print("Connecting to Adafruit IO"); |
| 42 | + io.connect(); |
| 43 | + |
| 44 | + // wait for a connection |
| 45 | + while(io.status() < AIO_CONNECTED) { |
| 46 | + Serial.print("."); |
| 47 | + delay(500); |
| 48 | + } |
| 49 | + |
| 50 | + // we are connected |
| 51 | + Serial.println(); |
| 52 | + Serial.println(io.statusText()); |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +void loop() { |
| 57 | + |
| 58 | + // io.run(); is required for all sketches. |
| 59 | + // it should always be present at the top of your loop |
| 60 | + // function. it keeps the client connected to |
| 61 | + // io.adafruit.com, and processes any incoming data. |
| 62 | + io.run(); |
| 63 | + |
| 64 | + // grab the current state of the photocell |
| 65 | + current = analogRead(PHOTOCELL_PIN); |
| 66 | + |
| 67 | + // return if the value hasn't changed |
| 68 | + if(current == last) |
| 69 | + return; |
| 70 | + |
| 71 | + // save the current state to the analog feed |
| 72 | + Serial.print("sending -> "); |
| 73 | + Serial.println(current); |
| 74 | + analog->save(current); |
| 75 | + |
| 76 | + // store last photocell state |
| 77 | + last = current; |
| 78 | + |
| 79 | + // wait one second (1000 milliseconds == 1 second) |
| 80 | + delay(1000); |
| 81 | +} |
0 commit comments