I am trying to control a stepper motor's movements with a pressure sensor input (0 to 5 V digital). For example, if the input is 4 V then the stepper motor should move forward 4 steps. If the input is 3 V then it should come backward 1 step. Please suggest a command in Arduino to keep record of the previous sensor value and steps taken by stepper motor.
This is the code I have written for simple rotation of the motor:
const int stepPin = 8;
const int dirPin = 9;
int sensorValue=A0;
int i;
void setup() {
Serial.begin(9600);
pinMode(sensorValue, INPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
if(voltage==1) {
digitalWrite(dirPin, HIGH);
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(5000);
digitalWrite(stepPin, LOW);
delayMicroseconds(5000);
}
delay(1000); // One second delay
}
digitalWrite(dirPin, LOW);
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
while(1);//only once
}