Hello Everyone,
I'm here asking for help for my project, what I'm facing now is, is don't know how to make my stepper motor stop in a certain direction, Here is what I want my project to work,
When there is no force detected stepper motor should not move,
when there is force detected the stepper motor should spin in a clockwise direction and stop at a certain step until when the force is removed then the stepper motor will spin in a counter-clockwise direction and stop at a certain step.
From now what I manage is, manage to have the force sensor when there is force it will move in a clockwise direction and when the force is removed it will turn counter clock-wise, but it did not stop in a certain direction.
Below is my code.
#define dirPin 5 #define stepPin 2 #define stepsPerRevolution 1600 int fsrAnalogPin = 0; int fsrReading; void setup() { // Declare pins as output: pinMode(fsrReading, INPUT); pinMode(stepPin, OUTPUT); pinMode(dirPin, OUTPUT); } void loop() { Serial.begin(9600); fsrReading = analogRead(fsrAnalogPin); Serial.print("Analog reading = "); Serial.print(fsrReading); if(fsrReading > 200){ digitalWrite(dirPin, LOW); // Spin the stepper motor 1 revolution slowly: for (int i = 0; i < 500; i++) { // These four lines result in 1 step: digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); } delay(2000); } else if(fsrReading < 200){ digitalWrite(dirPin, HIGH); // Spin the stepper motor 1 revolution quickly: for (int i = 0; i < 500; i++) { // These four lines result in 1 step: digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); } delay(2000); } }
Message was edited by: Christopher Stanton - code formatting