I am using unipolar 1.8 degree stepper motor. I have written code to rotate stepper motor i.e. when A0 pin becomes HIGH it should rotate in clockwise and when A1 pin becomes HIGH it should move anti clockwise but motor is rotating only once when I am making A0/A1 HIGH and then stops. After that it is not rotating even if i am making A0, A1 pin high. I am very new to this please tell how to solve this problem. I am using microstepping drive, nema 17 stepper motor and Arduino Uno board.
#include <Stepper.h>
#define STEPS_PER_MOTOR_REVOLUTION 32
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 200 //2048
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 9, 8);
int Steps2Take;
int buttonApin = A0;
int buttonBpin = A1;
int dirpin = 8;
int steppin = 9;
void setup()
{
pinMode(buttonApin, INPUT);
pinMode(buttonApin, INPUT);
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
small_stepper.setSpeed(1900);
}
void loop()
{
int valA = digitalRead(buttonApin);
if (valA == HIGH)
{ digitalWrite(dirpin, HIGH);
for (int x = 0; x < 100; x++)
{ digitalWrite(steppin, HIGH);
delayMicroseconds(1000);
digitalWrite(steppin, LOW);
delayMicroseconds(1000);
}
}
int valB = digitalRead(buttonBpin);
if (valB == HIGH)
{
digitalWrite(dirpin, LOW);
for (int x = 0; x < 100; x++)
{
digitalWrite(steppin, LOW);
delayMicroseconds(1000);
digitalWrite(steppin, HIGH);
delayMicroseconds(1000);
}
}
}
-
1Split the problem up, check you button code is working by using them to light a LED. Then get your motor spinning. (Any reason why you are using the Analog Pins?)Code Gorilla– Code Gorilla2017年02月23日 13:11:26 +00:00Commented Feb 23, 2017 at 13:11
-
I think yo need to read the buttons with analogRead() as you have connected them to A0 and A1 pins of arduino (A0 & A1 pins are analog ).Sonali_B– Sonali_B2017年12月12日 10:34:57 +00:00Commented Dec 12, 2017 at 10:34
-
how many wires does the motor use?jsotola– jsotola2017年12月13日 00:22:17 +00:00Commented Dec 13, 2017 at 0:22
-
your code looks like it is written to drive a regular motorjsotola– jsotola2017年12月13日 00:23:23 +00:00Commented Dec 13, 2017 at 0:23
1 Answer 1
I have mentioned in the comments that you should read the buttons with analogRead()
. But you can do it other way by simply connecting the button pin to digital inputs on arduino(any from 0-13). Analog pins are mostly used for accessing the sensor data as far as I know. I am correcting your code as below:
#include <Stepper.h>
#define STEPS_PER_MOTOR_REVOLUTION 32
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 200 //2048
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 9, 8);
int Steps2Take;
int buttonApin = 4; //connect button to digital pin 4 on arduino
int buttonBpin = 5; //connect button to digital pin 5 on arduino
int dirpin = 8;
int steppin = 9;
void setup()
{
pinMode(buttonApin, INPUT);
pinMode(buttonApin, INPUT);
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
small_stepper.setSpeed(1900);
}
void loop()
{
int valA = digitalRead(buttonApin);
if (valA == HIGH)
{ digitalWrite(dirpin, HIGH);
for (int x = 0; x < 100; x++)
{ digitalWrite(steppin, HIGH);
delayMicroseconds(1000);
digitalWrite(steppin, LOW);
delayMicroseconds(1000);
}
}
int valB = digitalRead(buttonBpin);
if (valB == HIGH)
{
digitalWrite(dirpin, LOW);
for (int x = 0; x < 100; x++)
{
digitalWrite(steppin, LOW);
delayMicroseconds(1000);
digitalWrite(steppin, HIGH);
delayMicroseconds(1000);
}
}
}