Need help for my project using vibration sensor

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 :slight_smile:
p.s. Ignore the GSM part

What do You want triggerBuzzer to do? How should it work?
This version will make the execution stop for ever at the first call.

triggerBuzzer will make the police siren tone in infinite loop, it will not stop until reset. I want my project to trigger the buzzer when there's vibration 'spike' on the sensor but I need it to able to differentiate between the 'spike' pattern and open gate pattern. So, I need help on the void loop section.

I see.

Do you understand where the "0" comes from? You need to show us what the "patterns" are.

Vibration sensors can be applied in many ways, but the fundamental principle is that they detect vibrations or movements in the object they're attached to. These sensors typically generate a signal or data output that corresponds to the vibration's amplitude and frequency. In the context of an inflatable baby bathtub, for instance, you could use a vibration sensor to monitor water turbulence during a baby's bath. When the baby moves, it would cause the water in the bathtub to vibrate. The sensor could detect these vibrations, allowing you to monitor the baby's activity level during bath time.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.