Dark-Be-Gone: a quick LilyPad hack

A tiny hack against the dark, using Lilypad Arduino.

When the light level falls and it starts to get dark, an LED switches on, and the buzzer plays a short tune to ward away the darkness.

This modified buzzer plays a few bars of Do Re Mi. After all, if Julie Andrews can’t chase away the darkness, who can?

Uses a light sensor, LED, LilyPad buzzer.

With thanks to MTU’s frequency of musical notes and Leah Buechley’s sound code.

Update: 17th March 2013

Here’s the code for creating the Dark-Be-Gone, a modified version of Leah Buechley’s sound code. Try changing the notes and timings in the scale to create new tunes with the LilyPad buzzer, using MTU’s frequency of musical notes as a guide.

[c]

/*
Dark-Be-Gone code: alysonfielding.com / @alysonf

This is a modified version of Leah Buechley’s LilyPad Sound Code:
http://web.media.mit.edu/~leah/LilyPad/07_sound_code.html

It also uses MTU’s chart of note frequencies:
http://www.phy.mtu.edu/~suits/notefreqs.html
*/

int ledPin = 11;
int speakerPin = 5;     // speaker connected to digital pin5
int sensorPin = 19;     // light sensor is connected to analog pin 19
int sensorValue;     // variable to store the value coming from the sensor

void setup()
{

pinMode(speakerPin, OUTPUT);     // sets the speakerPin to be an output
pinMode(ledPin, OUTPUT);     // sets the ledPin to be an output
pinMode(sensorPin, INPUT);
Serial.begin(9600);     //initialize the serial port

}

void loop()     // run over and over again
{

sensorValue = analogRead(sensorPin);     // read the value from the sensor
Serial.println(sensorValue);     // send that value to the computer
delay(100);     // delay for 1/10 of a second

if (sensorValue > 20){
digitalWrite(ledPin, LOW);
}
else {
digitalWrite(ledPin, HIGH);
scale();     // call the scale() function
delay(1000);     // delay for 1 second

}
}
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)     // the sound producing function
{
int x;
long delayAmount = (long)(1000000/frequencyInHertz);
long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
for (x=0;x<loopTime;x++)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin,LOW);
delayMicroseconds(delayAmount);
}
}

void scale ()
{

beep(speakerPin,2093,500);     //C (from the chart linked to above)
beep(speakerPin,2349,250);     //D
beep(speakerPin,2637,500);     //E
beep(speakerPin,2093,250);       //C
beep(speakerPin,2637,500);     //E
beep(speakerPin,2093,400);       //C
beep(speakerPin,2637,1000);     //E

beep(speakerPin,2349,500);     //D
beep(speakerPin,2637,500);     //E
beep(speakerPin,2793,250);     //F
beep(speakerPin,2793,250);     //F
beep(speakerPin,2637,250);     //E
beep(speakerPin,2349,250);     //D
beep(speakerPin,2793,1000);     //F
}

[/c]

2 thoughts on “Dark-Be-Gone: a quick LilyPad hack

  1. looks great. Can you please share the code? I want to use it for a little project I’m doing with my kid. thanks!

    Like

Comments are closed.