Hi!
I bought a Arduino stepper motor from Jaycar in Australia and I can't seem to make it turn anticlockwise. I am using it for a school project and need some answers by the 19th of June. It is supposed to once the ultrasonic sensor senses something turn clockwise and then anticlockwise. The stepper motor has rainbow wires and is connected to what I think is called a controller which you then use to connect it to the Arduino
but I have a breadboard in between first.
Here is my code:
#include <Stepper.h>
const int stepsPerRevolution = 120; //RPM
int x; // int
Stepper myStepper(stepsPerRevolution, 13, 12, 11, 10);
const int trigPin = 9;
const int echoPin = 8; //Where I connected all the pins on the arduino
// defines variables
long duration;
int distance;
int safetyDistance;
int previous = 0;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
myStepper.setSpeed(120);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
safetyDistance = distance;
if (safetyDistance <= 20 ) {
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
myStepper.step(stepsPerRevolution);
myStepper.step(stepsPerRevolution);
myStepper.step(stepsPerRevolution);
delay(1000);
Serial.println("anticlockwise");
myStepper.step(stepsPerRevolution);
myStepper.step(stepsPerRevolution);
myStepper.step(stepsPerRevolution);
myStepper.step(stepsPerRevolution);
}
}
Please!!!!!! Help
Thanks In Advance!