Use blink software for Arduino 1. Write a program to read in all four pushbutton
ID: 3561832 • Letter: U
Question
Use blink software for Arduino
1. Write a program to read in all four pushbuttons on the EduShield. When any of the four buttons is pressed, output a tone as long as the button is pressed. When the button is released, stop the tone. Use a frequency of A4 (440Hz) for the tone. Note that the buttons are on Digital pins 4,7,8, and 12.
2. Write a program that allows you to play the four-note tune mentioned above. Note that the speaker on the EduShield is connected to pin 5. Use the four pushbuttons on the EduShield for this. Assign each note to a push button. The frequencies may be obtained from the header file in the tutorial.
Explanation / Answer
% 1st question
int pushButton1 = 4;
int pushButton2 = 7;
int pushButton3 = 8;
int pushButton4 = 12;
int output_tune=0xA4;
// the setup routine runs once when you press reset:
void setup() {
// make the pushbutton's pin an input:
pinMode(pushButton1, INPUT);
pinMode(pushButton2, INPUT);
pinMode(pushButton3, INPUT);
pinMode(pushButton4, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
while(digitalRead(pushButton1)||digitalRead(pushButton2)||digitalRead(pushButton3)||digitalRead(pushButton4))
{
analogWrite(5,output_tune);
}
analogWrite(5,0);
}
%2nd Question
int pushButton1 = 4;
int pushButton2 = 7;
int pushButton3 = 8;
int pushButton4 = 12;
int output_tune1=0xA4;
int output_tune2=0xA9;
int output_tune3=0xAE;
int output_tune4=0xB4;
// the setup routine runs once when you press reset:
void setup() {
// make the pushbutton's pin an input:
pinMode(pushButton1, INPUT);
pinMode(pushButton2, INPUT);
pinMode(pushButton3, INPUT);
pinMode(pushButton4, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
while(digitalRead(pushButton1))
{
analogWrite(5,output_tune1);
}
while(digitalRead(pushButton2))
{
analogWrite(5,output_tune2);
}
while(digitalRead(pushButton3))
{
analogWrite(5,output_tune3);
}
while(digitalRead(pushButton4))
{
analogWrite(5,output_tune4);
}
analogWrite(5,0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.