Hello,
I am beginner in Arduino programming. I have this code below:
#define dirPin 12
#define stepPin 9
#define enPin 13
int pdd = 100;
void setup()
{
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(enPin, OUTPUT);
Serial.begin(9600);
digitalWrite(enPin, HIGH);
digitalWrite(dirPin, HIGH);
for(int x = 0; x < pdd; x++)
{
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
//delay(100);
}
void loop()
{
int pval = analogRead(A0);
int val = map (pval, 0, 1023, 0, 200);
Serial.println(val);
if (pdd>(val+10))
{
digitalWrite(enPin, HIGH);
digitalWrite(dirPin, HIGH);
for(int x = 0; x < (pdd-val); x++)
{
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
// delay(100);
}
else if (pdd<(val-10))
{
digitalWrite(enPin, HIGH);
digitalWrite(dirPin, LOW);
for(int x = 0; x < (val-pdd); x++)
{
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
// delay(100);
}
else (pdd == val);
{
digitalWrite(enPin, LOW);
}
}
In starting, on giving value manually in programming to rotate motor, then motor rotates.
Then, motor value checks with sensor value in IFs Statement, motor continue to rotate according to sensor value.
After this, I want that motor should stop for a while until the sensor value change.
But this is not happening, motor is continuously rotating without waiting for a sensor value to change.