Constrain not really working

Hello guys... here is the thing :)

I have this part of the code

void go_hard(int speedLeft, int speedRight)
{
  speedLeft = constrain(speedLeft, -254, 254);
  speedRight = constrain(speedRight, -254, 254);

  if (speedLeft > 0)
  {
    digitalWrite(12, LOW);
    analogWrite(mleft, speedLeft);
  }
  else
  {
    digitalWrite(12, HIGH);
    analogWrite(mleft, -speedLeft);
  }


  if (speedRight > 0)
  {
    digitalWrite(13, HIGH);
    analogWrite(mright, speedRight);
  }
  else
  {
    digitalWrite(13, LOW);
    analogWrite(mright, -speedRight);
   }
}

 

it just doesn't give me these set values... sometimes it jumps even over 800 or under -600

 

I have a qtra.readLine made via QTRSensors...

 

for example

position = qtra.ReadLine(6);

this should return values between 0 and 5000;

now my target is 2500

so position = position - 2500;

and than scale by 500;

i should get values between -5 and 5... but well you see... sometimes I got readings of - 50 and 50..

now the problem is that this is bad, because, i work with signed everywhere... possible that, when this maked a calculation will hit some

 

anyway I think I will do the trick maybe other way...

 

if you have ideea why I got sometimes this values... please help...

 

 

 

got it :slight_smile: had a problem

got it :slight_smile:

 

had a problem with the reading as readLine is unsigned…

 

int position = readLine(6);

int proportional = position;

proportional = proportional/500;