kind of digital analog problem with potentiometer !! help ?

hello guys,
Arduino i am using is Arduino Duemilanove just for the record,
well now i almost finished my circuit about driving a motor, the mechanism is using a throttle and brakes

what i want to do is this :
when i throttle, i need the motor to get fast as long as i turn the pot more till 1023
and for the brake when i turn the braking pot i need the motor speed to get low as long as i turn till 1023 we can say like the real brakes as long as you push the friction gets high till the car stop

so what is happening is this :
when i turn the throttle pot nothing happened untill i reach the 1023 (seems like a digital signal here !!)
as for the brakes, when i start turning the brake pot, the speed of the motor gets down bit by bit till it stops (this one works same as i wanted it, perfect)

one more problem is : now i am using A0 pin for brake and A1 pin for throttle
but first i had A0 for the throttle and A1 for brakes but the A0 pin is ignoring what i am telling it to do in the codes and always acting like brake and A1 as throttle so i had to flip the values A1 for throttle and A0 for brakes !!

does anyone have any idea what might be going on here ?

here are my codes :

int enablePin = 11; //PWM
int in1Pin = 10; //PWM
int in2Pin = 9; //PWM

int potPin1 = A1;
int potPin2 = A0;

void setup()
{
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  
}

void loop()
{
  int throttle = analogRead(potPin1);
  throttle = map(throttle, 0, 1023, 255, 0);
  
  int brake = analogRead(potPin2);
  brake = map(brake, 0, 1023, 10, 0);
  
  setMotor(throttle, brake);
}

void setMotor(int throttle, int brake)
{
  int pedal ;
  if(throttle){
    pedal = throttle;
    analogWrite(enablePin, throttle);
  }else if (brake){
    pedal = brake;
    analogWrite(enablePin, brake);
  }
  
  digitalWrite(in1Pin, throttle);
  digitalWrite(in2Pin, brake);
}

Are you getting the values you expect from the throttle and brake inputs? (I don’t see how you could ever know without printing them.)

Do you actually get a zero value from the throttle input when the pedal is not being pressed? It seems unlikely to me. I’d expect to get some intermediate value, in which case your if(throttle) needs to be if(throttle < threshold). Similarly for the brake pedal.

If you’re using two inputs I suggest that the brake pedal should take precedence over the throttle for safety reasons; currently you do it the other way round.

I don’t know whether the analog and digital output calls you’re making in setMotor() do actually apply braking and power to the motor as you intend. I suggest you write a test sketch using a simple hard-coded sequence of acceleration and braking commands to confirm that works.

You’re mapping the brake input to a value in the range 0 - 10 and then using that as an analog output value - that only produces a very small range of braking effect. Why don’t you use 0-255 instead?

The approach of applying the throttle and brakes separately and simultaneously does not seem very sensible to me. In fact you seem to be setting the PWM output for throttle and brake values simultaneously, and that looks completely wrong to me and makes me think that you have got the use of digital and analog writes to the ‘enable’ and ‘direction’ controls the wrong eay round. I think it would be better to read and analyse the input controls to give you a demanded power/braking, for example by representing power as a positive value 1…255 and braking as a negative value in the range -1…-255. Here zero would mean coasting. Then you can test whether the demanded power/braking is positive or negative and set the output polarity and PWM value accordingly. This would just require a small change to setMotor() to accept a single argument.

PeterH:
Are you getting the values you expect from the throttle and brake inputs? (I don’t see how you could ever know without printing them.)

Do you actually get a zero value from the throttle input when the pedal is not being pressed? It seems unlikely to me. I’d expect to get some intermediate value, in which case your if(throttle) needs to be if(throttle < threshold). Similarly for the brake pedal.

If you’re using two inputs I suggest that the brake pedal should take precedence over the throttle for safety reasons; currently you do it the other way round.

I don’t know whether the analog and digital output calls you’re making in setMotor() do actually apply braking and power to the motor as you intend. I suggest you write a test sketch using a simple hard-coded sequence of acceleration and braking commands to confirm that works.

You’re mapping the brake input to a value in the range 0 - 10 and then using that as an analog output value - that only produces a very small range of braking effect. Why don’t you use 0-255 instead?

