header
Programming Alarm

We want to use the light sensor to detect if the amount of light hitting the oven has changed. If it has, then we want to sound an alarm to tell us to reposition the oven to catch more sun. So, we need to change our code so that some sounds come out of the speaker if the value of the light sensor drops below a certain value. What value? Remember that light sensors are not that accurate, so you may have to play around with the value you use to sound the alarm.

// include the library code
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12,11,5,4,3,2);

// initialize our variables
int sensorPin = 0;
int photocellPin = 1;
int speaker = 10;
int tempC, tempF, lightReading;
int alarmLevel = 800;

// some notes to use on the speaker
int G = 392;
int C = 523;

void setup() {
  // set up the LCD's number of columns and rows
  lcd.begin(16, 2);
  
  // set up speaker
  pinMode(speaker, OUTPUT);
}

void loop() {  
  tempC = get_temperature(sensorPin);
  tempF = celsius_to_fahrenheit(tempC);
  lightReading = get_light_reading(photocellPin);
  
  if (lightReading < alarmLevel) sound_alarm();
  
  lcd.setCursor(0,0);
  lcd.print(tempF); lcd.print(" "); lcd.print((char)223); lcd.print("F");

  lcd.setCursor(0,1);
  lcd.print("Light: "); lcd.print(lightReading); lcd.print(" lux");
  delay(200);
}

void sound_alarm() {
  tone(speaker, G);
  delay(200);
  tone(speaker, C);
  delay(200);
  tone(speaker, G);
  delay(200);
  noTone(speaker);
  
}

int get_light_reading(int pin) {
  int photocellReading = analogRead(pin);
  return photocellReading;
}

int get_temperature(int pin) {
  // We need to tell the function which pin the sensor is hooked up to. We're using
  // the variable pin for that above
  
  // Read the value on that pin
  int temperature = analogRead(pin);
  
  // Calculate the temperature based on the reading and send that value back
  float voltage = temperature * 3.6;
  voltage = voltage / 1024.0;
  return ((voltage - 0.5) * 100);
}

int celsius_to_fahrenheit(int temp) {
  return (temp * 9 / 5) + 32;
}


Try covering up the light sensor with your hand to see if the alarm goes off.

Connect Potentiometer