Coding help

My speed control is not working properly, I am not such were I am going wrong

I have potentiometer to control the speed of my dc motor, when I try to adjust the speed will not change. When I restart the arduino the speed will change one time it will be slow, next time it will be faster and etc.

  const int Push_Button = 2;
    const int Limit_S_1 = 4; 
    const int Limit_S_2 = 3; 

    int sensorPin = A0;
    int sensorValue_1 = 0;
    int sensorValue_2 = 0;
    //---------------------------------------------
    const int A = 10; // motor direction pins
    const int B = 11; //motor direction pins
    int motor_pwm = 9 ; // motor pwm pin
    //---------------------------------------------

    int Speed=150;


    void setup()
    {
        pinMode(Push_Button, INPUT);
        pinMode(Limit_S_1, INPUT);
        pinMode(Limit_S_2, INPUT);
       
        pinMode(A, OUTPUT);
        pinMode(B, OUTPUT);

         sensorValue_1 = analogRead(sensorPin);
    }
    void loop()
    {
       
      if(digitalRead(Push_Button) == HIGH)
      {
        forward();
      }
     
         
    }
    void forward()
    {
      while(1)
     {
       sensorValue_2 = analogRead(sensorPin);
     
      if(sensorValue_1 < sensorValue_2)
      {
        Speed=Speed+10;
        sensorValue_1=sensorValue_2;
      } 
         if(sensorValue_1 > sensorValue_2)
      {
        Speed=Speed-10;
        sensorValue_1=sensorValue_2;
      }
     
      if (Speed>255){Speed=255;}
      if (Speed<1){Speed=0;}
       
       
      digitalWrite(A, HIGH);
      digitalWrite(B, LOW);
      analogWrite(motor_pwm,Speed);
     
       if(digitalRead(Limit_S_1) == HIGH)
      {
        backward();
      } 

     }
    }
    void backward()
    {
      while(1)
      {
      sensorValue_2 = analogRead(sensorPin);
     
      if(sensorValue_1 < sensorValue_2)
      {
        Speed=Speed+10;
        sensorValue_1=sensorValue_2;
      } 
         if(sensorValue_1 > sensorValue_2)
      {
        Speed=Speed-10;
        sensorValue_1=sensorValue_2;
      }
      if (Speed>255){Speed=255;}
      if (Speed<1){Speed=0;}
     
      digitalWrite(B, HIGH);
      digitalWrite(A, LOW);
      analogWrite(motor_pwm,Speed);
       
        if(digitalRead(Limit_S_2) == HIGH)
      {
        STOP();
      }
     
      }
    }
    void STOP()
    {
      digitalWrite(B, HIGH);
      digitalWrite(A, HIGH);
      analogWrite(motor_pwm,0);
      loop();
    }

You seem to be missing something here,

pinMode(Push_Button, INPUT); pinMode(Limit_S_1, INPUT); pinMode(Limit_S_2, INPUT);

pinMode(A, OUTPUT); pinMode(B, OUTPUT);

And do you plan on changing the pin at any time?

[u]const[/u] int A = 10; // motor direction pins [u]const[/u] int B = 11; //motor direction pins [u] [/u] int motor_pwm = 9 ; // motor pwm pin

By the way, WHY?

void loop() // This loops forever {

if(digitalRead(Push_Button) == HIGH) ** {** ** forward();** ** }**

} void forward() { while(1) // Why do you need this? { . . .

One last thing, WAY too much white space.

Hi! HazardsMind said it all. Just fix those things up, and it should be alright.

You have another problem with your code. You keep calling your functions in a loop, and never returning from them. This will eventually use up your memory as it fills with the return addresses.

KevinAnderson:
You have another problem with your code. You keep calling your functions in a loop, and never returning from them. This will eventually use up your memory as it fills with the return addresses.

in loop you call forward
in forward you call backward
in backward you call stop
in stop you call loop
in loop you call forward
in forward you call backward
in backward you call stop
in stop you call loop
in loop you call forward
in forward you call backward
in backward you call stop
in stop you call loop

etc until the stack overflows

Try something like this as a base for your logic…

enum possibleStates {
  idle,
  changingForward,
  forward,
  changingBackward,
  backward,
  stopping
};

possibleStates state=idle;

void setup(){
  //set pinmodes etc
}

void loop(){
  switch (state){
    case idle:
      //put commands here - maybe check that push_button and if its high set state=changingForward
      break;
    case changingForward:
      //put commands here - start moving forward and set state=forward
      break;
    case forward:
      //put commands here - change to state=changingBackward if at full swing
      //and commands that accelerate or decelerate
      break;
    case changingBackward:
      //put commands here - start moving backwards and set state=backwards
      break;
    case backward:
      //put commands here change to set state=stopping if at full swing
     // and commands to accelerate or decelerate
      break;
    case stopping:
      //put commands here to stop and set state=idle
      break;
    default:
      Serial.println("unknown state");
  }
}
 sensorValue_2 = analogRead(sensorPin);
     
      if(sensorValue_1 < sensorValue_2)
      {
        Speed=Speed+10;
        sensorValue_1=sensorValue_2;
      } 
         if(sensorValue_1 > sensorValue_2)
      {
        Speed=Speed-10;
        sensorValue_1=sensorValue_2;
      }

Hi, what are you doing here, all you should be doing is reading the sensorvalue at analog pin and converting it to speed, why are you adding and subtracting speed when the pot output is doing this for you?

Try using this bit of code.

sensorValue = analogRead(sensorPin);                  // reads the input from the pot, 0 - 1023
Speed=map( sensorValue, 0 , 1023, 0 ,255);                     // maps the sensorValue to Speed
analogWrite(motor_pwm,Speed);                                     // outputs Speed 0 -255 to motor.

Hope this helps…

Tom… :slight_smile: