Loop not looping (I have searched I promise)

Hi,
I've written a simple sketch with and an If statement and two conditions based on whether a button is pressed. The problem is, nothing happens when I press the button - if the button is pressed when I start the sketch, it stays in this state no matter if I release the button, and similarly if the button is open, it will continue registering it as open even if I press the button. I've been on this for two days and feel I must be missing something very simple as I can't for the life of me work out why it's not looping correctly. Feels like some stupid syntax error, but I just can't see what it is. Any suggestions gratefully received - I feel like I've tried everything google has to offer! Code below - realise it's a but ugly, but I'm pretty new to all this. Thanks, Paul

#include "pitches.h"
#include <Servo.h>

int melody[] = {
NOTE_C5,NOTE_C5,NOTE_C5 };
int duration = 400; // 400 miliseconds

Servo myservo; // create servo object to control a servo

int pos = 0;
int wait = (3);
int servPosition = 10;
int servDelay = 1;
int ledPin = 2 ;
int pushButton = 13;
int buttonState = digitalRead(pushButton);

void setup() {
myservo.attach(10); // attaches the servo on pin 10 to the servo object
pinMode (1,OUTPUT);
pinMode (2,OUTPUT);
pinMode (3,OUTPUT);
pinMode (4,OUTPUT);
pinMode (5,OUTPUT);
pinMode (pushButton,INPUT);
Serial.begin(9600);
}

void loop () {

if (buttonState == HIGH)

{
myservo.write(servPosition);

for (int thisNote = 0; thisNote < 3; thisNote++) {
tone(8, melody[thisNote], duration);
digitalWrite (ledPin,HIGH);
delay(800);
ledPin++;}

tone (8,NOTE_C6, 800);
myservo.write(300);
digitalWrite (2,LOW);
digitalWrite (3,LOW);
digitalWrite (4,LOW);
digitalWrite (5,HIGH);
delay(5000);
digitalWrite (2,LOW);
digitalWrite (3,LOW);
digitalWrite (4,LOW);
digitalWrite (5,LOW);
ledPin=2;
}

else {digitalWrite (5,HIGH);
delay(2000);
}
}

(deleted)

Thanks a lot. That's a great suggestion, and makes complete sense... but have included a couple of digitalRead points in the loop and still not working unfortunately.Ahh, I really thought that was it. New code below:

#include "pitches.h"
#include <Servo.h>

int melody[] = {
NOTE_C5,NOTE_C5,NOTE_C5 };
int duration = 400; // 400 miliseconds

Servo myservo;

int pos = 0;
int wait = (3);
int servPosition = 10;
int servDelay = 1;
int ledPin = 2 ;
int pushButton = 13;
int buttonState = digitalRead(pushButton);

void setup() {
myservo.attach(10); // attaches the servo on pin 10 to the servo object
pinMode (1,OUTPUT);
pinMode (2,OUTPUT);
pinMode (3,OUTPUT);
pinMode (4,OUTPUT);
pinMode (5,OUTPUT);
pinMode (pushButton,INPUT);
Serial.begin(9600);
}

void loop () {

digitalRead(pushButton);

if (buttonState == HIGH)

{
myservo.write(servPosition);

for (int thisNote = 0; thisNote < 3; thisNote++) {
tone(8, melody[thisNote], duration);
digitalWrite (ledPin,HIGH);
delay(800);
ledPin++;}

tone (8,NOTE_C6, 800);
myservo.write(300);
digitalWrite (3,LOW);
digitalWrite (4,LOW);
digitalWrite (5,HIGH);
delay(1000);
digitalWrite (2,LOW);
digitalWrite (3,LOW);
digitalWrite (4,LOW);
digitalWrite (5,LOW);
ledPin=2;
}

else {digitalWrite (5,HIGH);
Serial.println(buttonState);
delay(1000);
}
digitalRead(pushButton);
}

You still don’t update the buttonState variable...

Thank you SO MUCH! That worked. I'd (wrongly) assumed that once variables are formulaically connected that they are bound for the entirity of the sketch and automatically update their state when the input variable changes. Lesson well and truly learnt. Many thanks for your help.

(deleted)

Computers are wonderful things. They do exactly what you tell them to do, which is not necessarily what you want them to do. Similarly, if you want something done, you have to explicitly tell the processor to do it.

(deleted)

Many thanks. Been through quite a few example sketches and tutorials - things just started to go a bit more complicated when I tried to combine them to make my own projects. Did some Basic coding when I was a kid which may have skewed my base assumptions. I was staring at the same bit of code for several hours and losing the will to live, so thank you very much for saving my sanity!

DKWatson:
Computers are wonderful things. They do exactly what you tell them to do, which is not necessarily what you want them to do. Similarly, if you want something done, you have to explicitly tell the processor to do it.

well that's not a general fact... (or you need to define who is you)

There are languages or environments where you define variables as formulas and they get re-evaluated when necessary.

For example take excel, you enter a formula in cell A4 like =A3+1, and you refer to A4 (= a variable) say in A5 with a formula that states =A4*2 and now if you change A3, then the A4 and A5 content will change for you --> the environment auto-updates all related formula, you did not have to do it "yourself" actively.

It's similar with constraint programming. Constraints differ from the imperative programming languages in that they do not specify a step or sequence of steps to execute, but rather the properties of a solution to be found.

In the past I played a lot with Prolog for some problems that would have required very complex algorithm and long code in imperative programming languages and would fit on a A4 sheet of paper as a set of constraints.

Thanks, interesting to know that this varies with environment. The excel example is fitting as I use this a lot - happy to blame familiarity with this this rather than my own stupidy

pallen33:
Thanks, interesting to know that this varies with environment. The excel example is fitting as I use this a lot - happy to blame familiarity with this this rather than my own stupidy

:smiling_imp: glad I helped :grin:

That's an old saying. Maybe I'm too old.

pallen33:
Thank you SO MUCH! That worked. I'd (wrongly) assumed that once variables are formulaically connected that they are bound for the entirity of the sketch and automatically update their state when the input variable changes. Lesson well and truly learnt. Many thanks for your help.

Yep - that's a common misunderstanding. C is an "imperative" language. A C program consists of a series of statements each of which are "Do this! Now, do this! And then … do this other thing!" This is in contrast to things like, say, an excel worksheet.