if statement not working :(

As the title says, one of my if statements is not working, i made a program using a switch statement, my problem is that i have two pushbutton, it is represented as var and val, my problem is that in the if else statement of the first pushbutton val, what it suppose to do is when i press this button the program will continue to the next case but at the simulation I did in tinkercad, it does not stop there and it continues and does not stop there, the second pushbutton works perfectly but i am having a bad time on this one. Thank you!

this my code

// Define STATES the program can be in:
#define WELCOME 0
#define WELCOME2 1
#define WELCOME3 2
#define WELCOME4 3
#define EXTEND_HAND 4
#define FLEX_HAND 5
#define CALIBRATING 6
#define WAITING 7
#define EXECUTION 8
int programState= WELCOME;
int looptimes;
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // Digital pins assinged on each LCD display pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#include <Servo.h>
Servo servo1; // creates the first servo object to control the servo
const int numReadings = 10;
int readings[numReadings];
int readIndex = 0;
int total = 0;
int average = 0;
const int inputPin = A1; // Analog Input pin for flex sensor
int pushpin = 7; // Digital Pin for pushbutton
int pushpin2 = 8;
int var = 0; // variable reading for pushpin
int val = 0;
int sensorValue = 0; // the sensor value
int sensorMax = 0; // maximum sensor value
int sensorMin = 1023; // minimum sensor value
void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
servo1.attach(10); // attaches the servo object to the PWM pin 2
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0; }
lcd.begin(16,2); // indicates the size of the LCD display 16x2

}
void loop()
{

// read input and average values here
// save new SensorValue
// MAIN PROGRAM for the Servomotors and LCD display
var = digitalRead(pushpin);
val= digitalRead(pushpin2);
total = total - readings[readIndex];
readings[readIndex] = analogRead(inputPin);
total = total + readings[readIndex];
readIndex = readIndex + 1;

if (readIndex >= numReadings) {
readIndex = 0;
}

sensorValue = total / numReadings;
Serial.print(sensorMax);
Serial.print("|");
Serial.print(sensorMin);
Serial.print("|");
Serial.println(sensorValue);
switch( programState )
{
case WELCOME:
sensorMax = 0;
sensorMin = 1023;
servo1.write(180);
lcd.clear();
lcd.home();
lcd.print(“WELCOME”);
programState = WELCOME2;
break;
case WELCOME2:
if( looptimes >= 500 ){ // done 1000 loops * 10mS = 10 seconds
lcd.home();
lcd.clear();
lcd.print(“PLEASE READY”);
lcd.setCursor(0,1);
lcd.print(“YOUR HAND”);
looptimes = 0;
programState= WELCOME3;}
break;
case WELCOME3:
if( looptimes >= 500 ){ // done 1000 loops * 10mS = 10 seconds
lcd.clear();
lcd.home();
lcd.print(“FOR CALIBRATION”);
looptimes = 0;
programState = WELCOME4;}
break;
case WELCOME4:
if( looptimes >= 500 ){ // done 1000 loops * 10mS = 10 seconds
lcd.home();
lcd.print(“PRESS CALIBRATION BUTTON”);
lcd.setCursor(0,1);
lcd.print(" WHEN READY");
looptimes =0;
if (val == HIGH){ //IT DOES NOT STOP HERE,? IT CONTINUES TO THE NEXT CASE
lcd.clear();
lcd.home();
lcd.print(“EXTEND YOUR HAND”);
programState= EXTEND_HAND;
looptimes= 0;
}}
break;
case EXTEND_HAND:
// record the maximum sensor value while in this state
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
if( looptimes >= 500 ){ // done 1000 loops * 10mS = 10 seconds
lcd.clear();
lcd.print(“FLEX YOUR HAND”); // change display for next state
programState= FLEX_HAND; // and switch to next STATE
looptimes= 0; // remember to clear the loop counter
}
break;
case FLEX_HAND:
// record the minimum sensor value while in this state
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
if( looptimes >= 500 ){ // done 1000 loops * 10mS = 10 seconds
lcd.clear();
lcd.print(“CALIBRATING”);
programState= CALIBRATING;
looptimes= 0;
}
break;
case CALIBRATING:
// Calibrating state
if( looptimes >= 500 ){
lcd.home();
lcd.print(“FINISHED”);
looptimes= 0;
programState= WAITING;
}
break;
case WAITING:
// program will stay in this state forever
// unless we read some button and change state for example
lcd.home();
lcd.print(" PRESS TO START “);
if (var == HIGH ){ // This conditions applies to the pushbutton when presssed
looptimes= 0;
programState= EXECUTION;
}
break;
case EXECUTION:
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 180);
sensorValue = constrain(sensorValue, 0, 180); // in case the sensor value is outside the range seen during calibration
servo1.write(sensorValue);
lcd.clear();
lcd.setCursor(0, 0); // sets the character to column 0, row 0
lcd.print(sensorValue);
lcd.print(” “);
lcd.print(” Degrees ");
if (var == HIGH ) {
programState = WELCOME;
}
}
looptimes= looptimes+1; // count number of loops
delay( 10 ); // go around loop() every 10 milliseconds
}

Welcome to the forum! Please read How to use the forum and edit your post accordingly.

acesagcal:
it is represented as var and val

That's stupid! Variable names are there to help you. So just make it something sensible. This does not show they are even related to buttons nor what the buttons do. Something like stateButtonNext and stateButtonPrevious for example show what they do.

Next, simulations are useless crap if it gets more complicated than blinking a LED...

And if you really want to count, at least number every variable :wink: Or wayyyyyyy bettter, arrays!

And nothing follows a ; (except in a for()). Otherwise code just becomes hard to read :confused:

one of my if statements is not working

It most certainly IS. That it doesn't produce the results you expect simply means that your expectations are wrong.

my problem is that i have two pushbutton

Why is that a problem? How ARE the switches wired?

it is represented as var and val

What is? What are those names supposed to mean? That you have no imagination?

Why do you not declare that the mode of the pins that the switches are attached to are INPUT? You don't enable the pullup resistors, so that means that you have external pullup or pulldown resistors. Which DO you have? Why? It is far simpler to wire a switch properly using the internal pullup resistors.

If you don't have external resistors, you have floating pins, and have NO idea what the value read from the pin will mean, when the switch is not pressed. Therefore, you can not properly write code to deal with the state of the switches.

That’s stupid! Variable names are there to help you. So just make it something sensible.

Excuse me. You stole MY line.

When debugging problems with if and while it is helpful to print the value being tested before the test. This shows you what is being tested and whether the code ever executes the statements

How are the inputs wired ?
Any pullup or pulldown resistors in place or are the inputs floating at an unknown voltage ?
Consider using INPUT_PULLUP as the pinMode and wire the buttons to take the pin to GND when pressed

these are the pushbuttons

Capture.JPG

OP’s image.

a8993fb6360e134b6cd7a42c641b9a900969972a.jpg

Looks, to me, like the switches are always LOW, but hard to tell.

How to post an image.

the second pushbutton actually works, i am really wondering why the first one is always skipped :frowning:

acesagcal:
the second pushbutton actually works, i am really wondering why the first one is always skipped :frowning:

You have NOT properly defined how the switches are connected to the Arduino. You have FAR too much code for someone who thinks they are having problems.

Ditch that code, and write code that does NOTHING more than read the switch states, and print to the serial port. Don't even think about adding more code until that minimal sketch ALWAYS reports the correct state of both switches.

acesagcal:
the second pushbutton actually works, i am really wondering why the first one is always skipped :frowning:

Exchange the 2 pushbuttons on the breadboard. Does the problem follow the button ?

If the pushbuttons that you are using are square in shape are you sure than you have them oriented correctly ? Make the connections to them only from a pair of diagonally opposite pins to ensure that you have connected to the actual switches and not to 2 pins permanently connected together in the switch.

sorry for bothering everyone else, i have solved the problem, the resistor on the first pushbutton was not connected at the ground, my computer was lagging and i thought it was already connected, thanks for everyone that tried to help, again im so sorry....