I'm doing a project that detect a pattern using vibration sensor. This sensor place on the house left gate. When the system is on, it will read the sensor readings. When there's 5 consecutive '0' readings, it will go the next part. The next part is when there's a readings > 0 for a certain time, then back to 5 consecutive '0' readings, then it will triggered the buzzer. The buzzer will sound in infinite loop until I press reset on the arduino.
There are 2 scenario in this project. 1st when I throw something at the gate and 2nd when I open the gate. When I look at the serial monitor to learn the pattern, 1st scenario start with '0' readings for sometime then there's a value when I throw something at gate, then it back to '0'. For 2nd scenario, since I using swing gate, it start with '0' readings, then I open the left gate and there's a value because of the gate vibration, then it back to '0' readings, then there's a value again when I open the right gate, then it back to '0' readings.
What I learn the when open the gate, there's 2-3 of '0' readings between the open the left gate and right gate. How do I make the coding to differentiate the pattern?
With my coding below, it still triggered the buzzer after I open the right gate.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
const int BUTTON_PIN = 2;
const int BUZZER_PIN = 3;
const int VIB_PIN = A0;
int ZeroFlag = 0;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP); // set Arduino pin to input pull-up mode
pinMode(BUZZER_PIN, OUTPUT); // set Arduino pin to output mode
pinMode(VIB_PIN, INPUT);
mySerial.begin(9600); // Setting the baud rate of GSM Module
Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
}
void loop() {
long measurement = vibration();
Serial.println(measurement);
if (digitalRead(BUTTON_PIN) == LOW) {
triggerBuzzer();
}
if (measurement == 0) {
measurement = vibration();
Serial.println(measurement);
if (measurement == 0) {
measurement = vibration();
Serial.println(measurement);
if (measurement == 0) {
measurement = vibration();
Serial.println(measurement);
if (measurement == 0) {
measurement = vibration();
Serial.println(measurement);
if (measurement == 0) {
measurement = vibration();
Serial.println(measurement);
ZeroFlag = 1;
}
}
}
}
}
if (measurement > 0) {
measurement = vibration();
Serial.println(measurement);
if (measurement == 0) {
measurement = vibration();
Serial.println(measurement);
if (measurement == 0) {
measurement = vibration();
Serial.println(measurement);
if (measurement == 0) {
measurement = vibration();
Serial.println(measurement);
if (measurement == 0 && ZeroFlag == 1) {
measurement = vibration();
Serial.println(measurement);
if (measurement == 0) {
triggerBuzzer();
}
}
}
}
}
}
Serial.println("ZeroFlag: " + String(ZeroFlag)); // Display ZeroFlag value on the serial monitor
}
long vibration() {
long measurement = pulseIn(VIB_PIN, HIGH);
return measurement;
}
void triggerBuzzer() {
while (true) {
for (int i = 440; i < 1000; i++) {
tone(BUZZER_PIN, i);
delay(5);
}
for (int i = 1000; i > 440; i--) {
tone(BUZZER_PIN, i);
delay(5);
}
}
}
I just want the buzzer to trigger on the 1st scenario, not on the 2nd scenario.
Really appreciate if anyone can help me
p.s. Ignore the GSM part