Adding button to long program

Hi all, I’m a bit of a noob to this stuff, just getting my feet wet, and I want to take the Star Wars Imperial March piezo buzzer and LED program that was originally created by NickSort and I want to add a push button to allow it to play the whole program once thru, and then stop, until the button is pressed again. I’ve tried a few variants and each way I try it, the software errors out at the line from the original which states “‘firstSection’ was not defined in this scope” I will try to add the entire code as I have it currently written. If anyone can point me in the direction of what I did wrong, and how to fix it, I would be much obliged.

sketch_jan07e.ino (3.5 KB)

"'firstSection' was not defined in this scope"

You can't create a function from inside another function.

Read "How to use this forum" to learn how to use code tags to post your code.

(deleted)

First off, thank you very much spycatcher for your efforts, however, it still faults out at FirstSection();
I wonder if I could eliminate the problem by copying and duplicating the individual sections of the program into the area where the section starts, sure it would make the code 3x as long, but it would cut out the portion that is currently causing the error. I'm going to try it right now and see what happens.

Hmmm... getting a new error. Here is what I tried to do this time. It now gets to the squiggly bracket after voidbeep line and error says: a function-definition is not allowed here before (squigglybracket) token.

const int buttonPin = 2;
const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;

const int buzzerPin = 8;
const int ledPin1 = 12;
const int ledPin2 = 13;


int counter = 0;
int currState = HIGH;
int prevState = HIGH;


void setup() {
  // put your setup code here, to run once:
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buzzerPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}



void loop()
{
  while(!digitalRead(buttonPin))
  {}

  // Switch is now pressed
  //Play first section
  void beep(int note, int duration)
 {
  beep(a, 500);
  beep(a, 500);
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);

  delay(500);

  beep(eH, 500);
  beep(eH, 500);
  beep(eH, 500);
  beep(fH, 350);
  beep(cH, 150);
  beep(gS, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);

  delay(500);
  
  //Play second section
   beep(aH, 500);
  beep(a, 300);
  beep(a, 150);
  beep(aH, 500);
  beep(gSH, 325);
  beep(gH, 175);
  beep(fSH, 125);
  beep(fH, 125);
  beep(fSH, 250);

  delay(325);

  beep(aS, 250);
  beep(dSH, 500);
  beep(dH, 325);
  beep(cSH, 175);
  beep(cH, 125);
  beep(b, 125);
  beep(cH, 250);

  delay(700);

  //Variant 1
  beep(f, 250);
  beep(gS, 500);
  beep(f, 350);
  beep(a, 125);
  beep(cH, 500);
  beep(a, 375);
  beep(cH, 125);
  beep(eH, 650);

  delay(500);

  //Repeat second section
   beep(aH, 500);
  beep(a, 300);
  beep(a, 150);
  beep(aH, 500);
  beep(gSH, 325);
  beep(gH, 175);
  beep(fSH, 125);
  beep(fH, 125);
  beep(fSH, 250);

  delay(325);

  beep(aS, 250);
  beep(dSH, 500);
  beep(dH, 325);
  beep(cSH, 175);
  beep(cH, 125);
  beep(b, 125);
  beep(cH, 250);

  delay(700);

  //Variant 2
  beep(f, 250);
  beep(gS, 500);
  beep(f, 375);
  beep(cH, 125);
  beep(a, 500);
  beep(f, 375);
  beep(cH, 125);
  beep(a, 650);

  delay(650);
}
}

{
  //Play tone on buzzerPin
  tone(buzzerPin, note, duration);

  //Play different LED depending on value of 'counter'
  if (counter % 2 == 0)
  {
    digitalWrite(ledPin1, HIGH);
    delay(duration);
    digitalWrite(ledPin1, LOW);
  } else
  {
    digitalWrite(ledPin2, HIGH);
    delay(duration);
    digitalWrite(ledPin2, LOW);
  }

  //Stop tone on buzzerPin
  noTone(buzzerPin);

  delay(50);

  //Increment counter
  counter++;
}

void firstSection()
{
  beep(a, 500);
  beep(a, 500);
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);

  delay(500);

  beep(eH, 500);
  beep(eH, 500);
  beep(eH, 500);
  beep(fH, 350);
  beep(cH, 150);
  beep(gS, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);

  delay(500);
}

void secondSection()
{
  beep(aH, 500);
  beep(a, 300);
  beep(a, 150);
  beep(aH, 500);
  beep(gSH, 325);
  beep(gH, 175);
  beep(fSH, 125);
  beep(fH, 125);
  beep(fSH, 250);

  delay(325);

  beep(aS, 250);
  beep(dSH, 500);
  beep(dH, 325);
  beep(cSH, 175);
  beep(cH, 125);
  beep(b, 125);
  beep(cH, 250);

  delay(700);
}

That error means you're trying to define a function within a function, which is not allowed.

Try this:

const int buttonPin = 2;
const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;

const int buzzerPin = 8;
const int ledPin1 = 12;
const int ledPin2 = 13;


int counter = 0;
int currState = HIGH;
int prevState = HIGH;


void setup() {
 // put your setup code here, to run once:
 pinMode(buttonPin, INPUT_PULLUP);
 pinMode(buzzerPin, OUTPUT);
 pinMode(ledPin1, OUTPUT);
 pinMode(ledPin2, OUTPUT);
}



void loop() {
 while (!digitalRead(buttonPin)) {}

 // Switch is now pressed
 //Play first section
  beep(a, 500);
  beep(a, 500);
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);

  delay(500);

  beep(eH, 500);
  beep(eH, 500);
  beep(eH, 500);
  beep(fH, 350);
  beep(cH, 150);
  beep(gS, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);

  delay(500);

  //Play second section
  beep(aH, 500);
  beep(a, 300);
  beep(a, 150);
  beep(aH, 500);
  beep(gSH, 325);
  beep(gH, 175);
  beep(fSH, 125);
  beep(fH, 125);
  beep(fSH, 250);

  delay(325);

  beep(aS, 250);
  beep(dSH, 500);
  beep(dH, 325);
  beep(cSH, 175);
  beep(cH, 125);
  beep(b, 125);
  beep(cH, 250);

  delay(700);

  //Variant 1
  beep(f, 250);
  beep(gS, 500);
  beep(f, 350);
  beep(a, 125);
  beep(cH, 500);
  beep(a, 375);
  beep(cH, 125);
  beep(eH, 650);

  delay(500);

  //Repeat second section
  beep(aH, 500);
  beep(a, 300);
  beep(a, 150);
  beep(aH, 500);
  beep(gSH, 325);
  beep(gH, 175);
  beep(fSH, 125);
  beep(fH, 125);
  beep(fSH, 250);

  delay(325);

  beep(aS, 250);
  beep(dSH, 500);
  beep(dH, 325);
  beep(cSH, 175);
  beep(cH, 125);
  beep(b, 125);
  beep(cH, 250);

  delay(700);

  //Variant 2
  beep(f, 250);
  beep(gS, 500);
  beep(f, 375);
  beep(cH, 125);
  beep(a, 500);
  beep(f, 375);
  beep(cH, 125);
  beep(a, 650);

  delay(650);
}

void beep(int note, int duration) {
 //Play tone on buzzerPin
 tone(buzzerPin, note, duration);

 //Play different LED depending on value of 'counter'
 if (counter % 2 == 0) {
  digitalWrite(ledPin1, HIGH);
  delay(duration);
  digitalWrite(ledPin1, LOW);
 } else {
  digitalWrite(ledPin2, HIGH);
  delay(duration);
  digitalWrite(ledPin2, LOW);
 }

 //Stop tone on buzzerPin
 noTone(buzzerPin);

 delay(50);

 //Increment counter
 counter++;
}

void firstSection() {
 beep(a, 500);
 beep(a, 500);
 beep(a, 500);
 beep(f, 350);
 beep(cH, 150);
 beep(a, 500);
 beep(f, 350);
 beep(cH, 150);
 beep(a, 650);

 delay(500);

 beep(eH, 500);
 beep(eH, 500);
 beep(eH, 500);
 beep(fH, 350);
 beep(cH, 150);
 beep(gS, 500);
 beep(f, 350);
 beep(cH, 150);
 beep(a, 650);

 delay(500);
}

void secondSection() {
 beep(aH, 500);
 beep(a, 300);
 beep(a, 150);
 beep(aH, 500);
 beep(gSH, 325);
 beep(gH, 175);
 beep(fSH, 125);
 beep(fH, 125);
 beep(fSH, 250);

 delay(325);

 beep(aS, 250);
 beep(dSH, 500);
 beep(dH, 325);
 beep(cSH, 175);
 beep(cH, 125);
 beep(b, 125);
 beep(cH, 250);

 delay(700);
}

I moved the void beep() stuff outside your loop and added it to where I assumed you needed it.

Topiek, thank you very much for your assistance! However, that one loads without issue but just plays continuous loop, with or without input from the button, as if the program was not changed from the original. Perhaps I need to add an else command at the end, followed by a delay of say, 1000?

Nope. Even though I changed the 'while' command to an 'if' command, adding an else command to the end of the program causes the error, 'else' without a previous 'if' I then attempted changing the 'while' to 'if' without the 'else' at the end of the program, and still get no change from original program, plays without any input from button.

(deleted)

Perhaps I will just place a maintained switch on the power supply, and a 15s (or more) delay after the full music loop (after the delay 650) I have experimented with adding and changing a delay in that position and it works perfectly, but placing anything else in that location (for example an ‘else’) then causes a different error that ‘beep’ was not defined in this scope. I would really prefer to find out how to do what I intended, as I’m sure there is a way to do it, and I’m likely not the only person who wishes to take an existing program and just add a push button to start the loop, then dwell until button is pressed again, but for my purposes, a long delay after the full song, and a maintained switch on the power in to the board would suffice, just is not ideal. For those who may be curious why, this will be uploaded to a nano, with a 5v alkaline aaa battery supply, and will be installed in my brother’s cosplay uniform of mandalorian armor, and I had hoped to place a button that would be accessible from outside the suit, to play the song once. The forearm of the suit has a compartment just large enough to house all the parts, except the LEDs which will be placed elsewhere on the suit, I’m thinking on the chest just above the left chest pocket area, like where a military dress uniform places medals. Attached is a photo of my brother, in full cosplay uniform, at the SLC comic-con right after he met Carrie Fisher, just months before she passed away.

Tried taking the ! out of the while command, that was actually the first thing I tried, even before changing to if/else, still no change. It still acts as if the button is not even required.

Untitled.png

(deleted)

Tried taking the ! out of the while command, that was actually the first thing I tried, even before changing to if/else, still no change. It still acts as if the button is not even required.

Can you please report the values of digitalRead(buttonPin) with the button pressed and not pressed.

With pinMode INPUT_PULLUP this line should absolutely hold the code at this point until the button is pressed.

while(digitalRead(buttonPin))
  {}

cattledog:
Can you please report the values of digitalRead(buttonPin) with the button pressed and not pressed.

With pinMode INPUT_PULLUP this line should absolutely hold the code at this point until the button is pressed.

while(digitalRead(buttonPin))

{}

How do I do that? Serial plotter?

Wait! I got it! It works now. I swapped out the switch and rechecked all my wiring, and rewrote if, then replaced with while, and it works now!
Heres the code, just in case I messed something up, I want to make sure I have multiple copies. Also, if anyone else wishes to duplicate this project, here you go! It now will allow to press the button, play the entire song once, then wait until the button is pressed again, exactly how I wanted it! Thank you to all of you for your assistance!

const int buttonPin = 2;
const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;

const int buzzerPin = 8;
const int ledPin1 = 12;
const int ledPin2 = 13;


int counter = 0;
int currState = HIGH;
int prevState = HIGH;


void setup() {
 // put your setup code here, to run once:
 pinMode(buttonPin, INPUT_PULLUP);
 pinMode(buzzerPin, OUTPUT);
 pinMode(ledPin1, OUTPUT);
 pinMode(ledPin2, OUTPUT);
}



void loop() {
 while (digitalRead(buttonPin)) {}

 // Switch is now pressed
 //Play first section
  beep(a, 500);
  beep(a, 500);
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);

  delay(500);

  beep(eH, 500);
  beep(eH, 500);
  beep(eH, 500);
  beep(fH, 350);
  beep(cH, 150);
  beep(gS, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);

  delay(500);

  //Play second section
  beep(aH, 500);
  beep(a, 300);
  beep(a, 150);
  beep(aH, 500);
  beep(gSH, 325);
  beep(gH, 175);
  beep(fSH, 125);
  beep(fH, 125);
  beep(fSH, 250);

  delay(325);

  beep(aS, 250);
  beep(dSH, 500);
  beep(dH, 325);
  beep(cSH, 175);
  beep(cH, 125);
  beep(b, 125);
  beep(cH, 250);

  delay(700);

  //Variant 1
  beep(f, 250);
  beep(gS, 500);
  beep(f, 350);
  beep(a, 125);
  beep(cH, 500);
  beep(a, 375);
  beep(cH, 125);
  beep(eH, 650);

  delay(500);

  //Repeat second section
  beep(aH, 500);
  beep(a, 300);
  beep(a, 150);
  beep(aH, 500);
  beep(gSH, 325);
  beep(gH, 175);
  beep(fSH, 125);
  beep(fH, 125);
  beep(fSH, 250);

  delay(325);

  beep(aS, 250);
  beep(dSH, 500);
  beep(dH, 325);
  beep(cSH, 175);
  beep(cH, 125);
  beep(b, 125);
  beep(cH, 250);

  delay(700);

  //Variant 2
  beep(f, 250);
  beep(gS, 500);
  beep(f, 375);
  beep(cH, 125);
  beep(a, 500);
  beep(f, 375);
  beep(cH, 125);
  beep(a, 650);

  delay(650);
}

void beep(int note, int duration) {
 //Play tone on buzzerPin
 tone(buzzerPin, note, duration);

 //Play different LED depending on value of 'counter'
 if (counter % 2 == 0) {
  digitalWrite(ledPin1, HIGH);
  delay(duration);
  digitalWrite(ledPin1, LOW);
 } else {
  digitalWrite(ledPin2, HIGH);
  delay(duration);
  digitalWrite(ledPin2, LOW);
 }

 //Stop tone on buzzerPin
 noTone(buzzerPin);

 delay(50);

 //Increment counter
 counter++;
}

void firstSection() {
 beep(a, 500);
 beep(a, 500);
 beep(a, 500);
 beep(f, 350);
 beep(cH, 150);
 beep(a, 500);
 beep(f, 350);
 beep(cH, 150);
 beep(a, 650);

 delay(500);

 beep(eH, 500);
 beep(eH, 500);
 beep(eH, 500);
 beep(fH, 350);
 beep(cH, 150);
 beep(gS, 500);
 beep(f, 350);
 beep(cH, 150);
 beep(a, 650);

 delay(500);
}

void secondSection() {
 beep(aH, 500);
 beep(a, 300);
 beep(a, 150);
 beep(aH, 500);
 beep(gSH, 325);
 beep(gH, 175);
 beep(fSH, 125);
 beep(fH, 125);
 beep(fSH, 250);

 delay(325);

 beep(aS, 250);
 beep(dSH, 500);
 beep(dH, 325);
 beep(cSH, 175);
 beep(cH, 125);
 beep(b, 125);
 beep(cH, 250);

 delay(700);
}

Wait! I got it! It works now. I swapped out the switch and rechecked all my wiring, and rewrote if, then replaced with while, and it works now!

Good job sorting this out. You are not the first person to struggle with code when there was an underlying hardware problem. It's happened to us all.

Say I wanted to replace the button switch with an IR sensor, like a proximity sensor, the small ones that have the ir emitter and receiver, would I just wire the ir sensor to vcc and gnd, and the vout pin in place of the button pin, pin2 in this case? Thanks for the help!

Say I wanted to replace the button switch with an IR sensor, like a proximity sensor, the small ones that have the ir emitter and receiver, would I just wire the ir sensor to vcc and gnd, and the vout pin in place of the button pin, pin2 in this case? Thanks for the help!

It depends on whether or not the proximity sensor puts out a high or low when the object is present.

You will need one of these lines of code depending on the output logic. I think that most of these sensor are LOW with not object present.

while (digitalRead(proxSensorPin)) {} //sensor is HIGH when no object present
while (!digitalRead(proxSensorPin)) {}//sensor is LOW when no object present

It's also possible that the sensor module will have an active HIGH/LOW output and not require the INPUT_PULLUP mode to prevent a floating pin and may work better with INPUT.

Just to try it I went ahead and threw in a 3 pin ir sensor, only had to add a vcc wire, and it works exactly like the button, using the OUT of the sensor to pin2, where the button previously grounded pin2. Works flawless. If I pull the vcc wire, it acts as if the ‘button’ is being held down constantly, but with vcc and gnd connected to the sensor, it works exactly as a sensor should, if nothing is reflecting the ir to the sensor’s input, the loop waits, if something comes within the sensor’s range (adjustable with tiny potentiometer on the sensor) it triggers the sensor and in turn, begins the loop.