I had a servo motor toggle that was working when you pushed the button it rotated 180*, then if you pushed it again it would rotate 180* back the other direction.
I took this working servo motor code and modified it to control a stepper motor instead, and there is a problem in my loop where its just running the stepper motor non stop as soon as it boots up.
- Can you see where I made a mistake? Here is my code:
- I've tried experimenting with getting the stepper motor to go the opposite way (ex: motor.step(-800)), however the stepper motor always seems to rotate clockwise.
#include <Stepper.h>
int button = 2; //button pin, connect to ground to move servo
int press = 0;
boolean toggle = false;
Stepper motor(200, 8,9,10,11);
void setup() {
pinMode(button, INPUT); //arduino monitor pin state
Serial.begin(9600);
}
void loop() {
int Speed = analogRead(A0);
int RPM = map(Speed, 0, 1023, 0, 200);
press = digitalRead(button);
if (press == LOW)
{
if(toggle)
{
motor.step(800);
motor.setSpeed(RPM);
toggle = !toggle;
}
else
{
motor.step(-800);
motor.setSpeed(RPM);
toggle = !toggle;
}
}
Serial.println(RPM);
}
For my setup:
- Arduino Uno
- 5-wire stepper motor (http://www.amazon.com/gp/product/B00BOM5A2Q/ref=oh_details_o02_s00_i02?ie=UTF8&psc=1) plugged into pins 8,9,10,11
- button on pin 2