Wednesday, July 9, 2014

Light Alarm Sketch

I have been playing around with the Sound-Sensor board and finally have a simple "light alarm" working.

/*
  Light Alarm Delayed
  Version 1.0
  2014-07-01 J.G. Wezensky
  
  This sketch will use the light sensor to detect changes in lighting and sound an alarm
  if it changes beyond a pre-determined threshold.  The user can reset the accepted 
  value by clicking the button in the middle.  Then the user will have ten seconds before 
  the alarm is armed.
*/

const int LDR_PIN = 0;
const int SPEAKER_PIN = 3;
const int BUTTON_PIN = 1;

const long DEBOUNCE_DELAY = 40;
const int DEADBAND = 40;
const int ARM_DELAY_SEC = 10;

int lightSetting = 0;
boolean alarmSet = false;
boolean alarmTripped = false;

// tracks the left button
int buttonState;
int lastButtonState = HIGH;
long lastDebounceTime = 0;

float freq = 300;
const float freq_max = 500;
const float freq_min = 300;
float offset = 25;

void setup()
{
  // watch for pin to go to GND
  pinMode(BUTTON_PIN, INPUT);
  digitalWrite(BUTTON_PIN, HIGH);

  // use sound to indicate button press
  pinMode(SPEAKER_PIN, OUTPUT);  
}

void loop()
{
  // check the left button for a mode change
  int reading = digitalRead(BUTTON_PIN);
  if (reading != lastButtonState)
  {
    if (reading == LOW)
      Pressed();
    lastDebounceTime = millis();
  } 
  if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY)
    buttonState = reading;
  lastButtonState = reading;
  
  int light = analogRead(LDR_PIN);  
  if (abs(light - lightSetting) > DEADBAND)
    alarmTripped = true;
  
  if (alarmTripped)
    Buzz();
  else
    noTone(SPEAKER_PIN);
}

// -----------------------------------------------------------------------------
// Processes the press of the primary button
// -----------------------------------------------------------------------------
void Pressed()
{
  int i;
  for (i=0; i<ARM_DELAY_SEC; i++)
  {
    Beep();
    delay(1000);
  }
  lightSetting = analogRead(LDR_PIN);
  alarmTripped = false;
}

// -----------------------------------------------------------------------------
// Audio feedback for button press
// -----------------------------------------------------------------------------
void Beep()
{
  tone(SPEAKER_PIN, 440, 50);
}

void Buzz()
{
  tone(SPEAKER_PIN, freq, 25);
  freq += offset;
  if (freq > freq_max || freq < freq_min)
    offset = -offset;  

}

You should be able to just copy and paste from above.  

Here is how you work it.  After you have programmed the above sketch into your Fidget, replace the Comm board with the Sensor-Sound Board.



Then connect the battery.  It should take a few seconds before the buzzer begins going off.  Now click the button in the middle.  The buzzing should change to a beep every second.  You have ten seconds to put the alarm in a box, drawer or some other enclosure before it becomes armed.  After that, if the light hitting the sensor changes... the buzzer will go off again.  To turn off the alarm, simply unplug the power.


No comments:

Post a Comment