Arduino Night Lights + Melody


#include “pitches.h”
void setup() {
// initialize serial communications (for debugging only):
Serial.begin(9600);
pinMode(3, OUTPUT);

int speakerOut = 9;

int DEBUG = 1;
pinMode(speakerOut, OUTPUT);
if (DEBUG) {
Serial.begin(9600); //
}
}
int melody[] = { NOTE_C7, NOTE_GS7,NOTE_C7,NOTE_GS6, NOTE_F7,NOTE_G4, NOTE_GS6, NOTE_GS7, NOTE_F7,NOTE_GS6, NOTE_GS7, NOTE_F7, NOTE_GS7 };
int beats[] = { 16, 16, 16, 8, 8, 16, 32, 16, 16, 16, 8, 8 };
int MAX_COUNT = sizeof(melody) / 6;
long tempo = 20000;

int pause = 1000;

int rest_count = 100;

// Initialize core variables
int tone_ = 0;
int beat = 0;
long duration = 0;

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(9 ,HIGH);
delayMicroseconds(tone_ / 2);

// DOWN
digitalWrite(9 , 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);
}
}
}

void loop() {
// read the sensor:
int sensorValue = analogRead(A0);
Serial.println(sensorValue);

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);

if (sensorValue < 800) { // To change the point at which the light turns on change this value.
digitalWrite(3, LOW);

}
else {
digitalWrite(3,HIGH);
}
}

 
if (sensorValue < 900) {
digitalWrite(3, LOW);

}
else {
digitalWrite(3,HIGH);

}

}

 

 

One thought on “Arduino Night Lights + Melody

Leave a Reply

Your email address will not be published. Required fields are marked *