Motion Sensor Upgraded

Program


#include <SR04.h>
#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);
int a;

void setup() {
    Serial.begin(9600);
    delay(1000);
    pinMode(8, OUTPUT);
}

void loop() {
    a = sr04.Distance();
    
    if (a < 12) //Checks if object is less than 12cm
    {
        for(int i = 0; i<3; i++) 
        {
            digitalWrite(8, HIGH); //Turns buzzer on
            digitalWrite(4, HIGH); //Turns red light on
            delay(100);
            digitalWrite(8, LOW); //Turns buzzer on
            digitalWrite(4, LOW); //Turns red light off
            delay(100);
        }
    }
    if (a > 12 && a < 24) //Checks if object is between 12cm and 24cm
    {
        digitalWrite(4, LOW); //Turns red light off
        digitalWrite(7, LOW); //Turns green light off
        digitalWrite(9, HIGH); //Turns yellow light on
    }
    
    if (a > 24)  //Checks if object is greater than 24cm
    {
        
        digitalWrite(4, LOW); //Turns red light off
        digitalWrite(7, LOW); //Turns yellow light off
        digitalWrite(9, HIGH); //Turns green light on
    }
    delay(250);
}