Question about controlling stepper motor with high low voltage

Hi all,

The prepose of this code is to connect the stepper motor to a digital pin so that when I select high voltage, the stepper motor will run clockwise once only and stop until I switch the voltage to low and it will run counter clockwise once and stop.

In my current code my motor would run one direction continuously (looping the same command until I switch to low/high and it just continue to run until i switch again).

My question is: Is there anyway to make it run once only when I switch the state . That is including when the board just plugged in, it should not do anything until i switch voltage.

Thanks!

Here is my code

#include <Stepper.h>
#include <EEPROM.h>

int Pin_Digital_Input = 52;
int digitalState = 0;
int distanceCountAbs = 0;
const int stepsPerRevolution = 1600;

Stepper myStepper(stepsPerRevolution, 22, 24);

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

pinMode(Pin_Digital_Input, INPUT);

}

void loop() {
//read the input at the digital input pin
digitalState = digitalRead(Pin_Digital_Input);

//read the input on analog pin 0;
int sensorValue = analogRead(52);

//Convert the analog reading(0-1023)
float voltage = sensorValue * (5.0/1023.0);

if(digitalState == 1) {
  distanceCountAbs = (EEPROM.read(2)-2)/.015875;
  myStepper.setSpeed(EEPROM.read(1)); /////this will base on the user input from LCD and converted to step/s
  myStepper.step(distanceCountAbs); /////set to user input divided .015875mm/micro step
  myStepper.setSpeed(10);
  myStepper.step(2/.015875);
  delay (500);
}

if(digitalStat2 == 0){
myStepper.setSpeed(10); 
myStepper.step(-2/.015875);
myStepper.setSpeed(EEPROM.read(1)); /////this will base on the user input from LCD and converted to step/s
myStepper.step(-distanceCountAbs); //// return to home position
delay (500);
}

}

looks like for one condition, digitalState == 1, it moves to distanceCountAbs, but call it -2/0.015875 if the EEPROM value is 0 and then moves to 2/0.015875

in the other condition, it looks like it in the opposite directions but the values are the same.

why not just go to distanceCountAbs in the one condition and -distanceCountAbs in the other condition

  • do you need to read the EEPROM, i don't see any writes.
  • buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

why not (untested)

#include <Stepper.h>

int         Pin_Digital_Input  = 52;
const int   stepsPerRevolution = 1600;
Stepper     myStepper (stepsPerRevolution, 22, 24);

void loop ()
{
    if (HIGH == digitalRead (Pin_Digital_Input))
        myStepper.step (2/.015875);

    else
        myStepper.step (-2/.015875);
}

void setup () {
    Serial.begin (9600);

    pinMode (Pin_Digital_Input, INPUT_PULLUP);
    myStepper.setSpeed (10);
}
1 Like

the EEPROM(0) is saved with a value, I am using that to recall that value saved in the Arduino board. the write happened with another code that I have used previously hance only the call in this code.
thank for the suggestion! I will definitely try it out!

Just checked out the State Change example and I think that's exactly what I need! How about to have the motor commands only run once after the state changed?

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