# Switch Arduino Alarm Clock

1,137

6

1

## Introduction: Switch Arduino Alarm Clock

This alarm clock works with the following items;

• Arduino Uno
• 2 LEDs
• 1 buzzer
• jumper wires
• One button, from a LED flashlight
• Power source (I used my laptop, but you can also connect a battery)

This alarm clock turns on and off based on when the button opens or closes the circuit. When the circuit is closed, both LEDs alternate flashing while the buzzer goes off. This does not stop until the button is clicked to turn the circuit off.

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Set Up

Set up your arduino Circuit like the one above, though with one less LED, and the button pictured is the one from an Arduino code, I used a button from an LED flashlight.

The Arduino code will cause the lights to flash as the buzzer goes off, when the button is pushed on or off. The button acts as a switch in my setup, something the Arduino button cannot function as, which is why I've substituted in the button from a flahslight

## Step 2: Code

// TONES ==========================================

// Start by defining the relationship between // note, period, & frequency. #define c 3830 // 261 Hz #define d 3400 // 294 Hz #define e 3038 // 329 Hz #define f 2864 // 349 Hz #define g 2550 // 392 Hz #define a 2272 // 440 Hz #define b 2028 // 493 Hz #define C 1912 // 523 Hz // Define a special note, 'R', to represent a rest #define R 0 // SETUP ============================================ // Set up speaker on a PWM pin (digital 9, 10 or 11) int speakerOut = 9; int speakerOnTime=250; //Declare redOnTime an int, and set to 250 mseconds int speakerOffTime=0; //Declare redOffTime an int, and set to 250 // Do we want debugging on serial out? 1 for yes, 0 for no int DEBUG = 1; int redled=10; //Declare redLEDPin an int, and set to pin 20 int redledOnTime=250; //Declare redOnTime an int, and set to 250 mseconds int redledOffTime=0; //Declare redOffTime an int, and set to 250 int yellowled=11; //Declare redLEDPin an int, and set to pin 20 int yellowledOnTime=250; //Declare redOnTime an int, and set to 250 mseconds int yellowledOffTime=0; //Declare redOffTime an int, and set to 250 int button = 2; int statePin = LOW; void setup() { pinMode(redled, OUTPUT); // Tell Arduino that redLEDPin is an output pin pinMode(yellowled, OUTPUT); // Tell Arduino that redLEDPin is an output pin pinMode (button, OUTPUT); pinMode(speakerOut, OUTPUT); if (DEBUG) { Serial.begin(9600); // Set serial out if we want debugging } } // MELODY and TIMING ======================================= // melody[] is an array of notes, accompanied by beats[], // which sets each note's relative length (higher #, longer note) int melody[] = { c, g, a, g, f, e, d, c, g, f, e, d, g }; int beats[] = { 8, 8, 8, 8, 16, 8, 8, 8, 16, 16, 8, 8 }; int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping. // Set overall tempo long tempo = 10000; // Set length of pause between notes int pause = 1000; // Loop variable to increase Rest length int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES // Initialize core variables int tone_ = 0; int beat = 0; long duration = 0; // PLAY TONE ============================================== // Pulse the speaker to play a tone for a particular duration void playTone() { long elapsed_time = 0; if (digitalRead(button)==HIGH){ if (tone_ > 0) { // if this isn't a Rest beat, while the tone has // played less long than 'duration', pulse speaker HIGH and LOW while (elapsed_time < duration) { digitalWrite(speakerOut,HIGH); delayMicroseconds(tone_ / 2); // DOWN digitalWrite(speakerOut, LOW); delayMicroseconds(tone_ / 2); // Keep track of how long we pulsed elapsed_time += (tone_); } } else { // Rest beat; loop times delay for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count delayMicroseconds(duration); } } } } // LET THE WILD RUMPUS BEGIN ============================= void loop() { // Set up a counter to pull from melody[] and beats[] for (int i=0; i playTone(); // A pause between notes... delayMicroseconds(pause); analogWrite(speakerOut,500); digitalWrite(redled,HIGH); //Turn red LED on delay(redledOnTime); //Leave on for redOnTime digitalWrite(redled,LOW); //Turn red LED off delay(redledOffTime); //Leave off for redOffTime digitalWrite(yellowled,HIGH); //Turn red LED on delay(yellowledOnTime); //Leave on for redOnTime digitalWrite(yellowled,LOW); //Turn red LED off delay(yellowledOffTime); digitalWrite(speakerOut,HIGH); delayMicroseconds(tone_ / 2); delay(speakerOnTime); //Leave on for redOnTime digitalWrite(speakerOut, LOW); delayMicroseconds(tone_ / 2); delay(speakerOffTime); //Leave off for redOffTime digitalWrite(redled,HIGH); //Turn red LED on delay(redledOnTime); //Leave on for redOnTime digitalWrite(redled,LOW); //Turn red LED off delay(redledOffTime); //Leave off for redOffTime digitalWrite(yellowled,HIGH); //Turn red LED on delay(yellowledOnTime); //Leave on for redOnTime digitalWrite(yellowled,LOW); //Turn red LED off delay(yellowledOffTime); //Leave off for redOffTime } } /* * NOTES * The program purports to hold a tone for 'duration' microseconds. * Lies lies lies! It holds for at least 'duration' microseconds, _plus_ * any overhead created by incremeting elapsed_time (could be in excess of * 3K microseconds) _plus_ overhead of looping and two digitalWrites() * * As a result, a tone of 'duration' plays much more slowly than a rest * of 'duration.' rest_count creates a loop variable to bring 'rest' beats * in line with 'tone' beats of the same length. * * rest_count will be affected by chip architecture and speed, as well as * overhead from any program mods. Past behavior is no guarantee of future * performance. Your mileage may vary. Light fuse and get away. * * This could use a number of enhancements: * ADD code to let the programmer specify how many times the melody should * loop before stopping * ADD another octave * MOVE tempo, pause, and rest_count to #define statements * RE-WRITE to include volume, using analogWrite, as with the second program at *http://www.arduino.cc/en/Tutorial/PlayMelody * ADD code to make the tempo settable by pot or other input device * ADD code to take tempo or volume settable by serial communication * (Requires 0005 or higher.) * ADD code to create a tone offset (higer or lower) through pot etc * REPLACE random melody with opening bars to 'Smoke on the Water' */

## Recommendations

8 2.2K
101 21K
34 10K
Epilog Laser Class

113 Enrolled

## Discussions

Great idea. Thanks for posting your project!