Unit 9: Bad Vibrations

 

 

 

Code: 


const int piezoPin = A0;       // Analog pin for the piezo sensor
const int buzzerPin = 9;       // Pin for the active buzzer

// Vibration threshold (adjust based on testing)
const int threshold = 250;

// Alarm duration in milliseconds
const int alarmDuration = 1000;

void setup() {
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(9600);          // Begin serial communication for debugging
}

void loop() {
  int piezoValue = analogRead(piezoPin);
  Serial.println(piezoValue);  // Debug: print sensor reading

  if (piezoValue > threshold) {
    triggerAlarm();
  }

  delay(100); // Prevent serial flooding
}

void triggerAlarm() {
  digitalWrite(buzzerPin, HIGH);
  delay(alarmDuration);
  digitalWrite(buzzerPin, LOW);
}

  

 

Notes: