how to control a DC motor for two different speed

Hey there
I have a project in which I need to implement an asymetrical cycle(up faster, down slower and vice-versa)
for this am using an arduino uno, h bridge and a 12v dc motor. with a proportional controller code.
To trigger the change of speeds, I'm using a hall sensor that senses a magnet attached to a wheel on the motor. Everything works fine with just one speed.

I have made the following code for this asymmetrical cycle(two turns with two different speeds/ or half a turn with two different speeds)

But when I load it to the arduino nothing happens

#define encoder 2 
#define hallSensor 3                                           
#define enA 9                                                        
#define in1 6
#define in2 7

volatile long encoderPos = 0;
volatile byte hallValue = HIGH;
int interval = 100;
long prevMillis = 0;
long nowMillis = 0;
int pot;
long outP = 0;
int ref = 0;

void setup() {

TCCR2B = TCCR2B & 0b11111000 | 1;                                 
Serial.begin(57600);
pinMode (encoder, INPUT_PULLUP);
pinMode (hallSensor, INPUT_PULLUP);
pinMode (enA, OUTPUT);
pinMode (in1, OUTPUT);
pinMode (in2, OUTPUT);

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);


attachInterrupt(digitalPinToInterrupt(encoder), updateEncoder, RISING);
attachInterrupt(digitalPinToInterrupt(hallSensor), toggle, RISING),
prevMillis = millis();

}

void loop() {
  
nowMillis = millis();

if (nowMillis - prevMillis > interval){
  prevMillis = nowMillis;



if (hallValue == LOW){
      encoderPos = 0;
      }
 if (encoderPos < 40){
  int ref = 25;
    } else { int ref = 55;
  }  
   int erro = ref - encoderPos;
   int P = 20;
   int outP = erro * P;
   pwmOut(outP);
   encoderPos = 0;  
 Serial.print(encoderPos);
 Serial.print(" ");
 Serial.println(outP); 
  }
}
void pwmOut(int out) {                               
  if (out > 255) 
    out = 255;
  if (out < 0)
    out = 0;
    
   analogWrite(enA, out);                                                           
}
 
void updateEncoder (){
 encoderPos++;
  }
 void toggle (){
   hallValue != hallValue;
  }

P_asy_3.ino (1.6 KB)

Hello jahfari_deyvi,

++Karma; // For posting your code correctly on your first post.

You are defining multiple variables with the same name, for example you declare the global variable

int ref = 0;

Just above setup.

Then in loop you have:

 if (encoderPos < 40){
  int ref = 25;
    } else { int ref = 55;
  }

That's a new variable with the same name.
There are other instances of you doing this, but I leave you to find those. Note that this is a common mistake. The compiler allows it as it is perfectly legitimate to have 2 variables with the same name with different scopes, however, at best it is confusing. Remove the int from the example above.

long prevMillis = 0;
long nowMillis = 0;

millis is not signed but you are using a signed variable to hold it, this won't immediately cause a problem but will cause odd behaviour after about 23 days. My personal preference is to use uint32_t not unsigned long because with uint32_t it is clear exactly what you have.

Whether those things are the cause of your problems I don't know, but they need fixing before looking anywhere else.