Control stepper motor by rotary potentiometer sensor

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.

Welcome to the forum

Welcome to the world of noise. Your value pdd and val are unlikely to be the same for a long time. The A/D converter value will always change a little bit because of noise in your system even if the signal is constant.

The code in this Thread looks very similar to the code in this other Thread where there have already been a number of Replies.

Are you the same person with two Forum identities?

...R

Robin2:
The code in this Thread looks very similar to the code in this other Thread where there have already been a number of Replies.

Are you the same person with two Forum identities?

...R

We, two students doing project on same work.

apard204:
We, two students doing project on same work.

It will make it much easier to help if the two of you just use the other Thread so all the information is in one place.

...R

Robin2:
It will make it much easier to help if the two of you just use the other Thread so all the information is in one place.

...R

ok
Thanks

Please continue this discussion in Control stepper motor by compare two input data. - Programming Questions - Arduino Forum.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.