The approach of applying the throttle and brakes separately and simultaneously does not seem very sensible to me. In fact you seem to be setting the PWM output for throttle and brake values simultaneously, and that looks completely wrong to me and makes me think that you have got the use of digital and analog writes to the ‘enable’ and ‘direction’ controls the wrong eay round. I think it would be better to read and analyse the input controls to give you a demanded power/braking, for example by representing power as a positive value 1…255 and braking as a negative value in the range -1…-255. Here zero would mean coasting. Then you can test whether the demanded power/braking is positive or negative and set the output polarity and PWM value accordingly. This would just require a small change to setMotor() to accept a single argument.

ok about the 0-10 it’s because i don’t want the motor to go in reverse, all i want is to stop it, it works fine, my problem is that when the pot of throttle reach the end, when i turn it, then the motor start rotating which mean when the value is 255 on output, but if the value is less than 255 for example 200 the motor is not rotating :S this one is the opposit of what i want, because if the value is 100 then i want the motor to be rotating … not just when it’s 255 like a digital signal !
i will print values and then will post it here

ok here is a picture of the results
when i am not touching the potentiometers of throttle and brake the values that are printed are : 255 for throttle and 10 for brake and of course motor not running
when i turn the throttle pot value stays 255 and motor start running when i reached the pic of the pot

when i hit the brake the throttle value starts to get down bit by bit (which i want here )

codes for void function:

void loop()
{
  int throttle = analogRead(potPin1);
  throttle = map(throttle, 0, 1023, 255, 0);
  Serial.print("throttle value is : ");
  Serial.println(throttle);
  delay(500);
  
  int brake = analogRead(potPin2);
  brake = map(brake, 0, 1023, 10, 0);
  Serial.print("brake value is : ");
  Serial.println(brake);
  delay(500);
  
  setMotor(throttle, brake);
}

now i managed to make the throttle goes like i want but the brake are not !! which means i want the brake to work like before as long as i turn brakes making the speed slower untill it stops when i reach the last value of pot but now it's not, when i reach value 10 of pot the speed totally stops !!

this is the new loop :

void loop()
{
  int throttle = analogRead(potPin1);
  throttle = map(throttle, 0, 1023, 0, 255);
 // Serial.print("throttle value is : ");
  //Serial.println(throttle);
  //delay(500);
  
  int brake = analogRead(potPin2);
  brake = map(brake, 0, 1023, 0, 255);
  //Serial.print("brake value is : ");
  //Serial.println(brake);
  //delay(500);
  
  
  setMotor(throttle, brake);
}
 int throttle = analogRead(potPin1);
  throttle = map(throttle, 0, 1023, 0, 255);

is much simpler and quicker written int throttle = analogRead(potPin1) / 4;

AWOL:

 int throttle = analogRead(potPin1);

throttle = map(throttle, 0, 1023, 0, 255);



is much simpler and quicker written ` int throttle = analogRead(potPin1) / 4;`

your right but i keep it that way so i will always understand what's going on for now :slight_smile:

firashelou:
now i managed to make the throttle goes like i want but the brake are not !! which mean i want the brake to work like before as long as i turn brakes making the speed slower untill it stops when i reach the last value of pot but now it's not, when i reach value 10 of pot the speed totally stops !!

There may be a language barrier here, but I'm finding it very difficult to understand what behaviour you're trying to achieve.

I had assumed that the two inputs corresponded to brake and throttle pedal positions and you wanted power to be applied when the throttle pedal was operated and braking to be applied when the brake pedal was operated. But in this context, it doesn't make sense to expect "brakes making the speed slower untill it stops when i reach the last value of pot" - there is no direct relationship between the amount of braking and the speed.

PeterH:
There may be a language barrier here, but I'm finding it very difficult to understand what behaviour you're trying to achieve.

I had assumed that the two inputs corresponded to brake and throttle pedal positions and you wanted power to be applied when the throttle pedal was operated and braking to be applied when the brake pedal was operated. But in this context, it doesn't make sense to expect "brakes making the speed slower untill it stops when i reach the last value of pot" - there is no direct relationship between the amount of braking and the speed.

aha ok then when do you think there must be ? a relation in what way ?