Help with checking for constant vibration within timeframe

Hi all,

I'm new to all this stuff and trying to learn as I go along. I've managed to create an alarm project from this guide: Intruder Security Alarm using Vibration Sensor | The IOT Projects

The code is as follows:

//The IoT Projects: https://theiotprojects.com 
//Attiny85 Intruder Security Alarm using Viabration Sensor
// Using Digispark ATTINY85

int Buzz = 3; //Buzzer
int LED = 1;  //LED
int vs =2; // vibration sensor
int i=0;
void setup(){
  pinMode(Buzz, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(vs, INPUT); 
}
void loop(){
int vib = digitalRead(vs);
  if(vib == HIGH)
  {
    for(i=0;i<2;i++)
    {
    digitalWrite(Buzz, HIGH);
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(Buzz, LOW); 
    digitalWrite(LED, LOW); 
    delay(100);
  }
  }
  else{
    digitalWrite(Buzz, LOW);
    digitalWrite(LED, LOW); 
  }
}

Everything is working great but I need help adding something to do it.

The alarm sounds every time vibration is detected. Now what I want to do is:

When vibration detected, sound alarm twice (as the code currently does) as a warning, then if the warning alarm is activated X number of times within 30 seconds then sound the main alarm, ie: Alarm sounds forever

But I'm new to coding and I can't figure out how I would:
Start timer on first alarm activation
Count the number of times alarm is activated
If alarm has been activated X number of times within 30 seconds then keep alarm sounding forever
If alarm hasn't been activated X number of times within 30 seconds, reset timer and activation count.

Can someone help me please?

I think I just need an example so I can get my head around how it works.

Thank you.

Hello
Well, I think you need a time handler using the millis() function. Take a search engine end ask the WWW.

If counting is not taking place (a boolean is false) and the alarm becomes activated then save the value of millis(), set the boolean to true to flag that counting is taking place, set a counter variable to zero and sound the warning

If counting is taking place and the alarm becomes activated increment the counter

If counting is taking place, millis() now minus start millis() previously saved is equal to or greater than 30 seconds and the counter is greater than the trigger number then sound the alarm continuously

If counting is taking place, millis() now minus start millis() previously saved is equal to 30 seconds and the counter is less than the trigger number then turn off counting

Continuous alarm if 12 warnings within 30 seconds.

//The IoT Projects: https://theiotprojects.com
//Attiny85 Intruder Security Alarm using Viabration Sensor
// Using Digispark ATTINY85

const int BuzzPin = 3; //Buzzer
const int LEDPin = 1;  //LED
const int vsPin = 2; // vibration sensor

boolean Counting = false;
unsigned long CountingStartTime;
int Count;
const unsigned long CountTimeLimit = 30000ul; // 30 seconds
const int MAX_COUNT = 12; // How many times in 30 seconds for alarm

void setup()
{
  pinMode(BuzzPin, OUTPUT);
  pinMode(LEDPin, OUTPUT);
  pinMode(vsPin, INPUT);
  digitalWrite(BuzzPin, LOW);
  digitalWrite(LEDPin, LOW);
}

void loop()
{
  int vib = digitalRead(vsPin);

  if (Counting && millis() - CountingStartTime > CountTimeLimit)
  {
    // Counting timed out
    Counting = false;
  }

  if (vib == HIGH)
  {
    // Beep twice as a warning
    for (int i = 0; i < 2; i++)
    {
      digitalWrite(BuzzPin, HIGH);
      digitalWrite(LEDPin, HIGH);
      delay(500);
      digitalWrite(BuzzPin, LOW);
      digitalWrite(LEDPin, LOW);
      delay(100);
    }

    if (Counting)
    {
      // Count another warning
      Count++;

      // Too many warnings within the time period?
      if (Count >= MAX_COUNT)
      {
        digitalWrite(BuzzPin, HIGH);
        digitalWrite(LEDPin, HIGH);
        for (;;); // Infinite loop.  Reset to start over
      }
    }
    else
    {
      // First warning.  Start counting.
      Counting = true;
      CountingStartTime = millis();
      Count = 1;
    }
  }
}

Hi @maxrpg .

simplified flowchart
create variable counter = 0
loop()
did you play once?
yes --> flash alarm --> increment counter -- > time > 30 yes ----> reset counter
counter > X --> yes --> turn on alarm

RV mineirin