Go Down

Topic: -Newbie /where to add a limit switch code to my existing sketch up (Read 361 times) previous topic - next topic

tinback123

You asked "INPUT or INPUT_PULLOUT?"  Never INPUT_PULLOUT.  Perhaps you intended INPUT_PULLUP.  It is hard to go wrong with INPUT_PULLUP no matter what your wiring.  However, if you really want to choose between INPUT and INPUT_PULLUP, you need to share your wiring diagram or equivalent.  I cannot see your wiring and my crystal ball is in the shop!   :smiley-lol:

This

Code: [Select]

  if (!digitalRead(limitPin1)) {} //check if LimitPin1 was pressed  (is this right?)


will probably work correctly, but according to the documentation at
https://www.arduino.cc/reference/en/language/functions/digital-io/digitalread/
it should be

Code: [Select]

  if (digitalRead(limitPin1) == LOW) {} //check if LimitPin1 was pressed.


although possibly LOW should be HIGH depending upon how the limit switches work and how they are wired.

Hi!
I'm attaching my wiring diagram
I did not include on it any power supply, but in case you have any doubts.
The driver is connected to a 12V 5 A power supply ( the motor is a nema 23 stepper motor up to 3A)
and the arduino is connected via USB.

so inputs for the limit switch should be pins 4 and 7

I did a boo-boo before so my code should actually be this at start:

// define the numer of pins
const int dirPin  = 3; //Direction Pin
const int stepPin = 2;  // Steps Pin
const int enPin   = 6;  //Enable Pin

const int limitPin1  =7; //LimitPin 1
const int limitPin2 =4; //LimitPin2
const int botonuno     = 8; //Button 1
const int botondos     = 9; //Button 2

I hope this was helpfull for you

vaj4088

Your limit switches (pin 4 and pin 7) do not need INPUT_PULLUP because they have both ground and power available. A pin mode of INPUT is sufficient but INPUT_PULLUP will also work with a very tiny increase in current.

Your push buttons require INPUT_PULLUP and a press is LOW because only ground is available and (I assume) the push button switches are normally open.

tinback123

Hi
So according to what you say this is my code so far:


Code: [Select]

#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, 8, 9, 10, 11);  //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

  // Defino los pines como salidas
  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 (button1 == LOW ) {
  digitalWrite(dirPin,HIGH);

  delay(5);
                      }

if (button2 ==LOW ) {
  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);
              }
  }

 



Im getting the following message... ( please see file attached). and I do not know what to do about it...


up until the " if (isForward || isBackward) { " am I in the correct path?


vaj4088

Good hing that I was on my laptop which can read JPEG files and not my mobile device which sometimes can't. 

If you used Tools | auto format you might see that your "if" statement and its following code are NOT in the loop() function.  ALL executable code (aside from some exceptions) must be inside a function and this code is not.

You  need to move the code (or move a squiggly bracket, depending upon what is easy for you and what your intent is).


tinback123

Great, now I'm not getting that error anymore

So this is my code so far:

Code: [Select]

#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, 8, 9, 10, 11);  //I need to change this pins, forget about this

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 everything works OK

if (button1 == HIGH ) {
  digitalWrite(dirPin,HIGH);

  delay(5);
                      }

if (button2 ==HIGH ) {
  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);
              }
}
}



Now, what I have to do is to program something that would do the following:

If button1 is pressed, the motor will move 300 steps CW. If button2 is pressed, the motor will move 300 steps CCW.

But, if button1 is pressed AND limitPin1 is pressed, the motor will stop (not necessarily simultaneous)
and, if button2 is pressed and limitPin2 is pressed, the motor will also stop. (not necessarily simultaneous)

Any thoughts/suggestions and/or modifications about this? ( I don't know for sure if this is OK...)

Code: [Select]

{
if (digitalRead(button1) == HIGH) ; //if buton1 is pressed
if (digitalRead(limitPin1) == LOW {}//if limitPin1 is pressed
else { // if it is not, then limitpin1 is not activated, rotate the motor CW

  digitalWrite(dirPin,HIGH);
  digitalWrite(stepPin,HIGH);

 if (digitalRead(button2) == HIGH) ; //if button2 is pressed
if (digitalRead(limitPin2) == LOW {} //if limitPin2 is not pressed
else { // if it is not, then limitpin2 is not activated, rotate the motor CW

  digitalWrite(dirPin,HIGH);
  digitalWrite(stepPin,HIGH);
  
}




I don't know if that's a code that will actually work

or if I could actually put the following, which is from a code that I found online:

Code: [Select]

bool p1ButtonPress()
{
  bool isPress=false;
  //comparar p1ButtonState to its previous state.
  if (p1buttonState != lastp1buttonState) {
    //if the state has changed, increment the counter
    if (p1buttonState ==LOW ) {
      //if the current state is HIGH then the button went from off to on:
      bPress  = true;
      isPress = true;

    } else {
      //if the current state is LOW then the button went from on to off:
      isForward = true;
    }

    //Delay a little bit
    delay(50);
  }
  //save the current state as the last state, for next time through the loop
  lastp1buttonState = p1buttonSate;
  return isPress;
}

bool p2ButtonPress()
{
  bool isPress = false;
  //compare the p1buttonState to its previous state
  if (p2buttonState != lastp2buttonState) {
    //if the state has changed, increment the counter
    if (p2buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      bPress = true;
      isPress = true;

    }else{
      //if the current state is LOW then the button went from on to off:
      isBackward = true;
    }
    //Delay a little bit to avoid bouncing
    delay(50);
  }
  //save the current state as the lat state, for next time through the loop
  lastp2buttonState=p2buttonState;
  return isPress;
}
    }
  }
}



That piece of code came actually from the code below, which works as I want to, but has no limitswitches what so ever....



Code: [Select]
// defines pins numbers
const int dirPin  = 3;
const int stepPin = 2;
const int enPin   = 6;
const int limitPin = 7;

const int switchOne     = 8;
const int switchTwo     = 9;

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;

void setup() {

  Serial.begin(9600);
  pinMode( switchOne, INPUT_PULLUP);
  pinMode( switchTwo, INPUT_PULLUP);
  pinMode(limitPin,INPUT);
  
  // 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(switchOne);
   p2buttonState = digitalRead(switchTwo);

  if (p1ButtonPress()) {

    digitalWrite(dirPin,HIGH);

    delay(5);
  }

    if (p2ButtonPress()) {

      digitalWrite(dirPin,LOW);

      delay(5);
    }

    if( isForward || isBackward ){

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

bool p1ButtonPress()
{
   bool isPress = false;
   // compare the p1buttonState to its previous state
  if (p1buttonState != lastp1buttonState) {
    // if the state has changed, increment the counter
    if (p1buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      bPress = true;
      isPress = true;
      Serial.println("Plaer One score");
    
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
      isForward = true;
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastp1buttonState = p1buttonState;
  return isPress;
}

bool p2ButtonPress()
{
   bool isPress = false;
   // compare the p1buttonState to its previous state
  if (p2buttonState != lastp2buttonState) {
    // if the state has changed, increment the counter
    if (p2buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      bPress = true;
      isPress = true;
      Serial.println("Plaer Two score");
    
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
      isBackward = true;
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastp2buttonState = p2buttonState;
  return isPress;
}



What do you think Vaj?


Go Up