How to write proper if statements

This is a question that i don't know the solution

LOGIC: if myswitch = 1, the stepper motor must rotate to the right. myswitch = 1 only when the buttonStateRight is HIGH (clicked) and buttonStateHomeTriggerSwitch is LOW, if you click the switch (buttonStateHomeTriggerSwitch) while pressing buttonStateRight , the stepper motor must stop.

The code below suppose to do the same logic but its not working

void loop() {
  // put your main code here, to run repeatedly:
  // read the state of the pushbutton value:
  buttonStateHomeTriggerSwitch = digitalRead(buttonHomeTrigger);
  buttonStateRight = digitalRead(buttonRight);
  

if ((buttonStateRight == HIGH) && (buttonStateHomeTriggerSwitch == HIGH))
{
  String printValue = "Both HIGH: Trigger =  " + buttonStateHomeTriggerSwitch; //+ " Button = " + buttonStateRight;
  Serial.println(printValue);
   myswitch = 0;
}
else if ((buttonStateRight == HIGH) && (buttonStateHomeTriggerSwitch == LOW)) {
  String printValue = "Button HIGH: Trigger =  " + buttonStateHomeTriggerSwitch; //+ " Button = " + buttonStateRight;
  Serial.println(printValue);
   myswitch = 1;
}

else if ((buttonStateRight == LOW) && (buttonStateHomeTriggerSwitch == LOW))
{
  String printValue = "Both LOW: Trigger =  " + buttonStateHomeTriggerSwitch; //+ " Button = " + buttonStateRight;
  Serial.println(printValue);
  myswitch = 0;
}

else if ((buttonStateRight == LOW) && (buttonStateHomeTriggerSwitch == HIGH)) {
  String printValue = "B LOW: Trigger =  " + buttonStateHomeTriggerSwitch; //+ " Button = " + buttonStateRight;
  Serial.println(printValue);
   myswitch = 0;
}
else
{
      digitalWrite(ledPin, LOW);
   myswitch = 0;
}


if(myswitch == 1) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
       TurnRight();
    
  }
}

but its not working

That does not give us much to go on. What happens and why did you not post the complete program ?

When debugging code that tests values it is useful to print the values being tested, which you have done apart from the all important myswitch variable. I presume that the values printed are as expected although the the use of Strings is to be avoided in most cases.

How are the inputs wired ? Do you have pulldown resistors in place to hold the inputs in a known state at all times or are they floating at an unknown voltage ?

I provided the full code below, when i run the code and click and hold buttonStateRight and then click
buttonStateHomeTriggerSwitch the motor must stop but now it is not stopping. it is accessing 2 'if statement at the same time', it is running these

if ((buttonStateRight == HIGH) && (buttonStateHomeTriggerSwitch == HIGH))
{
  String printValue = "Both HIGH: Trigger =  " + buttonStateHomeTriggerSwitch; //+ " Button = " + buttonStateRight;
  Serial.println(printValue);
   myswitch = 0;
}
else if ((buttonStateRight == HIGH) && (buttonStateHomeTriggerSwitch == LOW)) {
  String printValue = "Button HIGH: Trigger =  " + buttonStateHomeTriggerSwitch; //+ " Button = " + buttonStateRight;
  Serial.println(printValue);
   myswitch = 1;
}

which is suppose to access only one if statement that the myswitch = 0;

const int buttonHome = 10;     // the number of the pushbutton pin
const int buttonLeft = 4;     // the number of the pushbutton pin
const int buttonRight = 9;     // the number of the pushbutton pin
const int buttonOn = 12;     // the number of the pushbutton pin
const int buttonOff = 11;     // the number of the pushbutton pin
const int buttonHomeTrigger = 13;     // the number of the home limit switch pin
const int buttonEndTrigger = 7;     // the number of the end limit switch pin
const int ledPin =  6;      // the number of the LED pin

//For stepper
const int stepPin = 5; 
const int dirPin = 2; 
const int enPin = 8;

// variables will change:
int buttonStateLeft = 0;         // variable for reading the pushbutton status
int buttonStateRight = 0;  
int buttonStateHome = 0;  
int buttonStateOn = 0;  
int buttonStateOff = 0; 
int buttonStateHomeTriggerSwitch = 0; 

int myswitch = 0;

int buttonHomeLimit = 0;
int buttonEndLimit = 0; 

