doubt with if() /if(! ) /else funct and coding with limit switches and buttons

Hi everybody

I’m using:
-Arduino UNO R3

  • NEMA 23 19kg.cm 200 steps 1.8° stepper motor ( (datasheet)

-TB 6560 Driver

  • 12V 5A power source

  • 2 (two) push buttons ( that we will call button1 and button2)
    -2 limit switches/mechanical endstops (that we will call limitPin1 and limitPin2)

As an attached file you may see the wiring of my project

I’m having a some doubts regarding the "if ( ) ", “if (! )”, and “else” functions.

this is the logic my project should follow:

  • If button1 is pressed, the motor will rotate 300 steps CW
    -If button2 is pressed, the motor will rotate 300 steps CCW
  • If button1 is pressed, and while rotating, limitPin1 is pressed, stop rotation.
    -if button2 is pressed, and while rotating, limitPin2 is pressed, stop rotation.

I have written 2 codes, but haven’t tested them yet using “if ( )”(in one code) and “if (! )” functions (on the other code), with comments almost in every line.
Both codes are basically the same except for some lines which are specified below:

This is code N°1:

#include <Stepper.h>

// DEFINE PIN NUMBERS
const int dirPin  = 3; //Direction pin
const int stepPin = 2;  // Stepper pin
const int enPin   = 6;  //Enable pin

const int limitPin1  =7; //LimitPin1
const int limitPin2 =4; //LimitPin2

const int button1     = 8; //Button1
const int button2     = 9; //Button2

int p1buttonState = 0;         // current state of the button
int lastp1buttonState = 0;     // previous state of the button

int p2buttonState = 0;         // current state of the button
int lastp2buttonState = 0;     // previous state of the button
bool bPress = false;

bool isForward = false;
bool isBackward = false;


const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor


// initialize the stepper library on pins 8 through 11:
//Stepper myStepper(stepsPerRevolution, 3, 2, 6);  //CAMBIAR ESTOS PINES

void setup() {
   // set the speed at 60 rpm:
//  myStepper.setSpeed(60);
  
   Serial.begin(9600);
  pinMode( button1, INPUT_PULLUP); //Button1 as an input signal
  pinMode( button2, INPUT_PULLUP); //Button2 as an input signal
  pinMode (limitPin1,INPUT); // Limitpin1  as input signal
  pinMode (limitPin2,INPUT); // Limitpin2 as input signal

  // Sets the two pins as outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);

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

}
void loop() {

   isForward=false;
   isBackward=false;
   
   p1buttonState = digitalRead(button1);
   p2buttonState = digitalRead(button2);
 // up to here I get no coding error according to arduino's verify command
{
if (p1buttonState == HIGH ) { //if button1 is pressed
  if (digitalRead(limitPin1) == LOW ){//if limitPin1 is pressed

digitalWrite(dirPin,LOW);}
  
  delay(5);
}
else { // if it is not, then limitpin1 is not activated, rotate the motor CW
 
  digitalWrite(dirPin,HIGH);
  
  delay(5);
                      }
{
if (p2buttonState == HIGH ) { // if button2 is pressed
  if (digitalRead(limitPin2) == LOW ){ //if limitPin2 is pressed
   digitalWrite(dirPin,LOW);}
  
}
  else { // if it is not, then limitpin2 is not activated, rotate the motor CCW
     
   digitalWrite(dirPin,HIGH);

  delay(5);
                     }
            


if (isForward || isBackward) {

              for (int x=0; x<300; x++) {
                digitalWrite(stepPin,HIGH);
                delayMicroseconds(1200);
                digitalWrite(stepPin,LOW);
                delayMicroseconds(1200);
              }
}
}
}
}
//HASTA ACA OK

and this is the part itself from code N°1 that changes

{
if (p1buttonState == HIGH ) { //if button1 is pressed
  if (digitalRead(limitPin1) == LOW ){//if limitPin1 is pressed

digitalWrite(dirPin,LOW);}
  
  delay(5);
}
else { // if it is not, then limitpin1 is not activated, rotate the motor CW
 
  digitalWrite(dirPin,HIGH);
  
  delay(5);
                      }
{
if (p2buttonState == HIGH ) { // if button2 is pressed
  if (digitalRead(limitPin2) == LOW ){ //if limitPin2 is pressed
   digitalWrite(dirPin,LOW);}
  
}
  else { // if it is not, then limitpin2 is not activated, rotate the motor CCW
     
   digitalWrite(dirPin,HIGH);

  delay(5);
                     }

This is code N°2:

#include <Stepper.h>

// DEFINE PIN NUMBERS
const int dirPin  = 3; //Direction pin
const int stepPin = 2;  // Stepper pin
const int enPin   = 6;  //Enable pin

const int limitPin1  =7; //LimitPin1
const int limitPin2 =4; //LimitPin2

const int button1     = 8; //Button1
const int button2     = 9; //Button2

int p1buttonState = 0;         // current state of the button
int lastp1buttonState = 0;     // previous state of the button

int p2buttonState = 0;         // current state of the button
int lastp2buttonState = 0;     // previous state of the button
bool bPress = false;

bool isForward = false;
bool isBackward = false;


const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor


// initialize the stepper library on pins 8 through 11:
//Stepper myStepper(stepsPerRevolution, 3, 2, 6);  //CAMBIAR ESTOS PINES

void setup() {
   // set the speed at 60 rpm:
//  myStepper.setSpeed(60);
  
   Serial.begin(9600);
  pinMode( button1, INPUT_PULLUP); //Botton1 as an input signal
  pinMode( button2, INPUT_PULLUP); //Button2 as an input signal
  pinMode (limitPin1,INPUT); // LimitPin1  as input signal
  pinMode (limitPin2,INPUT); // LimitPin2 as input signal

  // Sets the two pins as outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);

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

}
void loop() {

   isForward=false;
   isBackward=false;
   
   p1buttonState = digitalRead(button1);
   p2buttonState = digitalRead(button2);
 // UP TO HERE IF I click on verify,everything's OK
{
if (p1buttonState == HIGH ) { //if button1 is pressed
  if (!digitalRead(limitPin1)== HIGH){//if limitPin1 is pressed
//if it IS pressed, do nothing
}
else { // if it is not, then limitpin1 is not activated, rotate the motor CW
 
  digitalWrite(dirPin,LOW); //(HIGH=CCW / LOW= CW)
  digitalWrite(stepPin,HIGH);}
  delay(5);
                      }
{
if (p2buttonState == HIGH ) { // if button2 is pressed
  if (!digitalRead(limitPin2) == HIGH)  //if limitPin2 is pressed
 {
    }
  else { // if it is not, then limitpin2 is not activated, rotate the motor CCW
     
   digitalWrite(dirPin,HIGH); // (HIGH=CCW / LOW=CW)
   digitalWrite(stepPin,HIGH);

  delay(5);
                     }
            


if (isForward || isBackward) {

              for (int x=0; x<300; x++) {
                digitalWrite(stepPin,HIGH);
                delayMicroseconds(1200);
                digitalWrite(stepPin,LOW);
                delayMicroseconds(1200);
              }
}
}
}
}
}
//by clicking verify everything appears as OK

and this is the part itself from code N°2 that changes:

{
if (p1buttonState == HIGH ) { //if button1 is pressed
  if (!digitalRead(limitPin1)== HIGH){//if limitPin1 is pressed

}
else { // if it is not, then limitpin1 is not activated, rotate the motor CW
 
  digitalWrite(dirPin,LOW); //(HIGH=CCW / LOW= CW)
  digitalWrite(stepPin,HIGH);}
  delay(5);
                      }
{
if (p2buttonState == HIGH ) { // if button2 is pressed
  if (!digitalRead(limitPin2) == HIGH)  //if limitPin2 is pressed
 {  // if it IS pressed, do nothing (stop rotation)
    }
  else { // if it is not, then limitpin2 is not activated, rotate the motor CCW
     
   digitalWrite(dirPin,HIGH); // (HIGH=CCW / LOW=CW)
   digitalWrite(stepPin,HIGH);

  delay(5);
                     }

even though i’m not getting any error messages by clicking on arduino’s verify command,
I’m not entirely sure if:
-Both of these codes will work
-none of these codes will work

  • the coding sequence is correct

What do you guys think?
That code took me a long time to write. I watched several examples and codes, and tried to “extrapolate” them in order to achieve that code…

Please be advised that I’m a newbie ( so please be gentile :stuck_out_tongue: )

Thanks for reading this

const int button1     = 8; //Button1
if (button1 == HIGH ) { //if button1 is pressed

button1 will always have a value of 8.

Did you perhaps mean

if (p1buttonState == HIGH )

Yes, you’re right. thank you :slight_smile:

I’ll edit the main post

with that correction, codes should actually be:

code N°1:

#include <Stepper.h>

// DEFINE PIN NUMBERS
const int dirPin  = 3; //Direction pin
const int stepPin = 2;  // Stepper pin
const int enPin   = 6;  //Enable pin

const int limitPin1  =7; //LimitPin1
const int limitPin2 =4; //LimitPin2

const int button1     = 8; //Button1
const int button2     = 9; //Button2

int p1buttonState = 0;         // current state of the button
int lastp1buttonState = 0;     // previous state of the button

int p2buttonState = 0;         // current state of the button
int lastp2buttonState = 0;     // previous state of the button
bool bPress = false;

bool isForward = false;
bool isBackward = false;


const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor


// initialize the stepper library on pins 8 through 11:
//Stepper myStepper(stepsPerRevolution, 3, 2, 6);  //CAMBIAR ESTOS PINES

void setup() {
   // set the speed at 60 rpm:
//  myStepper.setSpeed(60);
  
   Serial.begin(9600);
  pinMode( button1, INPUT_PULLUP); //Botton1 as an input signal
  pinMode( button2, INPUT_PULLUP); //Button2 as an input signal
  pinMode (limitPin1,INPUT); // Limitpin1  as input signal
  pinMode (limitPin2,INPUT); // Limitpin2 as input signal

  // Sets the two pins as outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);

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

}
void loop() {

   isForward=false;
   isBackward=false;
   
   p1buttonState = digitalRead(button1);
   p2buttonState = digitalRead(button2);
 // HASTA ACA TODO OK
{
if (p1buttonState == HIGH ) { //if button1 is pressed
  if (digitalRead(limitPin1) == LOW ){//if limitPin1 is pressed

digitalWrite(dirPin,LOW);}
  
  delay(5);
}
else { // if it is not, then limitpin1 is not activated, rotate the motor CW
 
  digitalWrite(dirPin,HIGH);
  
  delay(5);
                      }
{
if (p2buttonState == HIGH ) { // if button2 is pressed
  if (digitalRead(limitPin2) == LOW ){ //if limitPin2 is pressed
   digitalWrite(dirPin,LOW);}
  
}
  else { // if it is not, then limitpin2 is not activated, rotate the motor CCW
     
   digitalWrite(dirPin,HIGH);

  delay(5);
                     }
            


if (isForward || isBackward) {

              for (int x=0; x<300; x++) {
                digitalWrite(stepPin,HIGH);
                delayMicroseconds(1200);
                digitalWrite(stepPin,LOW);
                delayMicroseconds(1200);
              }
}
}
}
}
//HASTA ACA OK

code N°2:

#include <Stepper.h>

// DEFINE PIN NUMBERS
const int dirPin  = 3; //Direction pin
const int stepPin = 2;  // Stepper pin
const int enPin   = 6;  //Enable pin

const int limitPin1  =7; //LimitPin1
const int limitPin2 =4; //LimitPin2

const int button1     = 8; //Button1
const int button2     = 9; //Button2

int p1buttonState = 0;         // current state of the button
int lastp1buttonState = 0;     // previous state of the button

int p2buttonState = 0;         // current state of the button
int lastp2buttonState = 0;     // previous state of the button
bool bPress = false;

bool isForward = false;
bool isBackward = false;


const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor


// initialize the stepper library on pins 8 through 11:
//Stepper myStepper(stepsPerRevolution, 3, 2, 6);  //CAMBIAR ESTOS PINES

void setup() {
   // set the speed at 60 rpm:
//  myStepper.setSpeed(60);
  
   Serial.begin(9600);
  pinMode( button1, INPUT_PULLUP); //Botton1 as an input signal
  pinMode( button2, INPUT_PULLUP); //Button2 as an input signal
  pinMode (limitPin1,INPUT); // LimitPin1  as input signal
  pinMode (limitPin2,INPUT); // LimitPin2 as input signal

  // Sets the two pins as outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);

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

}
void loop() {

   isForward=false;
   isBackward=false;
   
   p1buttonState = digitalRead(button1);
   p2buttonState = digitalRead(button2);
 // UP TO HERE IF I click on verify,everything's OK
{
if (p1buttonState == HIGH ) { //if button1 is pressed
  if (!digitalRead(limitPin1)== HIGH){//if limitPin1 is pressed

}
else { // if it is not, then limitpin1 is not activated, rotate the motor CW
 
  digitalWrite(dirPin,LOW); //(HIGH=CCW / LOW= CW)
  digitalWrite(stepPin,HIGH);}
  delay(5);
                      }
{
if (p2buttonState == HIGH ) { // if button2 is pressed
  if (!digitalRead(limitPin2))  //if limitPin2 is pressed
 {
    }
  else { // if it is not, then limitpin2 is not activated, rotate the motor CCW
     
   digitalWrite(dirPin,HIGH); // (HIGH=CCW / LOW=CW)
   digitalWrite(stepPin,HIGH);

  delay(5);
                     }
            


if (isForward || isBackward) {

              for (int x=0; x<300; x++) {
                digitalWrite(stepPin,HIGH);
                delayMicroseconds(1200);
                digitalWrite(stepPin,LOW);
                delayMicroseconds(1200);
              }
}
}
}
}
}
//by clicking verify everything appears as OK

UKHeliBob:

const int button1     = 8; //Button1
if (button1 == HIGH ) { //if button1 is pressed

button1 will always have a value of 8.

Did you perhaps mean

if (p1buttonState == HIGH )

I don't know exactly why, but if i change "button1" for "p1buttonState" on that line
the following error appears:

"exit Status1
'p1buttonState' was not declared in this scope .... :frowning:

You have a loop that moves 300 steps without checking the limit switches. In your description of the code you say:

  • If button1 is pressed, and while rotating, limitPin1 is pressed, stop rotation.
    -if button2 is pressed, and while rotating, limitPin2 is pressed, stop rotation.

To do that you have to check the limit switch before EVERY step.

I don't know exactly why, but if i change "button1" for "p1buttonState" on that line
the following error appears:

"exit Status1
'p1buttonState' was not declared in this scope .

Both of the programs in post #2 compile OK for me.

In the code posted I can see no reason why p1buttonState should be out of scope when its value is tested as it is declared as a global variable. It does not need to be an int but that is irrelevant for now

Please post the complete error message

Thank you Paul

I did some digging on what you said and I found this post where you also posted something similar (that’s a big coincidence!)

and according to what you posted, im guessing my code should say something like:

void loop() {
  for (int x = 0; x < 300 && digitalRead(limitPin1); x++)

now… is there an “or” variable in arduino? ( to use that same line with the two limitPins
for example:

 for (int x = 0; x < 300 && (digitalRead(limitPin1) == HIGH) "OR" (digitalRead(limitPin2) == HIGH); x++)

or should I write :

 for (int x=0; x<300 &&  (digitalRead(limitPin1)==HIGH); x++) {
                digitalWrite(stepPin,HIGH);
                delayMicroseconds(1200);
                digitalWrite(stepPin,LOW);
                delayMicroseconds(1200);

 for (int x=0; x<300 && (digitalRead(limitPin2)==HIGH); x++) {
                digitalWrite(stepPin,HIGH);
                delayMicroseconds(1200);
                digitalWrite(stepPin,LOW);
                delayMicroseconds(1200);

Again, thank you!

UKHeliBob:

I don't know exactly why, but if i change "button1" for "p1buttonState" on that line

the following error appears:

"exit Status1
‘p1buttonState’ was not declared in this scope .




Both of the programs in post #2 compile OK for me.

In the code posted I can see no reason why p1buttonState should be out of scope when its value is tested as it is declared as a global variable. It does not need to be an int but that is irrelevant for now

Please post the complete error message

Sure thing

This is the error(also screenshot as an attached file):

C:\Users\AGMARTIN\Desktop\prueba_de_kamchatka\prueba_de_kamchatka.ino: In function ‘void loop()’:

prueba_de_kamchatka:60:5: error: ‘p1buttonSate’ was not declared in this scope

if (p1buttonSate == HIGH ) { //if button1 is pressed

^

exit status 1
‘p1buttonSate’ was not declared in this scop

Just in case,once again, this is the code:

#include <Stepper.h>

// DEFINE PIN NUMBERS
const int dirPin  = 3; //Direction pin
const int stepPin = 2;  // Stepper pin
const int enPin   = 6;  //Enable pin

const int limitPin1  =7; //LimitPin1
const int limitPin2 =4; //LimitPin2

const int button1     = 8; //Button1
const int button2     = 9; //Button2

int p1buttonState = 0;         // current state of the button
int lastp1buttonState = 0;     // previous state of the button

int p2buttonState = 0;         // current state of the button
int lastp2buttonState = 0;     // previous state of the button
bool bPress = false;

bool isForward = false;
bool isBackward = false;


const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor


// initialize the stepper library on pins 8 through 11:
//Stepper myStepper(stepsPerRevolution, 3, 2, 6);  //CAMBIAR ESTOS PINES

void setup() {
   // set the speed at 60 rpm:
//  myStepper.setSpeed(60);
  
   Serial.begin(9600);
  pinMode( button1, INPUT_PULLUP); //Botton1 as an input signal
  pinMode( button2, INPUT_PULLUP); //Button2 as an input signal
  pinMode (limitPin1,INPUT); // Limitpin1  as input signal
  pinMode (limitPin2,INPUT); // Limitpin2 as input signal

  // Sets the two pins as outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);

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

}
void loop() {

   isForward=false;
   isBackward=false;
   
   p1buttonState = digitalRead(button1);
   p2buttonState = digitalRead(button2);
 // HASTA ACA TODO OK
{
if (p1buttonSate == HIGH ) { //if button1 is pressed
  if (digitalRead(limitPin1) == LOW ){//if limitPin1 is pressed

digitalWrite(dirPin,LOW);}
  
  delay(5);
}
else { // if it is not, then limitpin1 is not activated, rotate the motor CW
 
  digitalWrite(dirPin,HIGH);
  
  delay(5);
                      }
{
if (p2buttonState == HIGH ) { // if button2 is pressed
  if (digitalRead(limitPin2) == LOW ){ //if limitPin2 is pressed
   digitalWrite(dirPin,LOW);}
  
}
  else { // if it is not, then limitpin2 is not activated, rotate the motor CCW
     
   digitalWrite(dirPin,HIGH);

  delay(5);
                     }
            


if (isForward || isBackward) {

              for (int x=0; x<300 && (digitalRead(limitpin1)==HIGH); x++) {   //(evaluating if there's an "OR" variable to use in the same line with limitpin1 OR limitpin2 =HIGH)
                digitalWrite(stepPin,HIGH);
                delayMicroseconds(1200);
                digitalWrite(stepPin,LOW);
                delayMicroseconds(1200);
              }
}
}
}
}
//HASTA ACA OK

In post #3 you said that the error was

"exit Status1
'p1buttonState' was not declared in this scope

and in post #2 the code was

if (p1buttonState == HIGH ) { //if button1 is pressed

, which is correct

But now the code is

if (p1buttonSate == HIGH ) { //if button1 is pressed

It appears that you changed something.

Check the name of the variable you are testing

Yes or does exist. It is written "or".

Alternatively you will see "||" used as or as well.