# Stepper motor project

**URL:** <https://forum.arduino.cc/t/stepper-motor-project/1252713>\
**Category:** LEDs and Multiplexing\
**Created:** [April 27, 2024, 6:43am UTC](https://forum.arduino.cc/t/stepper-motor-project/1252713 "2024-04-27T06:43:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![josefc1991](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@josefc1991](https://forum.arduino.cc/u/josefc1991)\
**Post date:** [April 27, 2024, 6:43am UTC](https://forum.arduino.cc/t/stepper-motor-project/1252713/1 "2024-04-27T06:43:42Z")

</div>

Hello everybody, I am doing a project for a stepper motor that involves moving 288 degrees without using a limit or hall effect switch and the double bouncing feature for my cw button.... to make the motor scan till undouble bounced again. I have tried using  
const int numberOfSteps = 200 \* 0.80 to get me 288 degrees before the setup. Then input that into my loop for the void actONButtons but that was no success. Since I believe my dilemma is that the IF is reading so fast before the  
FOR (int x = 0; x \< numberOfSteps; n++)?  
EXAMPLE:  
if (buttonCWpressed == true) {  
digitalWrite(directionPin, LOW);  
digitalWrite(enablePin, LOW);  
for(int n = 0; n \< numberOfSteps; n++){  
Serial.println("CW ACT ON BUTTON");  
singleStep();  
}

So far i have gotten most of what i need done expect for the limitation of the 270 degrees and the double bounce feature. I have tried the examples and googled my double bounce but looking for examples that involves stepper motors. Any information to steer me in the right track would help. Im also open to criticism within my code I believe it is overwhelming for what it needs to do.

im not a veteran with arduino im a novice in class trying to gain a better fundamental of c++/arduino. thank you

 ![20240427_012620](https://europe1.discourse-cdn.com/arduino/original/4X/d/b/d/dbd600e61b620bb91c3fe8d2bb60c711e4a5f6c6.jpeg)

type or paste code here

```arduino

```

---

<div class="post-metadata">

**Author:** ![josefc1991](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@josefc1991](https://forum.arduino.cc/u/josefc1991)\
**Post date:** [April 27, 2024, 6:44am UTC](https://forum.arduino.cc/t/stepper-motor-project/1252713/2 "2024-04-27T06:44:22Z")

</div>

```arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
#include <Stepper.h>
#include <MobaTools.h>
#include <Servo.h>

///////MOTOR CONTROL 
byte directionPin = 3; //WHITE WIRE
byte stepPin = 4; //BLUE WIRE
byte enablePin = 5; //BLACK WIRE

const int numberOfSteps = 200 * 0.80; //288 DEGREEESISH

////////DIRECTIONAL CW AND CCW AND LIGHT CONTROL
byte ledPinGreen = 10; //CCW RED BLUE WIRE
byte ledPinRed = 11; //CW GREEN BLUE WIRE

int ledGreen = 0;
int ledRed = 0;
         

////////BUTTON CONTROL
byte buttonCWpin = 9; //RED WIRE
byte buttonCCWpin = 8; //RED WIRE

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
boolean bothbuttonPressed = false;

byte ledPin = 13;

////////LCD WORD BANK USED 
char array1[] = " BADBASSFISHING";
char array2[] = " FFS Stepper";
char array3[] = " Jose xxxx";
char array4[] = " xxxx PROJECt";
char array5[] = " System Running ";
char array6[] = "Motor Pos:";
char array7[] = " CW ";
char array8[] = "CCW ";
char array9[] = "Scanning";
char array10[] = "FFS/System Running";
char array11[] = "GPS info:";
char array12[] = "LAT:";
char array13[] = "LON:";
char array14[] = "Motor Off";
LiquidCrystal_I2C lcd(0x27, 20, 4);

//////// MILLI TIME USE TO ALLOW MULTIPLE FUNCTIONS
unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 20; // milliseconds

void setup() { 

Serial.begin(9600);
Serial.println("SYSTEM RUNNING");

////////MOTOR OUTPUTPINS
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(ledPin, OUTPUT);

/////////LIGHT GREEN CW AND RED LIGHT CCW
pinMode(ledPinGreen, OUTPUT); //cw 
pinMode(ledPinRed, OUTPUT); //ccw
     
pinMode(buttonCWpin, INPUT_PULLUP);
pinMode(buttonCCWpin, INPUT_PULLUP);
     
////////LCD
lcd.init();
lcd.backlight();

lcd.print(array1);

lcd.setCursor(0,1);
lcd.print(array2);

lcd.setCursor(0,2);
lcd.print(array3);

lcd.setCursor(0,3);
lcd.print(array4);
Serial.println("WORKING");
delay(6000);

////////LCD clear
lcd.clear();
Serial.println("LCD GETS CLEARED");

///////System Running
lcd.init();
lcd.backlight();
Serial.println("PROGRAM RUNNING");  
  
lcd.print(array10);

lcd.setCursor(0,1);
lcd.print(array6);

lcd.setCursor(0,2);
lcd.print(array11);

lcd.setCursor(0,3);
lcd.print(array12);

lcd.setCursor(10,3);
lcd.print(array13);

}

void loop() { 
    
    curMillis = millis();
    readButtons();
    actOnButtons();
    lightbuttonControl();
    
    
}

void readButtons() {
    
    buttonCCWpressed = false;
    buttonCWpressed = false;
    bothbuttonPressed = false;

    if (digitalRead(buttonCWpin) == LOW) {
        buttonCWpressed = true;
        Serial.println("CW READ BUTTON");
    }
    if (digitalRead(buttonCCWpin) == LOW) {
        buttonCCWpressed = true;
        Serial.println("CCW READ BUTTON");
    }
    if (digitalRead(buttonCCWpin) == LOW && digitalRead(buttonCWpin) == LOW) {
       bothbuttonPressed = true;
       Serial.println("STOP MF READ BUTTON");
    }

}

void actOnButtons() {
    if (buttonCWpressed == true) {
        digitalWrite(directionPin, LOW);
        digitalWrite(enablePin, LOW);
        Serial.println("CW ACT ON BUTTON");
        singleStep();
    }
    if (buttonCCWpressed == true) {
        digitalWrite(directionPin, HIGH);
        Serial.println("CCW ACT ON BUTTON");
        singleStep();
    }
    if (bothbuttonPressed == true) {
      digitalWrite(enablePin, HIGH);
      lcd.setCursor(10,1);
      lcd.print(array14);
      Serial.println("STOP MF ACT ON BUTTON");
      singleStep();
    }

}
    

 ///////////////////////
void lightbuttonControl() {

  if (buttonCWpressed == HIGH && buttonCCWpressed ==LOW){
    ledGreen = HIGH;
    ledRed = LOW;
    lcd.setCursor(10,1);
    lcd.print(array7);
    Serial.println("GREEN LIGHT");
  }

  else if (buttonCWpressed == LOW && buttonCCWpressed == HIGH){
    ledGreen = LOW;
    ledRed = HIGH;
    lcd.setCursor(10,1);
    lcd.print(array8);
    Serial.println("RED LIGHT");
  }

  else{
    ledGreen = LOW;
    ledRed = LOW;  
  }

  digitalWrite(ledPinGreen, ledGreen);
  digitalWrite(ledPinRed, ledRed);
}

void singleStep() {
    if (curMillis - prevStepMillis >= millisBetweenSteps) {
        prevStepMillis = curMillis;
        digitalWrite(stepPin, HIGH);
        digitalWrite(stepPin, LOW);
    }
}

```

```arduino
`type or paste code here`

```

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [April 27, 2024, 5:31pm UTC](https://forum.arduino.cc/t/stepper-motor-project/1252713/3 "2024-04-27T17:31:37Z")

</div>

> [@josefc1991](#):
>
> 288 degrees without using a limit or hall effect switch

288 from where? If you want 288 from 0, then you must "home" your motor position. That usually involves an encoder - as simple as a limit switch or hall sensor.

> [@josefc1991](#):
>
> undouble bounced

"De-bounce" - Check MobaTools example sketches. I am not certain, but it has a lot of timing utilities. @MicroBahner

---

<div class="post-metadata">

**Author:** ![josefc1991](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@josefc1991](https://forum.arduino.cc/u/josefc1991)\
**Post date:** [April 27, 2024, 6:02pm UTC](https://forum.arduino.cc/t/stepper-motor-project/1252713/4 "2024-04-27T18:02:26Z")

</div>

Are you talking millis? For the debounce? I found a couple last night that imma try out later today. And i wasnt homing it to begin with using robin 2 sketch 1 it would rotate the full 360. But i used the 200 x 0.75 somethint along the lines to get my 288. Where ever the stepper was at was its home...i was just looking for a general idea of how that sketch worked. Seems like the hall is a much easier option that settint an eternal code to limits the stepper angles.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [April 27, 2024, 8:19pm UTC](https://forum.arduino.cc/t/stepper-motor-project/1252713/5 "2024-04-27T20:19:15Z")

</div>

Here is an example of debouncing a button...

1. read the button
2. has the button changed states (HIGH to LOW or LOW to HIGH)
3. if yes, start a timer...
4. ... and store the new button state
5. if the timer has exceeded my chosen timeout (50 ms for me)...
6. ... check if the button STATE is in the NOT PRESSED state, and the button (not state) is the PRESSED (LOW)
7. end of debounce, call a function

> **[buttonChangeStatesUsingMillis - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/395928748514899969)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

---

<div class="post-metadata">

**Author:** ![MicroBahner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/microbahner/32/238923_2.png) [@MicroBahner](https://forum.arduino.cc/u/MicroBahner)\
**Post date:** [April 27, 2024, 8:38pm UTC](https://forum.arduino.cc/t/stepper-motor-project/1252713/6 "2024-04-27T20:38:42Z")

</div>

> [@xfpd](#):
>
> Check MobaTools example sketches. I am not certain, but it has a lot of timing utilities. @MicroBahner

Yes it has 😄. And can check buttons for short press, long press, single click, double click ...

---

<div class="post-metadata">

**Author:** ![josefc1991](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@josefc1991](https://forum.arduino.cc/u/josefc1991)\
**Post date:** [April 29, 2024, 4:25am UTC](https://forum.arduino.cc/t/stepper-motor-project/1252713/7 "2024-04-29T04:25:02Z")

</div>

whats up everyone had some downtime at work to mess with my stepper motor project. For the life of me I am lost on how to make the enable for the stepper to become unlatched if pressing the ccw button. EXAMPLE: I press the clockwise button till it hits the hall effect....halll effect turns the enable pin high causing the motor to stop...cool.....with that being said, when i press the ccw button it should unlatch the enable bit and go ccw and repeat etc etc....I been pounding my head on this for a bit and was looking for guidance on this. Yes i know i have not messed with the double bounce feature yet haha. One step at a time...thank you again everyone for your help and opinion.

---

<div class="post-metadata">

**Author:** ![josefc1991](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@josefc1991](https://forum.arduino.cc/u/josefc1991)\
**Post date:** [April 29, 2024, 4:25am UTC](https://forum.arduino.cc/t/stepper-motor-project/1252713/8 "2024-04-29T04:25:22Z")

</div>

```arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
#include <Stepper.h>
#include <MobaTools.h>
#include <Servo.h>

///////MOTOR CONTROL 
byte directionPin = 3; //WHITE WIRE
byte stepPin = 4; //BLUE WIRE
byte enablePin = 5; //BLACK WIRE

////////MOTOR LIMIT STOP
const int HallSensorB = 12; //BLUE WIRE
const int HallSensorA = 7; //VIOLET WIRE
const int ledPinBlue = 13; //RED WIRE
  

////////DIRECTIONAL CW AND CCW AND LIGHT CONTROL
byte ledPinGreen = 10; //CCW RED BLUE WIRE
byte ledPinRed = 11; //CW GREEN BLUE WIRE

int ledGreen = 0;
int ledRed = 0;
         

////////BUTTON CONTROL
byte buttonCWpin = 9; //RED WIRE
byte buttonCCWpin = 8; //RED WIRE

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
boolean bothbuttonPressed = false;

byte ledPin = 13;

////////LCD WORD BANK USED 
char array1[] = " BADBASSFISHING";
char array2[] = " FFS Stepper";
char array3[] = " Jose xxxx";
char array4[] = " xxxx PROJECt";
char array5[] = " System Running ";
char array6[] = "Motor Pos:";
char array7[] = " CW ";
char array8[] = "CCW ";
char array9[] = "Scanning";
char array10[] = "FFS/System Running";
char array11[] = "GPS info:";
char array12[] = "LAT:";
char array13[] = "LON:";
char array14[] = "Motor Off";
char array15[] = "Limt Reach";
LiquidCrystal_I2C lcd(0x27, 20, 4);

//////// MILLI TIME USE TO ALLOW MULTIPLE FUNCTIONS
unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 15; // milliseconds

void setup() { 

Serial.begin(9600);
Serial.println("SYSTEM RUNNING");

////////MOTOR OUTPUTPINS
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(ledPin, OUTPUT);

////////MOTOR LIMIT STOP
pinMode(HallSensorA, INPUT);
pinMode(HallSensorB, INPUT);

/////////LIGHT GREEN CW AND RED LIGHT CCW
pinMode(ledPinGreen, OUTPUT); //cw 
pinMode(ledPinRed, OUTPUT); //ccw
     
pinMode(buttonCWpin, INPUT_PULLUP);
pinMode(buttonCCWpin, INPUT_PULLUP);
     
////////LCD
lcd.init();
lcd.backlight();

lcd.print(array1);

lcd.setCursor(0,1);
lcd.print(array2);

lcd.setCursor(0,2);
lcd.print(array3);

lcd.setCursor(0,3);
lcd.print(array4);
Serial.println("WORKING");
delay(6000);

////////LCD clear
lcd.clear();
Serial.println("LCD GETS CLEARED");

///////System Running
lcd.init();
lcd.backlight();
Serial.println("PROGRAM RUNNING");  
  
lcd.print(array10);

lcd.setCursor(0,1);
lcd.print(array6);

lcd.setCursor(0,2);
lcd.print(array11);

lcd.setCursor(0,3);
lcd.print(array12);

lcd.setCursor(10,3);
lcd.print(array13);

}

void loop() { 
    
    curMillis = millis();
    readButtons();
    actOnButtons();
    lightbuttonControl();
    limitSwitch();
    
    
}

////////////////////////
void readButtons() {
    
    buttonCCWpressed = false;
    buttonCWpressed = false;
    bothbuttonPressed = false;

    if (digitalRead(buttonCWpin) == LOW) {
        buttonCWpressed = true;
        Serial.println("CW READ BUTTON");
    }
    if (digitalRead(buttonCCWpin) == LOW) {
        buttonCCWpressed = true;
        Serial.println("CCW READ BUTTON");
    }
    if (digitalRead(buttonCCWpin) == LOW && digitalRead(buttonCWpin) == LOW) {
       bothbuttonPressed = true;
       Serial.println("STOP READ BUTTON NO POWER TO MOTOR");
    }

}

/////////////////////////
void actOnButtons() {
    if (buttonCWpressed == true) {
        digitalWrite(directionPin, LOW);
        digitalWrite(enablePin, LOW);
        Serial.println("CW ACT ON BUTTON");
        singleStep();
    }
    if (buttonCCWpressed == true) {
        digitalWrite(directionPin, HIGH);
        Serial.println("CCW ACT ON BUTTON");
        singleStep();
    }
    if (bothbuttonPressed == true) {
      digitalWrite(enablePin, HIGH);
      lcd.setCursor(10,1);
      lcd.print(array14);
      Serial.println("STOP ACT ON BUTTON NO POWER TO MOTOR");
      singleStep();
    }

}

////////////////////////
void limitSwitch() {

  int hallSensorValue1 = digitalRead(HallSensorA);
  int hallSensorValue2 = digitalRead(HallSensorB);
  
  if (hallSensorValue1 == LOW) {
    digitalWrite(enablePin, HIGH);
    digitalWrite(ledPinBlue, HIGH);
    lcd.setCursor(10,1);
    lcd.print(array15);
    Serial.println("LIMIT REACHED A");
  }

  if (hallSensorValue2 == LOW) {
    digitalWrite(enablePin, HIGH);
    digitalWrite(ledPinBlue, HIGH);
    lcd.setCursor(10,1);
    lcd.print(array15);
    Serial.println("LIMIT REACHED B");
  }

  else{
    digitalWrite(ledPinBlue, LOW);
  }

}
    

 ///////////////////////
void lightbuttonControl() {

  if (buttonCWpressed == HIGH && buttonCCWpressed ==LOW){
    ledGreen = HIGH;
    ledRed = LOW;
    lcd.setCursor(10,1);
    lcd.print(array7);
    Serial.println("GREEN LIGHT");
  }

  else if (buttonCWpressed == LOW && buttonCCWpressed == HIGH){
    ledGreen = LOW;
    ledRed = HIGH;
    lcd.setCursor(10,1);
    lcd.print(array8);
    Serial.println("RED LIGHT");
  }

  else{
    ledGreen = LOW;
    ledRed = LOW;  
  }

  digitalWrite(ledPinGreen, ledGreen);
  digitalWrite(ledPinRed, ledRed);
}

//////////////////////
void singleStep() {
    if (curMillis - prevStepMillis >= millisBetweenSteps) {
        prevStepMillis = curMillis;
        digitalWrite(stepPin, HIGH);
        digitalWrite(stepPin, LOW);
    }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 26, 2024, 4:26am UTC](https://forum.arduino.cc/t/stepper-motor-project/1252713/9 "2024-10-26T04:26:07Z")

</div>

Closed on October 26, 2024, 4:26am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