void setup() {
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonLeft, INPUT);
  pinMode(buttonRight, INPUT);
  pinMode(buttonHome, INPUT);
  pinMode(buttonHomeTrigger, INPUT);
  pinMode(buttonEndTrigger, INPUT);
  pinMode(buttonOn, INPUT);
  pinMode(buttonOff, INPUT);
    // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);

  pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);
}

void loop() {
  // put your main code here, to run repeatedly:
  // read the state of the pushbutton value:
  buttonStateHomeTriggerSwitch = digitalRead(buttonHomeTrigger);
  buttonStateRight = digitalRead(buttonRight);
  

if ((buttonStateRight == HIGH) && (buttonStateHomeTriggerSwitch == HIGH))
{
  String printValue = "Both HIGH: Trigger =  " + buttonStateHomeTriggerSwitch; //+ " Button = " + buttonStateRight;
  Serial.println(printValue);
   myswitch = 0;
}
else if ((buttonStateRight == HIGH) && (buttonStateHomeTriggerSwitch == LOW)) {
  String printValue = "Button HIGH: Trigger =  " + buttonStateHomeTriggerSwitch; //+ " Button = " + buttonStateRight;
  Serial.println(printValue);
   myswitch = 1;
}

else if ((buttonStateRight == LOW) && (buttonStateHomeTriggerSwitch == LOW))
{
  String printValue = "Both LOW: Trigger =  " + buttonStateHomeTriggerSwitch; //+ " Button = " + buttonStateRight;
  Serial.println(printValue);
  myswitch = 0;
}

else if ((buttonStateRight == LOW) && (buttonStateHomeTriggerSwitch == HIGH)) {
  String printValue = "B LOW: Trigger =  " + buttonStateHomeTriggerSwitch; //+ " Button = " + buttonStateRight;
  Serial.println(printValue);
   myswitch = 0;
}
else
{
      digitalWrite(ledPin, LOW);
   myswitch = 0;
}


if(myswitch == 1) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
       TurnRight();
    
  }
}

int TurnRight(){
      digitalWrite(dirPin,LOW); //Changes the rotations direction
  // Makes 400 pulses for making two full cycle rotation
  for(int x = 0; x < 8 ; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(100);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(100);
    }
}

First of all: use DIRECTLY the digitalRead () into the if condictions
Second: if (value or function or variable) is "if it is not zero", so in case of digitalRead () the '==HIGH' is useless
Third: all the variable that can't be nore than 255 can became 'byte', so less expansive.
Another: NEVER, NEVER, NEVER use Strings on Arduino. You can have the same results by this code:

Serial.print/ln(F(" text you need write"));;

First of all: use DIRECTLY the digitalRead () into the if condictions

First of all, ignore that advice.

AWOL:
First of all, ignore that advice.

+1

...R

And the input wiring is what ?

I note that you are not using the built in pullup resistors and are testing inputs for HIGH to determine whether the buttons are pressed. So, do you have pulldown resistors in place ?

Print the values of buttonStateHomeTriggerSwitch and buttonStateRight immediately after you read the inputs. What do you see ? Is it what you expect ?

Is the circuit built on a breadboard and if so are you sure that all the connections are sound and has the breadboard got split or continuous power rails ?

How is the stepper powered ?

What does the serial output and behavior look like for this simplified version?

const int buttonHome = 10;     // the number of the pushbutton pin
const int buttonLeft = 4;     // the number of the pushbutton pin
const int buttonRight = 9;     // the number of the pushbutton pin
const int buttonOn = 12;     // the number of the pushbutton pin
const int buttonOff = 11;     // the number of the pushbutton pin
const int buttonHomeTrigger = 13;     // the number of the home limit switch pin
const int buttonEndTrigger = 7;     // the number of the end limit switch pin
const int ledPin =  6;      // the number of the LED pin


//For stepper
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;


// variables will change:
int buttonStateLeft = 0;         // variable for reading the pushbutton status
int buttonStateRight = 0;
int buttonStateHome = 0;
int buttonStateOn = 0;
int buttonStateOff = 0;
int buttonStateHomeTriggerSwitch = 0;


int myswitch = 0;


int buttonHomeLimit = 0;
int buttonEndLimit = 0;


