Assignment 6: Arduino Input/Output (Police Siren)

Idea:

For my assignment, we were told to deal with one a digital and an analog sensor, hence for my digital sensors, I used two switches and for my analog sensors I went with the light sensor. My idea for this assignment was to mimic police siren lights because I thought it might be creative and cool.

How it would work is you have two red led lights which both have their respective switches. Hence, if you press one switch, one of the red led lights would switch on. Same case if you were to press the other switch. However, if you were to press both switches at the same time, they would blink alternatively like police siren lights.

For the light sensor, I would make it such that if the light sensor reads above some specific threshold of voltage, then it would trigger another yellow led to indicate that the light sensor has switched and this would in turn also active both the red led lights to once again alternate while blinking, mimicking that police siren light effect.

Schematic:

Arduino Code:

const int ANALOG_THRESHOLD = 500;   //Constant Threshold of light used for the Light Sensor. 


void setup() {
  pinMode(13, OUTPUT);  //Top RED LED
  pinMode(8, OUTPUT);   //Bottom RED LED
  pinMode(4, OUTPUT);   //YELLOW LED
  pinMode(A2, INPUT);   //Switch 1 (Bottom Switch)
  pinMode(A3, INPUT);   //Switch 2 (Top Switch)
  pinMode(A0, INPUT);   //Analog Reader for Light Sensor
}

void loop() {
  int analogValue = analogRead(A0);
  int switch1 = digitalRead(A2);
  int switch2 = digitalRead(A3);

  //If the bottom switch is on, then the bottom light will be on (LED connected to pin-8). 
  //Otherwise, it will remain off. 
  if(switch1 == HIGH) {
    digitalWrite(8, HIGH);
  } else {
    digitalWrite(8, LOW);
  }

  //If the top switch is on, then the top light will be on (LED connected to pin-13). 
  //Otherwise, it will remain off. 
  if(switch2 == HIGH) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }

  //If both switches are pressed together, then it will call the police_siren() function:
  if(switch1 == HIGH && switch2 == HIGH) {
    police_siren();
  }

  //Above all else, the switches will remain off if neither of the switches are pressed
  else {
    digitalWrite(13, LOW);
    digitalWrite(8, LOW);
  }


  //This is for the light sensor, if the analogValue read from A0 is greater than the threshold value of light.
  //Then it would let the yellow LED turn on and signal to call the police_siren() function. 
  if(analogValue > ANALOG_THRESHOLD) {
    digitalWrite(4, HIGH);
    police_siren();
  }

  //If the analogValue read is below the threshold of light, then it will remain off. 
  else {
    digitalWrite(4, LOW);
  }
}

//This is the police_siren function which simply takes the two RED LEDS and blinks them alternatively.
//Essentially mimicking a police siren lighting on top of a police car. 
void police_siren() {
    digitalWrite(13, LOW);
    digitalWrite(8, HIGH);
    delay(300);
    digitalWrite(13, HIGH);
    digitalWrite(8, LOW);
    delay(300);
}

Video:

Leave a Reply