Hi everybody
I'm using:
-Arduino UNO R3
- NEMA 23 19kg.cm 200 steps 1.8° stepper motor ( (datasheet)
-
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
)
Thanks for reading this

