-
Notifications
You must be signed in to change notification settings - Fork 13
/
ESP8266-RCWL0516.ino
175 lines (152 loc) · 4.56 KB
/
ESP8266-RCWL0516.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* ************************************************************************ */
/*
Detects the state of an input and announces new states in the console
window.
This code could be used with any of the GPIO pins as long as their
configured as inputs.
There are two methods of state-change detection in this sketch :
* Polled - The input pin is read in the loop() function and if the
current input state doesn't match the last changed state then
announce the change and save the state.
* Interrupt - There are two methods used here for invoking an
input interrupt when the input state changes :
* Interrupt on Change
* Interrupt on a Level, toggle between levels on subsequent
level changes.
*/
// change as necessary
#define SERIAL_BAUD 115200
// The pin and LED states
#define SENSOR_PIN D2
#define LED_BUILTIN_ON false
#define LED_BUILTIN_OFF true
/*
You can choose between polled or interrupt driven
sensor state-change detection.
Good info on interrupts -
http://gammon.com.au/interrupts
*/
//#define POLLED
// - OR -
#define INTERR
// this will enable interrupt on change instead of
// interrupt on level (low vs high)
//#define INTERR_CHG
#ifdef POLLED
#ifdef INTERR
There can be only one
#endif
#endif
/* ************************************************************************ */
/*
Two variables are needed when polling the sensor's current state. Each
pass through loop() will read the sensor state and compare it against
the last state that was saved when there was a transition of the sensor
state.
*/
#ifdef POLLED
bool state;
bool lastState;
#endif
/* ************************************************************************ */
/*
Gotta start somewhere...
*/
void setup()
{
Serial.begin(SERIAL_BAUD);
Serial.println();
Serial.println();
setUpIO();
#ifdef POLLED
// initialize the states...
state = false;
lastState = false;
#endif
#ifdef INTERR
#ifdef INTERR_CHG
// attach an interrupt handler to run when the input changes
attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), sensorHandler, CHANGE);
#else
// attach an interrupt handler to run when the input is going low
attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), sensorHandlerActive, FALLING);
#endif
#endif
}
/*
Set up the GPIO pins as needed...
*/
void setUpIO()
{
// set up the built-in LED for use...
pinMode(LED_BUILTIN, OUTPUT);
// turn off the on-board LED
digitalWrite(LED_BUILTIN, LED_BUILTIN_OFF);
// set up the pin to read the sensor state
// NOTE: An external pull-up resistor was used in
// the circuit where this sketch was developed.
pinMode(SENSOR_PIN, INPUT);
}
#ifdef INTERR
/* ************************************************************************ */
#ifdef INTERR_CHG
/*
A single interrupt handler for "change" on the input pin.
*/
void sensorHandler()
{
bool state = digitalRead(SENSOR_PIN);
// indicate the new sensor state - active
setLED(state);
}
#else // INTERR_CHG
/*
The ESP8266 only allows one handler per pin. That is why each handler
attaches the opposite handler before returning.
*/
void sensorHandlerActive()
{
// indicate the new sensor state - active
setLED(LED_BUILTIN_ON);
// attach an interrupt handler to run when the input is going high
detachInterrupt(digitalPinToInterrupt(SENSOR_PIN));
attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), sensorHandlerIdle, RISING);
}
void sensorHandlerIdle()
{
// indicate the new sensor state - idle
setLED(LED_BUILTIN_OFF);
// attach an interrupt handler to run when the input is going low
detachInterrupt(digitalPinToInterrupt(SENSOR_PIN));
attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), sensorHandlerActive, FALLING);
}
#endif // INTERR_CHG
/*
*/
void setLED(bool state)
{
digitalWrite(LED_BUILTIN, state);
// sensor indicates active when it pulls
// the input to 0.
Serial.println("interr - " + String(state ? "IDLE" : "ACTIVE"));
}
#endif // INTERR
void loop()
{
#ifdef POLLED
// read the state of the sensor and see if it
// matches the last known state
if((state = digitalRead(SENSOR_PIN)) != lastState)
{
// no match, change the state of the LED
digitalWrite(LED_BUILTIN, state);
// remember this state as the last
lastState = state;
// sensor indicates active when it pulls
// the input to 0.
Serial.println("polled - " + String(state ? "IDLE" : "ACTIVE"));
}
yield();
delay(500);
#endif
}