void setup()
{
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonLeft, INPUT);
  pinMode(buttonRight, INPUT);
  pinMode(buttonHome, INPUT);
  pinMode(buttonHomeTrigger, INPUT);
  pinMode(buttonEndTrigger, INPUT);
  pinMode(buttonOn, INPUT);
  pinMode(buttonOff, INPUT);


  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);


  pinMode(enPin, OUTPUT);
  digitalWrite(enPin, LOW);
}


void loop()
{
  // put your main code here, to run repeatedly:
  // read the state of the pushbutton value:
  boolean buttonStateRight = digitalRead(buttonRight);
  boolean buttonStateHomeTriggerSwitch = digitalRead(buttonHomeTrigger);


  Serial.print(buttonStateRight);
  Serial.print(' ');
  Serial.println(buttonStateHomeTriggerSwitch);


  if ((buttonStateRight == HIGH) && (buttonStateHomeTriggerSwitch == LOW))
  {
    digitalWrite(ledPin, HIGH);    // turn LED on:
    Serial.println("Runing...");
    TurnRight();
  }
  else
  {
    Serial.println("STOPPED...");

    digitalWrite(ledPin, LOW);
  }


  delay(500);  // Just for testing to slow down the serial output
}


void TurnRight()
{
  digitalWrite(dirPin, LOW);


  for (int x = 0; x < 8 ; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(100);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(100);
  }
}

Good day, when the button and the switch are clicked at the same time it stop and then it run again.

Check the display below

1 1
STOPPED...
1 1
STOPPED...
1 0
Runing...
1 0
Runing...
1 0
Runing...
1 0
Runing...
1 1
STOPPED..
1 1
STOPPED..
1 1
STOPPED..
1 0
Runing...
1 0
Runing...
1 0
Runing...
1 0
Runing...

Hi,
How have you got your button wired to the controller input?

If you are switching the input to 5V, have you got a 10k resistor between the input and gnd?

If you are switching the input to gnd, have you got a 10k resistor between the input and 5V?

You cannot leave an input pin open circuit.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

AWOL:
First of all, ignore that advice.

Could you elaborate on that please?

I'm guessing what you mean is to NOT do this:

if (digitalRead(pin)==0){
   do something....
  }

but rather do this instead:

int pinState = digitalRead(pin);
if (pin == 0){
   do something...
  }

Why is putting the digitalRead() in the if-statement a bad choice? It works, and reduces lines of code and variables required without being messy to read.

silly_cone:
Why is putting the digitalRead() in the if-statement a bad choice? It works, and reduces lines of code and variables required without being messy to read.

Good luck debugging it.

silly_cone:
Why is putting the digitalRead() in the if-statement a bad choice? It works, and reduces lines of code and variables required without being messy to read.

Because it makes it impossible to check the value that is read before it is used in the IF statement.

Who cares how many lines of code there are? It's not as if you are wasting reams of paper.

...R

Robin2:
It's not as if you are wasting reams of paper.

... unless you are printing out your code on paper :slight_smile:
Haha !!

Robin2:
Because it makes it impossible to check the value that is read before it is used in the IF statement.

Gotcha, simplifies debugging.

I thought maybe it was considered poor-form due to memory or processor-cycles or something along those lines.

silly_cone:
Why is putting the digitalRead() in the if-statement a bad choice? It works, and reduces lines of code and variables required without being messy to read.

If you read your inputs at the start of the loop, you effectively take a snapshot of your inputs, then use the code to act on it.
If you are continually reading inputs throughout the code, it makes it hard to debug, if your code has any blocking code, then a mid change of input could disrupt your program.

if (val == 0)

is quicker than typing

if (digitalRead(valPin) == 0)

It also encourages the programmer to write descriptive names for the variables.

Tom... :slight_smile:

shonieratshie:
Good day, when the button and the switch are clicked at the same time it stop and then it run again.

Check the display below
1 1
STOPPED...
1 0
Runing...

Looks like it is doing exactly as you asked: When buttonStateRight is HIGH and buttonStateHomeTriggerSwitch is LOW it turns on the LED and takes 8 steps to the right. When both are HIGH it turns off the LED and doesn't move. Was that not what you wanted?!?

Good day

I got the solution, i changed the switched from INPUT to INPUT_PULLUP

pinMode(buttonHomeTrigger, INPUT_PULLUP);
pinMode(buttonEndTrigger, INPUT_PULLUP);

Thank you for trying to help me...