THE SETUP
THE CODE
unsigned long startTime;
boolean start =false;
unsigned long elapsedTime;
boolean didntWash = false;
boolean flash = true;
int speakerPin = 9; //Set speaker to pin 9
int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
void setup() {
pinMode(4, OUTPUT);
pinMode(speakerPin, OUTPUT);
pinMode(13, OUTPUT);
}
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, 1000);
delayMicroseconds(tone);
digitalWrite(speakerPin, 0);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void loop() {
//digitalWrite(4,HIGH);
if(didntWash){
if(millis() - startTime < 5000){
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
if(flash){
digitalWrite(13, HIGH);
flash = !flash;
} else {
digitalWrite(13, LOW);
flash = !flash;
}
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
} else {
digitalWrite(13, LOW);
elapsedTime = 0;
didntWash = false;
start = false;
}
}
if(!start && !didntWash){
if(analogRead(0) < 300){
digitalWrite(13, HIGH);
startTime = millis();
start = true;
}
} else if (start && !didntWash) {
elapsedTime = millis() - startTime;
if(elapsedTime < 3000){
if(analogRead(1) < 300){
didntWash = true;
startTime = millis();
}
} else {
start = false;
elapsedTime = 0;
digitalWrite(13, LOW);
}
}
}
No comments:
Post a Comment