Stepper motor project

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

type or paste code here

#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);
    }
}
`type or paste code here`

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.

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

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.

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
1 Like

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

1 Like

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.

#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);
    }
}

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