Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Blynk dashboard: https://imgur.com/a/Ql2mr
- // Used widgets and pins:
- // TIMER ACTIVE = LED widget, connected to virtual pin 4 (V4)
- // NOISE LEVEL = Value Display widget, connected to virtual pin 2 (V2)
- // RUNNING = LED widget, connected to virtual pin 3 (V3)
- // Those are all Labled Value widgets, Rreadingrate is set to PUSH
- // HOURS = connected to virtual pin 7 (V7)
- // MINUTES = connected to virtual pin 6 (V6)
- // SECONDS = connected to virtual pin 5 (V5)
- // START/STOP TIMER = Button widget, connected to virtual pin 0 (V0), set to PUSH
- // RESET TIMER = Button widget, connected to virtual pin 1 (V1), set to PUSH
- #include <ESP8266WiFi.h>
- #include <BlynkSimpleEsp8266.h>
- /* Comment this out to disable prints and save space */
- //#define BLYNK_PRINT Serial
- /**********************/
- /* USER CONFIGURATION */
- /**********************/
- // the following values are the only thing that needs to be adjusted
- // BLYNK API KEY
- char auth[] = "xxx";
- // WIFI CONFIGRUATION
- char ssid[] = "xxx";
- char pass[] = "xxx";
- // NOISE LEVEL CONFIGURATION - this is the main configuration
- // the adc value (noise level) when the roomba should be considered running
- // the adc on the ESP8266 ranges from 0 to 1023 (10 bit),
- #define ON_NOISE_LEVEL 600
- // the adc value (noise level) when the roomba should be considered not running
- #define OFF_NOISE_LEVEL 200
- // to prevent false triggeres the sketch takes a specific ("MAX_NOISE_SAMPLES") amount of
- // noise samples (adc readings) and only if all of the readings are above or below a given
- // value (based on the variables "ON_NOISE_LEVEL" and "OFF_NOISE_LEVEL") the roomba is
- // considered running, or not running resp..
- // If the roomba is considered running the timer starts
- #define MAX_NOISE_SAMPLES 10
- // the amount of false values that are allowed
- #define NOISE_SAMPLE_FAILURE_RATE 3
- // this is the time (in milliseconds) between each reading
- // if "MAX_NOISE_SAMPLES" is set 5 and "NOISE_SAMPLE_INTERVAL" is set to 500 it
- // takes 5 * 500 milliseconds until the roomba is considered running or not running, resp..
- #define NOISE_SAMPLE_INTERVAL 250
- /***************************/
- /* SKETCH VARIABLES */
- /* no configuration needed */
- /***************************/
- #define BLUE_ONBOARD_LED 2
- int last_noise_levels[MAX_NOISE_SAMPLES];
- int current_position = 0;
- unsigned long previousMillis = 0;
- unsigned long second_counter = 0;
- unsigned long minute_counter = 0;
- unsigned long hour_counter = 0;
- boolean roomba_is_running = false;
- boolean timer_is_running = true;
- int noise_level = 0;
- WidgetLED roomba_active_led(V3); // blynk led widget connected to virtual pin 3 (V3)
- WidgetLED timer_active_led(V4); // blynk led widget connected to virtual pin 4 (V4)
- BlynkTimer timer;
- // reads the adc value and ads the value to the "last_noise_levels" array
- // then checks if all values in the array are above or below the
- // given ("ON_NOISE_LEVEL" and "OFF_NOISE_LEVEL") values
- void check_noise() {
- boolean roomba_has_started = false;
- boolean roomba_has_stoped = false;
- last_noise_levels[current_position++] = analogRead(0);
- if (current_position == MAX_NOISE_SAMPLES) {
- current_position = 0;
- }
- // if all readings expect NOISE_SAMPLE_FAILURE_RATE readings in the array are above "ON_NOISE_LEVEL" the timer starts
- int failure_rate = 0;
- for (int i = 0; i < MAX_NOISE_SAMPLES; i++) {
- if (last_noise_levels[i] >= ON_NOISE_LEVEL) {
- roomba_has_started = true;
- }
- else {
- if (++failure_rate >= NOISE_SAMPLE_FAILURE_RATE) {
- roomba_has_started = false;
- break;
- }
- }
- }
- // if all readings expect NOISE_SAMPLE_FAILURE_RATE readings in the array are below "OFF_NOISE_LEVEL" the timer stops
- failure_rate = 0;
- for (int i = 0; i < MAX_NOISE_SAMPLES; i++) {
- if (last_noise_levels[i] <= OFF_NOISE_LEVEL) {
- roomba_has_stoped = true;
- }
- else {
- if (++failure_rate >= NOISE_SAMPLE_FAILURE_RATE) {
- roomba_has_stoped = false;
- break;
- }
- }
- }
- // final check to see if the roomba is considered running or not running
- if (roomba_has_started == true) {
- digitalWrite(BLUE_ONBOARD_LED,LOW); // turn on the onboard led (it is connected "activle low")
- roomba_active_led.on();
- roomba_is_running = true;
- }
- if (roomba_has_stoped == true) {
- digitalWrite(BLUE_ONBOARD_LED,HIGH);
- roomba_active_led.off();
- roomba_is_running = false;
- }
- /*
- Serial.println("------------------");
- Serial.print("Roomba has started: ");
- Serial.println(roomba_has_started);
- Serial.print("Roomba has stopped: ");
- Serial.println(roomba_has_stoped);
- Serial.println("------------------");
- Serial.println(current_position);
- Serial.println("------------------");
- for (int i = 0; i < MAX_NOISE_SAMPLES; i++)
- Serial.println(last_noise_levels[i]);
- */
- }
- // stop timer - counts up the seconds, minutes and hours
- void count_up()
- {
- second_counter++;
- if (second_counter == 60) {
- second_counter = 0;
- minute_counter++;
- if (minute_counter == 60) {
- minute_counter = 0;
- hour_counter++;
- }
- }
- }
- // this timer fires every seconds
- // it is used to send the current adc value and the current time to blynk app
- // it also counts up the stop timer if the roomba is running
- // and if the timer is active(not disabled through the user by pressing
- // the Start/Stop button in the blynk app)
- void myTimerEvent()
- {
- Blynk.virtualWrite(V2, analogRead(0));
- Blynk.virtualWrite(V5, second_counter);
- Blynk.virtualWrite(V6, minute_counter);
- Blynk.virtualWrite(V7, hour_counter);
- if (roomba_is_running && timer_is_running) {
- count_up();
- }
- }
- // START/STOP BUTTON - STARTS AND STOPS(PAUSE) THE TIMER
- BLYNK_WRITE(V0)
- {
- int pinValue = param.asInt(); // assigning incoming value from pin V0 to a variable
- //Serial.println(pinValue);
- if (pinValue == 0) {
- if (timer_is_running) {
- timer_active_led.off();
- timer_is_running = false;
- }
- else {
- timer_active_led.on();
- timer_is_running = true;
- }
- }
- }
- // RESET BUTTON - RESETS THE TIMER
- BLYNK_WRITE(V1)
- {
- int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
- //Serial.println(pinValue);
- if (pinValue == 0) {
- second_counter = 0;
- minute_counter = 0;
- hour_counter = 0;
- }
- Blynk.virtualWrite(V5, second_counter);
- Blynk.virtualWrite(V6, minute_counter);
- Blynk.virtualWrite(V7, hour_counter);
- }
- void setup()
- {
- //Serial.begin(115200);
- pinMode(BLUE_ONBOARD_LED,OUTPUT);
- digitalWrite(BLUE_ONBOARD_LED,HIGH);
- for (int i = 0; i < MAX_NOISE_SAMPLES; i++) {
- last_noise_levels[i] = 0;
- }
- Blynk.begin(auth, ssid, pass);
- roomba_active_led.off();
- timer_active_led.on();
- timer.setInterval(1000L, myTimerEvent);
- }
- void loop()
- {
- Blynk.run();
- timer.run();
- // read adc value every "NOISE_SAMPLE_INTERVAL" milliseconds
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= NOISE_SAMPLE_INTERVAL) {
- previousMillis = currentMillis;
- check_noise();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement