Robot Art show
Copy of our code
//set the pins where the buttons, LEDs and buzzer connect
int button[] = {2, 4, 6, 8}; //red is button[0], yellow is button[1], green is button[2], blue is button[3]
int led[] = {3, 5, 7, 9}; //red is led[0], yellow is led[1], green is led[2], blue is led[3]
int tones[] = {262, 330, 392, 494}; //tones to play with each button (c, e, g, b)
int buzzerPin = 10;
void setup() {
pinMode(button[0], INPUT_PULLUP);
pinMode(button[1], INPUT_PULLUP);
pinMode(button[2], INPUT_PULLUP);
pinMode(button[3], INPUT_PULLUP);
//set all of the LED pins to output
pinMode(led[0], OUTPUT);
pinMode(led[1], OUTPUT);
pinMode(led[2], OUTPUT);
pinMode(led[3], OUTPUT);
pinMode(buzzerPin, OUTPUT); //set the buzzer pin to output
}
void loop() {
if (digitalRead(button[0]) == LOW) { //if the first key is pressed
tone(buzzerPin, 2093); //play the frequency for c
digitalWrite(led[0], HIGH);
}
else if (digitalRead(button[1]) == LOW) { //if the second key is pressed
tone(buzzerPin, 2349); //play the frequency for d
digitalWrite(led[1], HIGH);
}
else if (digitalRead(button[2]) == LOW) { //if the third key is pressed
tone(buzzerPin, 2637); //play the frequency for e
digitalWrite(led[2], HIGH);
}
else if (digitalRead(button[3]) == LOW) { //if the third key is pressed
tone(buzzerPin, 3135); //play the frequency for f
digitalWrite(led[3], HIGH);
}
else {
noTone(buzzerPin);
digitalWrite(led[0], LOW);
digitalWrite(led[1], LOW);
digitalWrite(led[2], LOW);
digitalWrite(led[3], LOW);
}
}
For this project, we made a 4 note piano. We started using the code for the digital trumpet, and then modified it to play the notes in mary had a little lamb as well as add a fourth note to the piano. We also modified it to flash a different color light depending on what note or chord you are playing.