I posted a before and took down the code because it was pretty embarrassing but I'm sure this is still just as much. I'm really new to programming and I can't figure out what is going on here. I've been tearing through examples and I don't see much in the way of what is happening.
const int buttonPin1 = 8; // Button 1
const int speakerPin = 9; // Speaker
const int buttonPin2 = 10; // Button 2
const int buttonPin3 = 12; // Button 3
const int buttonPin4 = 2; // Button 4
const int buttonPin5 = 4; // Button 5
const int ledPin = 13; // led pin
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
// functions
void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
}
void buttons()
{
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 == LOW ) {
tone(speakerPin, 75,1);
}
else {
noTone(speakerPin);
}
buttonState2 = digitalRead(buttonPin2);
if (buttonState2 == LOW ) {
tone(speakerPin, 100,1);
}
else {
noTone(speakerPin);
}
buttonState3 = digitalRead(buttonPin3);
if (buttonState3 == LOW ) {
tone(speakerPin, 200,1);
}
else {
noTone(speakerPin);
}
buttonState4 = digitalRead(buttonPin4);
if (buttonState4 == LOW) {
tone(speakerPin, 300,1);
}
else {
noTone(speakerPin);
}
buttonState5 = digitalRead(buttonPin5);
if (buttonState5 == LOW) {
tone(speakerPin, 400,1);
}
else {
noTone(speakerPin);
}
}
void loop() {
buttons();
}
When I upload this I get a beep from the speaker and then it stays quiet. It does not respond to button presses other than the reset which just causes the initial beep. Am I just missing the structure of how this is supposed to be or am I way off?
Cut your code and hardware down to the bare minimum that exhibits the problem.
Check your wiring
If you need pull-up resisitors, make sure they're enabled.
Check your wiring. (yes, I know I've already said that, but it's important.) - it is easier to spot faults when there is less clutter.
Add some serial debug prints.
const int buttonPin1 = 8; // Button 1
const int speakerPin = 9; // Speaker
void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(buttonPin1, INPUT);
// Do you need pull-up enabled here?
}
void buttons()
{
int buttonState1 = digitalRead(buttonPin1);
if (buttonState1 == LOW ) {
tone(speakerPin, 75,1);
}
else {
noTone(speakerPin); // Maybe think about an obviously different tone. Does this turn off a tone already playing....?
}
}
void loop() {
buttons();
}
... should be used in the test programs too. But, at first, the "tone" subroutine should be written to just turn an LED on, and noTone will turn it off. Once the "backbone" of your program (all the if... else... stuff) is working, and having done the "making tones" research, it "should" (ha!) be easy to just go into your "light the LEDs" program, tweak "tone" and "noTone", and get what you wanted.
I Think the problem comes from the else branch of your if.. then .. else ...
For each non met condition you have a noTone(). So if you push a button you send a tone but your program continues with the following if in which the condition is not met and where you have the noTone that switches the sound off.
You'd better use a structure like the following. With the noTone as the default_statement
if (condition)
statement
else if (condition)
statement
else if (condition)
statement
else
default_statement