/* -------------------- Tones in an Array----------------------- * Hardware Setup- * -Wire a piezo disk between digital pin 6 and Gnd * . -Wire an LED thru 330 ohm resistor from digital pin 7 to Gnd *------------------------------------------------------------*/ int piezoPin = 6; // piezo pin number int ledPin = 7; // LED pin number int swPin = 5; // variable for switch pin number boolean swState; // variable for "state" of the switch int half = 400; int quarter = 200; int c4 = 261; int d4 = 294; int e4 = 330; int f4 = 349; int g4 = 392; int a4 = 440; int b4 = 440; int c5 = 525; float brightness; void setup() { pinMode(swPin, INPUT_PULLUP); pinMode(piezoPin, OUTPUT); // Set the digital piezo pin as an output pinMode(ledPin, OUTPUT); // Set the digital LED pin as an output } void loop() { swState = digitalRead(swPin); if(swState == LOW){ digitalWrite(ledPin, HIGH); playSong(); digitalWrite(ledPin, LOW); } else { digitalWrite(ledPin, LOW); } delay(700); } void playSong() { playNote(c4, half, 70); playNote(c4, half, 70); playNote(c4, quarter, 100); playNote(d4, quarter, 70); playNote(e4, quarter, 70); } void playNote(int note, int duration, int rest) { tone(piezoPin, note, duration); // syntax: tone(pin, freq, duration) int dur = duration + rest; delay(dur); }