-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldr.ino
75 lines (57 loc) · 1.63 KB
/
ldr.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "config.h"
// analog pin 0
#define LDR A0
#define LED_PIN 5
// photocell state
int current = 0;
int last = -1;
// set up the 'analog' feed
AdafruitIO_Feed *analog = io.feed("photocell");
AdafruitIO_Feed *digital = io.feed("digital");
void setup() {
// start the serial connection
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
// wait for serial monitor to open
while(! Serial);
// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();
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();
// grab the current state of the photocell
current = analogRead(LDR);
// return if the value hasn't changed
if(current == last)
return;
// save the current state to the analog feed
Serial.print("sending -> ");
Serial.println(current);
analog->save(current);
// store last photocell state
last = current;
// wait one second (1000 milliseconds == 1 second)
delay(1000);
}
void handleMessage(AdafruitIO_Data *data) {
Serial.print("received <- ");
if(data->toPinLevel() == HIGH)
Serial.println("HIGH");
else
Serial.println("LOW");
digitalWrite(LED_PIN, data->toPinLevel());
}