I'm trying to create a program that will allow me control a piezoelectric speaker through pushbuttons. I am equipped with two pushbuttons yet I am required to create switches for three separate series of tones. I was able to successfully code a program that plays a tone when one pushbutton is pressed and another tone when the other pushbutton is pressed, but I am unable to have a third tone play when both are pressed. I considered the fact that the wiring was done incorrectly, but this theory was disproved when I tested the voltages of both buttons being pressed simultaneously using a digital oscilloscope. If both voltages are outputting HIGH states, then why is my code not functioning properly? (code is attached)
I'm currently working on designing a program that controls a piezoelectric speaker through the use of pushbuttons. I have 3 separate series of tones that need to be emitted using only two push buttons. The speaker responds when one pushbutton is pushed individually and the other is not, but when both buttons are pushed there is no response. I checked my wiring using a digital oscilloscope and both inputs read as HIGH when simultaneously pressed. I also tried using a logical and operator yet yielded the same results. Can anyone show me how to adjust my code without using a switch statement?
int AlarmSwitch = 4;
int RobotSwitch = 3;
int i, freq, duration;
void setup()
{
{
pinMode(AlarmSwitch, INPUT);
pinMode(RobotSwitch, INPUT);
Serial.begin(9600);
}
}
void loop()
{
if (digitalRead(AlarmSwitch) == HIGH)
{
if (digitalRead(RobotSwitch) == LOW)
{
Serial.println("Alarm...");
for(i=0; i<4; i++)
{
tone(11, 1500, 500);
delay(500);
noTone(11);
delay(500);
}
}
}
else if (digitalRead(RobotSwitch) == HIGH)
{
if (digitalRead(AlarmSwitch) == LOW)
{
Serial.println("Robot...");
tone(11, 2800, 100);
delay(100);
tone(11, 2400, 200);
delay(200);
tone(11, 4200, 140);
delay(140);
tone(11, 2000, 30);
delay(30);
}
}
else if (digitalRead(AlarmSwitch) == HIGH)
{
if (digitalRead(RobotSwitch) == HIGH)
{
Serial.println("Hyperspace...");
for(duration = 15; duration > 0; duration--)
{
for(freq = 2000; freq <= 2500; freq = freq + 20)
{
tone(11, freq, duration);
delay(duration);
}
}
}
}
else
{
Serial.println("Use a switch to play a tone");
}
}
Does LOW mean that the switch is pressed? Or does it mean that the switch is released?
If LOW means pressed, you should have code like:
int robotState = digitalRead(RobotSwitch);
int alarmState = digitalRead(AlarmSwitch);
if(robotState == LOW && alarmState == LOW)
{
// both switches are pressed
}
else if(robotState == LOW)
{
// only the robot switch is pressed
}
else if(alarmState == LOW)
{
// only the alarm switch is pressed
}
dlloyd:
For this to work, you will need a pulldown resistor (i.e. 10K) installed from pin 3 to GND and another resistor installed from pin 4 to GND.
I realize this and already have such resistors in place. Like I said, the wiring is undoubtedly done correctly, yet the program still fails to function properly.