[SOLVED] warning: comparison between signed and unsigned integer expressions

Hello,

I was coding for a sumorobot. My code is still in the process of completion. However, when I verified to see how it going, it worked perfectly fine but it gave two warning. I wanted how to fix it (and whether I should even be concerned in the first place as it verified without issue).

Below I have indicated the code, the “error” and the place where the “error” seems to occur. In addition, if it helps someone asked a similar question a couple of months ago on forums, but I did not understand the error. I have attached the link to that forum here:https://forum.arduino.cc/index.php?topic=403483.0.

This is my code.

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin 
#define CW  0 //Define the values for clockwise and counter clockwise 
#define CCW 1
#define MOTOR_A 0 // Define the values for the two motors 
#define MOTOR_B 1
const byte PWMA = 3;  // PWM control (speed) for motor A
const byte PWMB = 11; // PWM control (speed) for motor B
const byte DIRA = 12; // Direction control for motor A
const byte DIRB = 13; // Direction control for motor B
int inPin = 9;        // the number of the input pin
int outPin = 10;      // the number of the output pin
int state = HIGH;     // the current state of the output pin
int reading;          // the current reading from the input pin
int previous = LOW;   // the previous reading from the input pin
long time = 0;        // the last time the output pin was toggled
long debounce = 200;  // the debounce time, increase if the output flickers
int dist = 0;
int object = 2000;
int sensorPin = A0; // select the input pin for IDR
int sensorValue = 0; // variable to store the value coming from the sensor
int sensorMin = 800;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value
void setup() {
  Serial.begin (9600); // open communication
  pinMode(trigPin, OUTPUT); // set trigpin as an output
  pinMode(echoPin, INPUT);
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
  setupArdumoto(); // Set all pins as outputs
}

void loop() {
  reading = digitalRead(inPin);
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
    {
      state = LOW;
    }
    else {
      state = HIGH;
    }

    time = millis();
  }
  digitalWrite(outPin, state);

  previous = reading;
  if (dist <= object)
  { //if distance is equal to or less than 20cm prepare to charge forward at enemy's robot
    attack();//move forward and commence attacking by charging at enemy's robot robot
  }

  else if (dist > object)
  { //if distance is greater than 20cm then just turn left continuously
    turnleft();
  }
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen
  delay(100); // Delay 100 miliseconds to print values on screen
  if (sensorValue < 90) //if LDR dectect a value less than 90 move backward
  {
    backward();
  }
  else if (sensorValue = constrain(sensorValue, 91, 800)); //if LDR detects value in between 91 and 800 simply continue turning left
  {
    turnleft();
  }
}
void attack() {
  driveArdumoto(MOTOR_A, CW, 240);  // Motor A at 240/255.
  driveArdumoto(MOTOR_B, CW, 240);  // Motor B at 240/255.
}
void turnleft() {
  driveArdumoto (MOTOR_A, CW, 54); //Motor A does not run at all.
  driveArdumoto (MOTOR_B, CW, 128); //Motor B runs at half speed.
}
void backward() {
  driveArdumoto(MOTOR_A, CCW, 240); // Motor A goes backward at 240/255
  driveArdumoto(MOTOR_B, CCW, 240); //Motor B goes backward at 240/255
}
void driveArdumoto(byte motor, byte dir, byte spd)// dirve the motor in a certain direction at certain speed
{
  if (motor == MOTOR_A)
  {
    digitalWrite(DIRA, dir);
    analogWrite(PWMA, spd);
  }
  else if (motor == MOTOR_B)
  {
    digitalWrite(DIRB, dir);
    analogWrite(PWMB, spd);
  }
}

void setupArdumoto()
{
  pinMode(PWMA, OUTPUT);// All pins should be setup as outputs:
  pinMode(PWMB, OUTPUT);
  pinMode(DIRA, OUTPUT);
  pinMode(DIRB, OUTPUT);

  digitalWrite(PWMA, LOW); // Initialize all pins as low:
  digitalWrite(PWMB, LOW);
  digitalWrite(DIRA, LOW);
  digitalWrite(DIRB, LOW);
}

Here is the “error” that it is showing.

F:\Arduino\Sumo_Robot_Sketch\Sumo_Robot_Sketch.ino: In function 'void loop()':

F:\Arduino\Sumo_Robot_Sketch\Sumo_Robot_Sketch.ino:35:61: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

   if (reading == HIGH && previous == LOW && millis() - time > debounce) {

                                                             ^

F:\Arduino\Sumo_Robot_Sketch\Sumo_Robot_Sketch.ino:65:57: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

   else if (sensorValue = constrain(sensorValue, 91, 800));

                                                         ^

Here is the error found in the code

void loop() {
  reading = digitalRead(inPin);
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
    {
      state = LOW;
    }
    else {
      state = HIGH;
    }

    time = millis();
  }
  digitalWrite(outPin, state);

  previous = reading;



 else if (sensorValue = constrain(sensorValue, 91, 800)); //if LDR detects value in between 91 and 800 simply continue turning left
  {
    turnleft();
  }

Thank you for your help!

time should be unsigned long. "millis() - time" would be better as "(millis() - time)"

warnings aren't fatal but it's best to have none. making them go away will make you think about what youre doing and perhaps avoid a logic error.

parentheses are a good thing; classic example is 'x + y modulo z'

is that treated as "(x+y) modulo z" or "x + (y modulo z) "?

The problem is that you implicitly declared both 'time' and 'debounce' to be signed long integers:

long time = 0;
long debounce = 200;

The compiler is warning you that comparing a signed integer with an unsigned integer (from the millis() function) is not a good idea. Declare them to be unsigned.

unsigned long time = 0;
unsigned long debounce = 200;

In the long run, it is best to get used to declaring them in a way which also explicitly declares the specific size of the integer. For example, uint32_t specifies an unsigned 32-bit integer.

uint32_t time = 0;
uint32_t debounce = 200;

Pete

Thanks to everyone who helped. But how should I fix the code where the parentheses are needed. (I have placed the code where it requires parentheses down below).

else if (sensorValue = constrain(sensorValue, 91, 800)); //if LDR detects value in between 91 and 800 simply continue turning left
  {
    turnleft();
  }

Basically this code is indicating (or at least trying to) that if the LDR indicates a value between 91 and 800 the robot should continue to turn left.

Thanks again!

800));

Get rid d of the ;

I got rid of the ; and it was unnecessary (Thanks for your help on that!). But the error still seems to be there. Is there something else that I need to do?

else if (sensorValue = constrain(sensorValue, 91, 800)); //if LDR detects value in between 91 and 800 simply continue turning left
  {
    turnleft();
  }

"Basically this code is indicating (or at least trying to) that if the LDR indicates a value between 91 and 800 the robot should continue to turn left."

Don't get fancy, look at how the 'if' function syntax works.

https://www.arduino.cc/en/Reference/If

.

I looked at it and if I understood what it was saying, it is the fact that the if statements require a comparison. But then how I am I suppose to make the sensor detect a value between 91 and 800. I was using this Arduino link as reference https://www.arduino.cc/en/Reference/Constrain

int state = HIGH;     // the current state of the output pin
int reading;          // the current reading from the input pin
int previous = LOW;   // the previous reading from the input pin

I always declare variables that will only hold digital pin states as boolean.

boolean state = true;     // the current state of the output pin
boolean reading;          // the current reading from the input pin
boolean previous = false;   // the previous reading from the input pin

The newer IDEs will accept "bool" instead of"boolean".

ldr = constrain(sensorValue, 91, 800);
if(ldr >= 91 && ldr <= 800)
{
}

Thanks everyone for helping! I finally got it to work.