Button Push

Hey guys! I'm completely to the arduino uno and I just have a quick question regarding button switches. I'm sure this has been answered before, but I want to share my own code so I know exactly where to put what's in question...

I have the Mario Theme Song playing when my arduino is plugged in with no problems, but my code keeps on looping forever and ever. What I'd like to have happen is everytime I push a button, the code runs though once, and then stops, waiting for the next button press. Here's my code:

int speakerPin = 9;

int length = 400; // the number of notes
char notes[] = "EE E QE T t Q t e y u &y tQ TY RT E QWj Q t e y u &y tQ TY RT E QWj T%R# E 6yQ yQW T%R# E I II T%R# E 6yQ yQW # W Q T%R# E 6yQ yQW T%R# E I II T%R# E 6yQ yQW # W Q QQ Q QW EQ yt QQ Q QWE QQ Q QW EQ yt EE E QE T t Q t e y u &y tQ TY RT E QWj Q t e y u &y tQ TY RT E QWj EQ t 6 yR Ry uY YYT REQ yt EQ t 6 yR Ry yR RRE WQ ";
int beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,};
int tempo = 100;

void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}

void playNote(char note, int duration) {
char names[] = { 'Q', 'E', 'T', 't', 'e', 'y', 'u', '&', 'Y', 'R', 'W', 'j', '^', '%', '#', '6', 'I'};
int tones[] = { 955.56617, 758.43762, 637.76324, 1276, 1516.8522, 1136.3636, 1012.3917, 1072.5931,
568.18112, 715.86061, 851.31017, 1012.3917, 601.96723, 675.68481, 803.53556, 1203.949,
477.78309,};

// play the tone corresponding to the note name
for (int i = 0; i < 17; i++) {
if (names == note) {
playTone(tones, duration);
}
}
}

void setup() {
pinMode(speakerPin, OUTPUT);
}

void loop() {
for (int i = 0; i < length; i++) {
if (notes == ' ') {
delay(beats * tempo); // rest
} else {
playNote(notes, beats * tempo);
}

// pause between notes
delay(tempo / 2);
}
}

I'm sorry if this is the completely wrong way/place to post this. I'm completely new to this :slight_smile:

You know that loop() is called over and over, right?

If you want the code in loop() to be executed only when the switch BECOMES pressed, look at the state change detection example.

Ok I think I understand. So the button push code should be in loop()? Where do I put everything thats currently in loop()?

So the button push code should be in loop()?

Yes.

Where do I put everything thats currently in loop()?

Inside the "if switch is pressed" statement's body inside the "if switch state has changed" statement's body.

PaulS:
Inside the "if switch is pressed" statement's body inside the "if switch state has changed" statement's body.

I'm having trouble finding where this is.

This is the code I'm working with now:

/*
State change detection (edge detection)

Often, you don't need to know the state of a digital input all the time,
but you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON. This is called
state change detection, or edge detection.

This example shows how to detect when a button or button changes from off to on
and on to off.

The circuit:

  • pushbutton attached to pin 2 from +5V
  • 10K resistor attached to pin 2 from ground
  • LED attached from pin 13 to ground (or use the built-in LED on
    most Arduino boards)

created 27 Sep 2005
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

*/

// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int speakerPin = 13; // the pin that the LED is attached to

int length = 400; // the number of notes
char notes[] = "EE E QE T t Q t e y u &y tQ TY RT E QWj Q t e y u &y tQ TY RT E QWj T%R# E 6yQ yQW T%R# E I II T%R# E 6yQ yQW # W Q T%R# E 6yQ yQW T%R# E I II T%R# E 6yQ yQW # W Q QQ Q QW EQ yt QQ Q QWE QQ Q QW EQ yt EE E QE T t Q t e y u &y tQ TY RT E QWj Q t e y u &y tQ TY RT E QWj EQ t 6 yR Ry uY YYT REQ yt EQ t 6 yR Ry yR RRE WQ ";
int beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1,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1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,};
int tempo = 100;

void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}

void playNote(char note, int duration) {
char names[] = { 'Q', 'E', 'T', 't', 'e', 'y', 'u', '&', 'Y', 'R', 'W', 'j', '^', '%', '#', '6', 'I'};
int tones[] = { 955.56617, 758.43762, 637.76324, 1276, 1516.8522, 1136.3636, 1012.3917, 1072.5931,
568.18112, 715.86061, 851.31017, 1012.3917, 601.96723, 675.68481, 803.53556, 1203.949,
477.78309,};

// play the tone corresponding to the note name
for (int i = 0; i < 17; i++) {
if (names == note) {
_ playTone(tones*, duration);_
_
}_
_
}_
_
}_
_
// Variables will change:_
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
_
// initialize the button pin as a input:_
_
pinMode(buttonPin, INPUT);_
_
// initialize the LED as an output:_
_
pinMode(speakerPin, OUTPUT);_
_
// initialize serial communication:_
_
Serial.begin(9600);_
_
}_
void loop() {
_
// read the pushbutton input pin:_
_
buttonState = digitalRead(buttonPin);_
_
// compare the buttonState to its previous state*_
* if (buttonState != lastButtonState) {*
* // if the state has changed, increment the counter*
* if (buttonState == HIGH) {*
* // if the current state is HIGH then the button*
* // wend from off to on:*
* buttonPushCounter++;*
* Serial.println("on");*
* Serial.print("number of button pushes: ");*
* Serial.println(buttonPushCounter);*
* } else {*
* // if the current state is LOW then the button*
* // wend from on to off:*
* Serial.println("off");*
* }*
* // Delay a little bit to avoid bouncing*
* delay(50);*
* }*
* // save the current state as the last state,*
* //for next time through the loop*
* lastButtonState = buttonState;*
* // turns on the LED every four button pushes by*
* // checking the modulo of the button push counter.*
* // the modulo function gives you the remainder of*
* // the division of two numbers:*
* if (buttonPushCounter % 4 == 0) {*
* digitalWrite(speakerPin, HIGH);*
* } else {*
* digitalWrite(speakerPin, LOW);*
* }*
}

emgen277:
I'm having trouble finding where this is.

 // compare the buttonState to its previous state
 if (buttonState != lastButtonState) {

That is the "if switch state has changed" statement. The body is what follows the { that you have in the wrong place. Put it on a line BY ITSELF.

   // if the state has changed, increment the counter
   if (buttonState == HIGH) {

That is the "if switch is pressed" statement. The body is what follows the { that you have in the wrong place. Put it on a line BY ITSELF.

Does the body go into each statement?

emgen277:
Does the body go into each statement?

At this point you should just go look at some of the examples and see how an if statement should look. There comes a point where you are asking us things so basic and fundamental that it starts to look like you're not trying and that tends to cause the help to dry up. Go look at a few examples with if statements in them. If you still don't understand then come back and ask some questions, but at least look like you've made the effort to look.

You should also go read the "How to Use This Forum" post that you obviously thought you would just skip over and post anyway. It explains how to properly post code so it looks nice and doesn't get mangled by the forum software. It also has a lot of other things you should really know too.