debugging help needed

Hi folks,
I need a quick hand with this code block.
For some reason when lmSpeed drops below 120, sendSpeed sticks at 255 no matter what.
When lmSpeed is greater that 130, the code works as expected.

Thanks in advance. Here's the code:

void loop() {
  sendSpeed = 0;
  aRead = 0;
  lmSpeed = 0;
  aRead = analogRead(A1);
  aRead = constrain(aRead, 0, 1023);
  lmSpeed = map(aRead,0,1023, 0, 255);
  lmSpeed = constrain(lmSpeed, 0, 255);

  //working
  //stop
  if (lmSpeed >> 120 && lmSpeed << 130) {
    drive(0, 255, 0);
  }

  //working
  //forward
  if (lmSpeed >> 130){ 
      sendSpeed = map(lmSpeed, 130, 255, 255, 0);
      sendSpeed = constrain(sendSpeed, 0, 255);
      drive(0, sendSpeed, 0); //left motor, motor speed, forward
  } 
  
  //not working
  //reverse
  if (lmSpeed << 120){
    sendSpeed = map(lmSpeed, 0, 120, 0, 255);
    sendSpeed = constrain(sendSpeed, 0, 255);
    sendSpeed = 255 - sendSpeed;
    drive(0,sendSpeed,1);
  } 
    
  Serial.println(aRead);
  Serial.println(lmSpeed);
  Serial.println(sendSpeed);
  delay(2000);
}

I'd prefer to see all the code.

lmSpeed >> 120 && lmSpeed << 130

Or maybe not

Did you really intend to use bit shifting operators here?

  if (lmSpeed >> 120 && lmSpeed << 130)

I assume that was supposed to be:

  if (lmSpeed > 120 && lmSpeed < 130)

Apart from the > vs >> thing (which I guess you did by analogy to = vs == ?), is it correct to have sendSpeed =0 inside loop()?

JimboZA, Thanks for the reply. Should be fine because it is just resetting the variables at the beginning of each loop.

But why?
You're just going to assign them in the next few instructions.

wildbill:
Did you really intend to use bit shifting operators here?

  if (lmSpeed >> 120 && lmSpeed << 130)

I assume that was supposed to be:

  if (lmSpeed > 120 && lmSpeed < 130)

Thanks for the catch. Unfortunately, this didn’t resolve the issue though.

AWOL:
I’d prefer to see all the code.

lmSpeed >> 120 && lmSpeed << 130

Or maybe not

Here’s the full code:

const int leftMotorEn = 4;
const int leftMotorLpwm = 5;
const int leftMotorRpwm = 6;

const int rightMotorEn = 8;
const int rightMotorLpwm = 9;
const int rightMotorRpwm = 10;

int vt;
int rmSpeed;
int lmSpeed;
int aRead;
int sendSpeed;

void setup() {                
  Serial.begin(9600);
  pinMode(leftMotorLpwm, OUTPUT); 
  pinMode(leftMotorRpwm, OUTPUT);  
  pinMode(leftMotorEn, OUTPUT);
  pinMode(rightMotorLpwm, OUTPUT); 
  pinMode(rightMotorRpwm, OUTPUT);  
  pinMode(rightMotorEn, OUTPUT);
}

void loop() {
sendSpeed = 0;
aRead = 0;

aRead = analogRead(A1);
aRead = constrain(aRead, 0, 1023);

//Serial.println(aRead);

lmSpeed = map(aRead,0,1023, 0, 255);
lmSpeed = constrain(lmSpeed, 0, 255);
//Serial.println(lmSpeed);
    //working
    //stop
    if (lmSpeed > 120 && lmSpeed < 130) {
      drive(0, 255, 0);
    }

    //working
    //forward
    if (lmSpeed >> 130){ 
        sendSpeed = map(lmSpeed, 130, 255, 255, 0);
        sendSpeed = constrain(sendSpeed, 0, 255);
        drive(0, sendSpeed, 0); //left motor, motor speed, forward
    } 
    //not working
    //reverse
    if (lmSpeed << 120){
      sendSpeed = map(lmSpeed, 0, 120, 0, 255);
      sendSpeed = constrain(sendSpeed, 0, 255);
      sendSpeed = 255 - sendSpeed;
      drive(0, sendSpeed,1);
    } 
    
    Serial.println(aRead);
    Serial.println(lmSpeed);
    Serial.println(sendSpeed);
    
    
    delay(2000);       //for 200ms


}


void drive(int motorSel, int pwm, int reverse){
  //motorSel - pass 0 for left motor, 1 for right
  //pwm - the speed (0 max, 255 stop)
  //dir - direction of motor (0 forward, 1 reverse)
  
  int lpwmPin;  //used for pin number storage
  int rpwmPin;  //used for pin number storage

   if(motorSel==0) {
    lpwmPin = leftMotorLpwm;
    rpwmPin = leftMotorRpwm;
    digitalWrite(leftMotorEn, HIGH);
  }

  if(motorSel==1){  //if right motor is selected
    lpwmPin = rightMotorLpwm;
    rpwmPin = rightMotorRpwm;
    digitalWrite(rightMotorEn, HIGH);
  } 
  if (reverse == 1){  //dir = 1 for reverse, dir = 0 for forward
    analogWrite(rpwmPin, pwm);
    analogWrite(lpwmPin, 255);
  } 
 if (reverse == 0) {
    analogWrite(lpwmPin, pwm);
    analogWrite(rpwmPin, 255);
  }
}

void disableAll(){
  digitalWrite(leftMotorEn, LOW);
  digitalWrite(rightMotorEn, LOW);
}

AWOL:
But why?
You're just going to assign them in the next few instructions.

They were just added as part of my debugging. They won't stay, but they are also not causing the issue.

lmSpeed >> 130

Fixed?
Right.

AWOL:

lmSpeed >> 130

Fixed?
Right.

Darnit - I missed one. Thank you VERY much. It is working now. :slight_smile:
Arduino forum is the BEST.

I missed one.

There were two :wink:

Darnit - I missed one.

You can’t count, either.

    if (lmSpeed >> 130){ 
        sendSpeed = map(lmSpeed, 130, 255, 255, 0);
        sendSpeed = constrain(sendSpeed, 0, 255);
        drive(0, sendSpeed, 0); //left motor, motor speed, forward
    } 
    //not working
    //reverse
    if (lmSpeed << 120){