I’m having problem with my project. Everything works until I try to send email using Blynk.email()
What happens is, my counter is set to 0, when you trigger the sensor first time, it counts 1 and plays Audio1, the second time you hit it, it counts 2 and plays Audio2. After about 5 seconds it stops and resets counter.
Everything works to this point as planned.
but!
When I add Blynk.email("user@email.com", “Subject”, “Alarm Triggered!”); to one of the if() statements
I still receive email but the counter stops counting and gets stuck in 1. It never counts up after you hit the sensor.
Here is my code:
#define Audio1 2 // Primary Warning
#define Audio2 3 // Secondary Warning
#define BLYNK_PRINT Serial // Blynk
#include <SPI.h> // Blynk
#include <WiFi101.h> // Blynk
#include <BlynkSimpleWiFiShield101.h> // Blynk
int VibrationSensor1 = 10; // Sensor #1
int VibrationSensor2 = 11; // Sensor #2
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 1000;
const unsigned long ALARM_DURATION = 5000; // 5 Seconds Timeout
const unsigned long HIT_RESET_TIME = 900000; // 15 Minutes Reset Time
char auth[] = “AUTH TOKEN”; // Blynk
char ssid[] = “SSID”; // Blynk
char pass[] = “PASSWORD”; // Blynk
void setup() {
pinMode(Audio2, OUTPUT); // Audio2 OUTPUT
digitalWrite(Audio2, HIGH); // Audio2 Standby
pinMode(Audio1, OUTPUT); // Audio1 OUTPUT
digitalWrite(Audio1, HIGH); // Audio1 Standby
pinMode(VibrationSensor1, INPUT); // set VibrationSensor1 input for measurment
pinMode(VibrationSensor2, INPUT); // set VibrationSensor2 input for measurment
Serial.begin(9600); // init serial 9600
startMillis = millis(); // initial start time
delay(5000);
Serial.println(“System Ready!”);
Blynk.begin(auth, ssid, pass);
}
void loop() {
static int hitCount;
static unsigned long alarmStartMs; // when alarm was turned on
static boolean alarmOn; // is alarm turned on?
static unsigned long firstHitMs; // when the first hit happened
long measurement1 = TP_init1();
long measurement2 = TP_init2();
delay(2);
Serial.print(“HitCount = “);
Serial.print(hitCount);
Serial.println(””);
if (alarmOn) {
if (millis() - alarmStartMs >= ALARM_DURATION) {
Serial.println("Turn Alarm Off");
//reset variables
alarmOn = false;
hitCount = 0;
//turn off
digitalWrite(Audio2, HIGH); // Turn OFF Audio2
}
}
if (hitCount > 0 && millis() - firstHitMs >= HIT_RESET_TIME) {
hitCount = 0;
}
if ((measurement1 >= 1000) || (measurement2 >= 500)) { //Sensitivity of Sensor 1 and Sensor 2
hitCount += 1;
if (hitCount <= 1) {
//First hit
Serial.println("First Hit...");
digitalWrite(Audio1, LOW); // Turn ON Audio1
delay(200);
digitalWrite(Audio1, HIGH); // Turn OFF Audio1
Blynk.email("user@email.com", "Subject", "Alarm Triggered!"); // HERE IS MY PROBLEM, ONCE THIS LINE IS ACTIVE COUNTER STOPS WORKING
}
else if (hitCount >= 2 && !alarmOn) {
//Second hit
Serial.println("Second hit - Turn alarm on!");
digitalWrite(Audio2, LOW); // Turn ON Audio2
alarmOn = true;
alarmStartMs = millis();
}
}
Blynk.run();
}
long TP_init1() {
long measurement1 = pulseIn (VibrationSensor1, HIGH); //wait for the pin to get HIGH and returns measurement //Sensor 1
return measurement1;
}
long TP_init2() {
long measurement2 = pulseIn (VibrationSensor2, HIGH); //wait for the pin to get HIGH and returns measurement //Sensor 2
return measurement2;
}
Thank you!