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:warning: comparison between signed and unsigned integer expressions - Programming Questions - Arduino Forum.
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!