Unit 21: Danger is near

 

 

 

Code for Sketch One: 



// Pin definitions
const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 11;

// Timing variables
const long movementDelay = 10000; // 10 seconds of allowed movement
unsigned long startTime;

// Sensitivity control
const int numSamples = 5; // Number of samples to average
const int distanceThreshold = 30; // Distance threshold in cm

void setup() {
  // Initialize pin modes
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  
  // Start with the buzzer off
  digitalWrite(buzzerPin, LOW);
  
  // Record the start time
  startTime = millis();
}

void loop() {
  long distance = getAverageDistance(); // Get averaged distance reading
  
  // Allow movement for the first 10 seconds
  if (millis() - startTime > movementDelay) {
    if (distance < distanceThreshold) {
      digitalWrite(buzzerPin, HIGH); // Sound the alarm
    } else {
      digitalWrite(buzzerPin, LOW); // No alarm
    }
  }

  delay(500); // Small delay between readings
}

// Function to get averaged distance from multiple readings
long getAverageDistance() {
  long totalDistance = 0;

  for (int i = 0; i < numSamples; i++) {
    long duration, distance;

    // Send a pulse to trigger the sensor
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    // Read the echo pin and convert the time into distance
    duration = pulseIn(echoPin, HIGH);
    distance = (duration * 0.0343) / 2; // in cm

    totalDistance += distance;
    delay(50); // Small delay between samples
  }

  return totalDistance / numSamples; // Return the averaged distance
}


  

Code for Sketch Two:



// Pin definitions
const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 11;

// Timing variables
const long movementDelay = 10000; // 10 seconds of allowed movement
unsigned long startTime;

// Sensitivity control
const int numSamples = 5; // Number of samples to average
const int distanceThreshold = 30; // Distance threshold in cm

// Counter variables
int alarmCount = 0; // Count how many times the alarm has been triggered
const int maxAlarmCount = 6; // Alarm rings continuously after 6 activations

void setup() {
  // Initialize pin modes
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  
  // Start with the buzzer off
  digitalWrite(buzzerPin, LOW);
  
  // Record the start time
  startTime = millis();
}

void loop() {
  long distance = getAverageDistance(); // Get averaged distance reading
  
  // Allow movement for the first 10 seconds
  if (millis() - startTime > movementDelay) {
    if (distance < distanceThreshold) {
      alarmCount++; // Increment the alarm count when movement is detected
      if (alarmCount >= maxAlarmCount) {
        digitalWrite(buzzerPin, HIGH); // Sound alarm continuously
      } else {
        digitalWrite(buzzerPin, HIGH); // Sound alarm for this activation
        delay(1000); // Brief delay to indicate activation
        digitalWrite(buzzerPin, LOW); // Turn off after sounding
      }
    }
  }

  delay(500); // Small delay between readings
}

// Function to get averaged distance from multiple readings
long getAverageDistance() {
  long totalDistance = 0;

  for (int i = 0; i < numSamples; i++) {
    long duration, distance;

    // Send a pulse to trigger the sensor
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    // Read the echo pin and convert the time into distance
    duration = pulseIn(echoPin, HIGH);
    distance = (duration * 0.0343) / 2; // in cm

    totalDistance += distance;
    delay(50); // Small delay between samples
  }

  return totalDistance / numSamples; // Return the averaged distance
}

  

 

Notes: