Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- LED Alarm Box
- Turns on LEDs in a pattern, then turns on an audible alarm.
- This example code is in the public domain.
- modified 5 Feb 2016
- by Dylan Boyd
- */
- // TONES ==========================================
- // Defining the tones used for the alarm
- #define C 1911
- #define C1 1804
- #define D 1703
- #define Eb 1607
- #define E 1517
- #define F 1432
- #define F1 1352
- #define G 1276
- #define Ab 1204
- #define A 1136
- #define Bb 1073
- #define B 1012
- #define c 955
- #define c1 902
- #define d 851
- #define eb 803
- #define e 758
- #define f 716
- #define f1 676
- #define g 638
- #define ab 602
- #define a 568
- #define bb 536
- #define b 506
- #define p 0
- #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 ============================================
- // Setting up the buzzer and photocell
- int speakerOut = 8;
- int photocellPin = 0; // the cell and 10K pulldown are connected to a0
- int photocellReading; // the analog reading from the analog resistor divider
- // the setup function runs once when you press reset or power the board
- void setup() {
- // initialize LED Pins, Speaker Pin, and starting the Serial.
- Serial.begin(9600);
- pinMode(5, OUTPUT);
- pinMode(6, OUTPUT);
- pinMode(7, OUTPUT);
- pinMode(speakerOut, OUTPUT);
- }
- // initalizing the beats of the song and the melody
- int melody[] = {e, e, e, c, e, g, G, c, G, E, A, B, Bb, A, G, e, g, a, f, g, e, c, d, B, c};
- int beats[] = {6, 12, 12, 6, 12, 24, 24, 18, 18, 18, 12, 12, 6, 12, 8, 8, 8, 12, 6, 12, 12, 6, 6, 6, 12};
- 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 for song
- 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 (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);
- }
- }
- }
- // the loop function runs over and over again forever
- void loop() {
- photocellReading = analogRead(photocellPin);
- Serial.print("Analog reading = ");
- Serial.print(photocellReading); // the raw analog reading
- // We'll have a few threshholds, qualitatively determined
- if (photocellReading < 10) {
- Serial.println(" - Dark");
- } else if (photocellReading < 200) {
- Serial.println(" - Dim");
- } else if (photocellReading < 500) {
- Serial.println(" - Light");
- } else if (photocellReading < 800) {
- Serial.println(" - Bright");
- } else {
- Serial.println(" - Very bright");
- }
- delay(1000);
- if(photocellReading > 500){ //if room isn't dark enough, delay
- delay(10000);
- }
- if (photocellReading < 600) // if room is dark, light LEDs in pattern
- { //LED PATTERN:
- digitalWrite(5, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(200); // wait for a second
- digitalWrite(5, LOW); // turn the LED off by making the voltage LOW
- delay(200); // wait for a second
- digitalWrite(6, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(200); // wait for a second
- digitalWrite(6, LOW); // turn the LED off by making the voltage LOW
- delay(200); // wait for a second
- digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(200); // wait for a second
- digitalWrite(7, LOW); // turn the LED off by making the voltage LOW
- delay(1000); // wait for a second
- digitalWrite(5, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(200); // wait for a second
- digitalWrite(5, LOW); // turn the LED off by making the voltage LOW
- delay(200); // wait for a second
- digitalWrite(6, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(200); // wait for a second
- digitalWrite(6, LOW); // turn the LED off by making the voltage LOW
- delay(200); // wait for a second
- digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(200); // wait for a second
- digitalWrite(7, LOW); // turn the LED off by making the voltage LOW
- delay(1000); // wait for a second
- digitalWrite(5, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(200); // wait for a second
- digitalWrite(5, LOW); // turn the LED off by making the voltage LOW
- delay(200); // wait for a second
- digitalWrite(6, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(200); // wait for a second
- digitalWrite(6, LOW); // turn the LED off by making the voltage LOW
- delay(200); // wait for a second
- digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(200); // wait for a second
- digitalWrite(7, LOW); // turn the LED off by making the voltage LOW
- delay(1000); // wait for a second
- // 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;
- }
- // LET THE WILD RUMPUS BEGIN SUPER MARIO! =============================
- // Set up a counter to pull from melody[] and beats[]
- for (int i=0; i<MAX_COUNT; i++) {
- tone_ = melody[i];
- beat = beats[i];
- duration = beat * tempo; // Set up timing
- playTone();
- // A pause between notes...
- delayMicroseconds(pause);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement