starting button

My attempt with this code was to basically not start the rest of the code below buttonfunc(); in my main loop until I press a tackbutton thats on my breadboard. I think this should work but for some reason when I click the button it just sits there. It should start the rest of my code. I am just learning C so could someone please help me out hear?

int tresPin = 2;
int dosPin = 3;
int unoPin = 6;
////////////////////////
int buzzPin = 9;
  int length = 4; // the number of notes
  char notes[] = "b";
  int beats[] = {2};
  int tempo = 300;
///////////////////////  
int buttonPin = 5;
  int reading;
  //int previous = LOW; 
///////////////////////
int msPin = 5;

void setup()               
{
  // begin the serial communication
  Serial.begin(9600);
  
  pinMode(tresPin, OUTPUT);     
  pinMode(dosPin, OUTPUT); 
  pinMode(unoPin, OUTPUT); 
  pinMode(buzzPin, OUTPUT); 
  pinMode(buttonPin, INPUT);
  pinMode(msPin, INPUT);

}


void buttonfunc() {      

   int z;
   z=0;
   
   while(z==0) {
     if (reading == HIGH) { //&& previous == LOW
       Serial.println("Starting Systems...");
       z=1;
       break;
     }
     else {
       buttonfunc(); 
     }
  }
  
}

void loop() // continuous loop
{
  
  reading = digitalRead(buttonPin);
  buttonfunc();      
        
  Serial.println("3");
  digitalWrite(tresPin, HIGH);  
  delay(1500); 
  digitalWrite(tresPin, LOW);
  
  Serial.println("2");
  digitalWrite(dosPin, HIGH);  
  delay(1500); 
  digitalWrite(dosPin, LOW);
  
  Serial.println("1");
  digitalWrite(unoPin, HIGH);  
  delay(1500); 
  digitalWrite(unoPin, LOW);
  
  digitalWrite(tresPin, HIGH);  
  digitalWrite(dosPin, HIGH);  
  digitalWrite(unoPin, HIGH); 
  Serial.println("Go!"); 
  
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats[i] * tempo);
    }

    // pause between notes
    delay(tempo / 2); 
  } 
  
  digitalWrite(tresPin, LOW);
  digitalWrite(dosPin, LOW);
  digitalWrite(unoPin, LOW);

}

Bouzy,

The problem is that you calculate the value for "reading" only once before you enter "buttonfunc();". Buttonfunc then loops forever, waiting for reading to go HIGH -- which it never will -- and -- yikes! -- calling itself recursively! This is a crash waiting to happen (quickly!). Here's a better suggestion for buttonfunc():

void buttonfunc()
{
  while (digitalRead(buttonPin) == HIGH)
    ; // do nothing!
  Serial.println("Starting systems...");
}

Mikal

The problem is that you calculate the value for "reading" only once before you enter "buttonfunc();". Buttonfunc then loops forever, waiting for reading to go HIGH -- which it never will -- and -- yikes! -- calling itself recursively! This is a crash waiting to happen (quickly!). Here's a better suggestion for buttonfunc():

He's waiting for the pin to go HIGH, while your code waits for the pin to go LOW. So your function will exit immediately as the pin already is LOW. The loop must wait for the pin to go HIGH.

void buttonfunc()
{
  while (digitalRead(buttonPin) != HIGH); // do nothing!
  Serial.println("Starting systems...");
}

Oops! Quite right. Sorry. I shouldn't post so late at night. :slight_smile:

Mikal

while (digitalRead(buttonPin) != HIGH)__
;

skumlerud: good bug-spotting, but a style recommendation - put empty semicolons on the next line (like mikalhart) for better visibility.

It's all a matter of style but I prefer to use { } in place of a ; that could easy be missed or worse miss added.
So:-

while (digitalRead(buttonPin) != HIGH) { }

Grumpy, that is perfectly fine and valid. I like { ; } for entirely empty stub functions myself. Source code is for the humans, so it's good to get new programmers into the habit of making human-friendly source code.

skumlerud: good bug-spotting, but a style recommendation - put empty semicolons on the next line (like mikalhart) for better visibility.

I'm not a fan of this style. Semicolon terminates a statement and should be on the end of the statement - i.e. on the end of the line. If you want to put something on the next line for visibility I suggest an empty pair of brackets. That indicates an empty block, in this case it will make it clear that the while-loop doesn't do anything in the loop.

To make a digital Read pin go to high all I have to do is feed it volts?? Or do I need to make it go high with code? Right now I have 5v going to a tac switch and a analog in going to digital read pin 5. Will this code work for that?

You'll need to make a closed circuit between the arduino ground and the digital pin, and ensure that there is voltage present that is above a set threshold. (I do not know what that is)

At least, I think this is how a pin is set to HIGH.