Alarm System – Analog and Digital Sensors

I would like to start off by saying that I’m actually not very proud of my assignment this week. This is because, for the first time, I felt like I didn’t really go beyond what we did in class.
However, the reason for this is that I found out that I have not yet grasped what we did last week. Therefore, I took the time to watch the recorded lectures and read some online resources to fully understand the basics of input and output. This took up most of the time I had for this week’s assignment, so I decided to keep it simple and just use what I have learned to make sure that I now knew how to handle analog and digital input/output.

idea

Trying to incorporate everything that I have learned so far, I came up with this idea. I wanted to create some sort of alarm system, but with a visual signal (represented by a flashing LED) instead of sound.

In the circuit, there are 3 LEDs, 1 push button, and 1 photoresistor.
2 LEDs (green and red) are digital output, and the other LED (yellow) is an analog output.
Initially, the red and yellow LEDs are off. The green LED is on, meaning the alarm system hasn’t been triggered.
The closer you get to the photoresistor, the brighter the yellow LED lights up, signaling that something is getting close.
When you actually touch the photoresistor, the alarm system is triggered: the green LED turns off, the yellow LED is at its brightest, and the red LED starts to flash.
If you want to turn off the “alarm”, you press the button which turns the green LED back on and the red LED off.

NOTe

I also ended up adding a boolean alarmStopped that does the following:

        • If something touches the photoresistor but doesn’t move away and stays still, you can still shut off the alarm.
        • As soon as it moves and touches the photoresistor again, the alarm system is triggered once again.

circuit and code

int green = 2; //green LED
int red = 3; //red LED
bool redState = LOW;
int b = 4; //buton
bool prevBState = LOW; //to check previous button state
int yellow = 5; //yellow LED
int pr = A0; //photoresitor
bool alarm; //to check if alarm is triggered
bool alarmStopped; //to check if alarm has been stopped
//timer to make yellow LED flash
long timer;
int timerLength = 100;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(red,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(yellow,OUTPUT);
  pinMode(b,INPUT);
  pinMode(pr,INPUT);
  alarm = false;
  alarmStopped = false;
  digitalWrite(red,LOW);
  digitalWrite(green,HIGH);
  timer = millis();
}

void loop() {
  // put your main code here, to run repeatedly:
  //analog read photoresistor values
  int prValue = analogRead(pr); 
  //digitally read current button state
  int bState = digitalRead(b); 
  
  //Serial.println(prValue);
  
  //map the photoresistor value (from min 270 to max 640) to the LED needed value 
  //and constrain it between those values 0 and 255
  int mappedValue = 255- constrain(map(prValue,270,640,0,255),0,255);
  //light the yellow LED according to the photoresistor value
  analogWrite(yellow,mappedValue);
  
  //Serial.println(mappedValue);
  
  //in my case, the photoresistor has been touched when the LED value is around 150
  if (mappedValue > 150 && alarmStopped == false)
  {
    alarm = true;
  }
  //if alarm has been triggered, flash red LED and turn off green LED
  if (alarm == true)
  {
    if (millis()>timer){
      redState = !redState;
      timer = millis() + timerLength;
    }
    digitalWrite(red,redState);
    digitalWrite(green,LOW);
  }
  //if button has been pressed
  if (bState == HIGH && prevBState == LOW)
  {
    alarmStopped = true; //alarm system has been stopped
    alarm = false; //alarm is not currently triggered
    digitalWrite(red,LOW); //turn off red LED
    digitalWrite(green,HIGH); //turn green LED back on
  }
  prevBState = bState; //update the previous button state every frame
  //this is to allow the button to turn off the alarm
  //even if we're still touch the photoresistor
  //however the alarm will be triggered again if you move and touch the photoresistor once more 
  if (alarm == false && mappedValue< 150)
  {
    alarmStopped = false;
  }
}

 

Final outcome

Leave a Reply