This is a simple alarm clock when the day light is strong enough to trigger the Arduino to play Mario theme song.
Below is my code:
#include “pitches.h”
int speaker = 11;
int sensor = A0;
int sensorValue = 0;
int melody[] = {
NOTE_E7, NOTE_E7,0,NOTE_E7,0, NOTE_C7, NOTE_E7,0,NOTE_G7,0,0,0,NOTE_G6,0,0,0,
NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0,NOTE_C7, NOTE_D7, NOTE_B6, 0, 0,
NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0,NOTE_C7, NOTE_D7, NOTE_B6, 0, 0
};
int noteDurations[] = {
12, 12, 12, 12, 12, 12, 12, 12,12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12,12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
9, 9, 9,12, 12, 12, 12,12, 12, 12, 12,12, 12, 12, 12,
12, 12, 12, 12,12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
9, 9, 9,12, 12, 12, 12,12, 12, 12, 12,12, 12, 12, 12,
};
//notes in the melody:
void setup() {
Serial.begin(9600);
pinMode(sensor, INPUT);
pinMode(speaker, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensor);
Serial.println(sensorValue);
// sensorValue = map(sensorValue, 1, 15, 100, 2000);
// tone(speaker, sensorValue);
// Serial.println(speaker);
Serial.println(sensorValue);
if (sensorValue > 1000) {
for (int thisNote = 0; thisNote < 78; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note’s duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
}
}
delay(1000/4);
noTone(speaker);
}