SHOW:
|
|
- or go back to the newest paste.
1 | // Blynk dashboard: https://imgur.com/a/Ql2mr | |
2 | // Used widgets and pins: | |
3 | // TIMER ACTIVE = LED widget, connected to virtual pin 4 (V4) | |
4 | // NOISE LEVEL = Value Display widget, connected to virtual pin 2 (V2) | |
5 | // RUNNING = LED widget, connected to virtual pin 3 (V3) | |
6 | ||
7 | // Those are all Labled Value widgets, Rreadingrate is set to PUSH | |
8 | // HOURS = connected to virtual pin 7 (V7) | |
9 | // MINUTES = connected to virtual pin 6 (V6) | |
10 | // SECONDS = connected to virtual pin 5 (V5) | |
11 | ||
12 | // START/STOP TIMER = Button widget, connected to virtual pin 0 (V0), set to PUSH | |
13 | // RESET TIMER = Button widget, connected to virtual pin 1 (V1), set to PUSH | |
14 | ||
15 | #include <ESP8266WiFi.h> | |
16 | #include <BlynkSimpleEsp8266.h> | |
17 | ||
18 | /* Comment this out to disable prints and save space */ | |
19 | //#define BLYNK_PRINT Serial | |
20 | ||
21 | /**********************/ | |
22 | /* USER CONFIGURATION */ | |
23 | /**********************/ | |
24 | // the following values are the only thing that needs to be adjusted | |
25 | ||
26 | // BLYNK API KEY | |
27 | char auth[] = "xxx"; | |
28 | ||
29 | // WIFI CONFIGRUATION | |
30 | char ssid[] = "xxx"; | |
31 | char pass[] = "xxx"; | |
32 | ||
33 | // NOISE LEVEL CONFIGURATION - this is the main configuration | |
34 | ||
35 | // the adc value (noise level) when the roomba should be considered running | |
36 | // the adc on the ESP8266 ranges from 0 to 1023 (10 bit), | |
37 | #define ON_NOISE_LEVEL 600 | |
38 | // the adc value (noise level) when the roomba should be considered not running | |
39 | #define OFF_NOISE_LEVEL 200 | |
40 | // to prevent false triggeres the sketch takes a specific ("MAX_NOISE_SAMPLES") amount of | |
41 | // noise samples (adc readings) and only if all of the readings are above or below a given | |
42 | // value (based on the variables "ON_NOISE_LEVEL" and "OFF_NOISE_LEVEL") the roomba is | |
43 | // considered running, or not running resp.. | |
44 | // If the roomba is considered running the timer starts | |
45 | #define MAX_NOISE_SAMPLES 10 | |
46 | // the amount of false values that are allowed | |
47 | #define NOISE_SAMPLE_FAILURE_RATE 3 | |
48 | // this is the time (in milliseconds) between each reading | |
49 | // if "MAX_NOISE_SAMPLES" is set 5 and "NOISE_SAMPLE_INTERVAL" is set to 500 it | |
50 | // takes 5 * 500 milliseconds until the roomba is considered running or not running, resp.. | |
51 | #define NOISE_SAMPLE_INTERVAL 250 | |
52 | ||
53 | ||
54 | ||
55 | /***************************/ | |
56 | /* SKETCH VARIABLES */ | |
57 | /* no configuration needed */ | |
58 | /***************************/ | |
59 | ||
60 | #define BLUE_ONBOARD_LED 2 | |
61 | ||
62 | int last_noise_levels[MAX_NOISE_SAMPLES]; | |
63 | int current_position = 0; | |
64 | ||
65 | unsigned long previousMillis = 0; | |
66 | ||
67 | unsigned long second_counter = 0; | |
68 | unsigned long minute_counter = 0; | |
69 | unsigned long hour_counter = 0; | |
70 | ||
71 | boolean roomba_is_running = false; | |
72 | boolean timer_is_running = true; | |
73 | int noise_level = 0; | |
74 | ||
75 | WidgetLED roomba_active_led(V3); // blynk led widget connected to virtual pin 3 (V3) | |
76 | WidgetLED timer_active_led(V4); // blynk led widget connected to virtual pin 4 (V4) | |
77 | ||
78 | BlynkTimer timer; | |
79 | ||
80 | ||
81 | // reads the adc value and ads the value to the "last_noise_levels" array | |
82 | // then checks if all values in the array are above or below the | |
83 | // given ("ON_NOISE_LEVEL" and "OFF_NOISE_LEVEL") values | |
84 | void check_noise() { | |
85 | boolean roomba_has_started = false; | |
86 | boolean roomba_has_stoped = false; | |
87 | ||
88 | last_noise_levels[current_position++] = analogRead(0); | |
89 | ||
90 | if (current_position == MAX_NOISE_SAMPLES) { | |
91 | current_position = 0; | |
92 | } | |
93 | ||
94 | // if all readings expect NOISE_SAMPLE_FAILURE_RATE readings in the array are above "ON_NOISE_LEVEL" the timer starts | |
95 | - | // if all readings in the array are above "ON_NOISE_LEVEL" the timer starts |
95 | + | |
96 | for (int i = 0; i < MAX_NOISE_SAMPLES; i++) { | |
97 | if (last_noise_levels[i] >= ON_NOISE_LEVEL) { | |
98 | roomba_has_started = true; | |
99 | } | |
100 | else { | |
101 | if (++failure_rate >= NOISE_SAMPLE_FAILURE_RATE) { | |
102 | roomba_has_started = false; | |
103 | break; | |
104 | } | |
105 | } | |
106 | } | |
107 | // if all readings expect NOISE_SAMPLE_FAILURE_RATE readings in the array are below "OFF_NOISE_LEVEL" the timer stops | |
108 | - | // if all 3 readings in the array are below "OFF_NOISE_LEVEL" the timer stops |
108 | + | failure_rate = 0; |
109 | - | failure_rate = NOISE_SAMPLE_FAILURE_RATE; |
109 | + | |
110 | if (last_noise_levels[i] <= OFF_NOISE_LEVEL) { | |
111 | roomba_has_stoped = true; | |
112 | } | |
113 | else { | |
114 | if (++failure_rate >= NOISE_SAMPLE_FAILURE_RATE) { | |
115 | roomba_has_stoped = false; | |
116 | break; | |
117 | } | |
118 | } | |
119 | } | |
120 | ||
121 | // final check to see if the roomba is considered running or not running | |
122 | if (roomba_has_started == true) { | |
123 | digitalWrite(BLUE_ONBOARD_LED,LOW); // turn on the onboard led (it is connected "activle low") | |
124 | roomba_active_led.on(); | |
125 | roomba_is_running = true; | |
126 | } | |
127 | if (roomba_has_stoped == true) { | |
128 | digitalWrite(BLUE_ONBOARD_LED,HIGH); | |
129 | roomba_active_led.off(); | |
130 | roomba_is_running = false; | |
131 | } | |
132 | ||
133 | /* | |
134 | Serial.println("------------------"); | |
135 | Serial.print("Roomba has started: "); | |
136 | Serial.println(roomba_has_started); | |
137 | Serial.print("Roomba has stopped: "); | |
138 | Serial.println(roomba_has_stoped); | |
139 | Serial.println("------------------"); | |
140 | Serial.println(current_position); | |
141 | Serial.println("------------------"); | |
142 | for (int i = 0; i < MAX_NOISE_SAMPLES; i++) | |
143 | Serial.println(last_noise_levels[i]); | |
144 | */ | |
145 | } | |
146 | ||
147 | // stop timer - counts up the seconds, minutes and hours | |
148 | void count_up() | |
149 | { | |
150 | second_counter++; | |
151 | if (second_counter == 60) { | |
152 | second_counter = 0; | |
153 | minute_counter++; | |
154 | if (minute_counter == 60) { | |
155 | minute_counter = 0; | |
156 | hour_counter++; | |
157 | } | |
158 | } | |
159 | } | |
160 | ||
161 | // this timer fires every seconds | |
162 | // it is used to send the current adc value and the current time to blynk app | |
163 | // it also counts up the stop timer if the roomba is running | |
164 | // and if the timer is active(not disabled through the user by pressing | |
165 | // the Start/Stop button in the blynk app) | |
166 | void myTimerEvent() | |
167 | { | |
168 | Blynk.virtualWrite(V2, analogRead(0)); | |
169 | Blynk.virtualWrite(V5, second_counter); | |
170 | Blynk.virtualWrite(V6, minute_counter); | |
171 | Blynk.virtualWrite(V7, hour_counter); | |
172 | if (roomba_is_running && timer_is_running) { | |
173 | count_up(); | |
174 | } | |
175 | } | |
176 | ||
177 | // START/STOP BUTTON - STARTS AND STOPS(PAUSE) THE TIMER | |
178 | BLYNK_WRITE(V0) | |
179 | { | |
180 | int pinValue = param.asInt(); // assigning incoming value from pin V0 to a variable | |
181 | //Serial.println(pinValue); | |
182 | ||
183 | if (pinValue == 0) { | |
184 | if (timer_is_running) { | |
185 | timer_active_led.off(); | |
186 | timer_is_running = false; | |
187 | } | |
188 | else { | |
189 | timer_active_led.on(); | |
190 | timer_is_running = true; | |
191 | } | |
192 | } | |
193 | } | |
194 | // RESET BUTTON - RESETS THE TIMER | |
195 | BLYNK_WRITE(V1) | |
196 | { | |
197 | int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable | |
198 | //Serial.println(pinValue); | |
199 | ||
200 | if (pinValue == 0) { | |
201 | second_counter = 0; | |
202 | minute_counter = 0; | |
203 | hour_counter = 0; | |
204 | } | |
205 | Blynk.virtualWrite(V5, second_counter); | |
206 | Blynk.virtualWrite(V6, minute_counter); | |
207 | Blynk.virtualWrite(V7, hour_counter); | |
208 | } | |
209 | ||
210 | void setup() | |
211 | { | |
212 | //Serial.begin(115200); | |
213 | ||
214 | pinMode(BLUE_ONBOARD_LED,OUTPUT); | |
215 | digitalWrite(BLUE_ONBOARD_LED,HIGH); | |
216 | ||
217 | for (int i = 0; i < MAX_NOISE_SAMPLES; i++) { | |
218 | last_noise_levels[i] = 0; | |
219 | } | |
220 | ||
221 | Blynk.begin(auth, ssid, pass); | |
222 | ||
223 | roomba_active_led.off(); | |
224 | timer_active_led.on(); | |
225 | ||
226 | timer.setInterval(1000L, myTimerEvent); | |
227 | } | |
228 | ||
229 | void loop() | |
230 | { | |
231 | Blynk.run(); | |
232 | timer.run(); | |
233 | // read adc value every "NOISE_SAMPLE_INTERVAL" milliseconds | |
234 | unsigned long currentMillis = millis(); | |
235 | if (currentMillis - previousMillis >= NOISE_SAMPLE_INTERVAL) { | |
236 | previousMillis = currentMillis; | |
237 | check_noise(); | |
238 | } | |
239 | } |