C++ forbids comparison between pointer and integer.

Not sure what I am doing wrong as I have looked at multiple Google solutions, but they aren’t the same type of problem as mine.

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
  Servo myservo2;              // twelve servo objects can be created on most boards
 
 char val;
int pos = 0;    // variable to store the servo position 

 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
 myservo2.attach(8);
   Serial.begin(9600); // Start serial communication at 9600 bps

} 
 
void loop() 
{ 
  
     if (Serial.available()) 
   { // If data is available to read,
     val = Serial.read(); // read it and store it in val
   }
   if(val == "1")
   {
    myservo2.write(0);
    delay(1000);
   myservo.write(180); 
     
   }
   
   
      if(val=="2")
   {
   myservo.write(75); 
   delay(1000);
   myservo2.write(100); //gun
     
   }
   
   //Writing co-ordinates to gun
   if (val != "1" | val != "2")
   {
     myservo2.write(val); //gun
     
   }
 

}

Error:

Caet.ino: In function ‘void loop()’:
Caet.ino:34:14: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
Caet.ino:43:15: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
Caet.ino:52:15: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
Caet.ino:52:28: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
Error compiling.

if(val == '1')
not
if(val == "1")

Moving on a bit:

   //Writing co-ordinates to gun
   if (val != "1" | val != "2")

If val is equal to 1, it won't be equal to 2, right? So the condition is true.

If val is equal to 2, it won't be equal to 1, right? So the condition is true.

If val is neither 1 nor 2, the condition is also true.

So, the condition is true all the time, in which case you may as well not have the "if" in the first place.

     if (Serial.available()) 
   { // If data is available to read,
     val = Serial.read(); // read it and store it in val
   }
   if(val == "1")

Plus, you are doing the "if val == 1" stuff all the time, whether or not you got data from Serial.

Servo myservo;  // create servo object to control a servo 
  Servo myservo2;              // twelve servo objects can be created on most boards

Do these comments add any value?

What do the servos actually do? Don't names like wristServo and elbowServo make more sense, if the servos control the knee and ankle?

Who else's servos would you be using? You wouldn't even consider naming the instances myFriendAlexandersServo and myFriendAlexandersServo2, would you?

By the way, your choice of values makes no sense, either. Why would you want to have to have a cheat sheet to know what to type?

If you have two servos that control left and right movement and up and down movement, sending 'r' and 'l', for right and left, and 'u' and 'd' for up and down makes more sense than sending '1', '2', '5', and '6'.

Write your code in such a way that, two weeks from now, how it works is still perfectly obvious. Choose data so that, two weeks from now, what to send to control the device is still perfectly obvious.

twirap:
Not sure what I am doing wrong

You mix up single characters with a string.

In the C programming language a character is always included in single quotes, so ‘1’ or ‘2’ are characters.

But strings are included in double quotes so “Hello” and “1” and “2” are strings.

If you declare and read a character:

char val;
if (Serial.available()) 
{ // If data is available to read,
   val = Serial.read(); // read it and store it in val
 }

You can compare characters (included in single quotes) only.

So a valid comparison would be:

   if(val == '1')  // <-- single quotes!!!

I truly apologize for writing shitty code. The comments are trash because this code was meant to be written to test a program and then that program wouldn't be used every again by me. I understand that I should do proper commenting so if I want to reuse the code in future then I know what is going on.

Instead of ==1 or ==2 I did try to do "u" "d" but I got the same error with the comparison, so I thought maybe I have to make it all numbers rather than single characters. Now that I know I should be using ' instead of " I will replace it with 'u' and 'd' instead.

A problem is that the data coming from serial will not just be 'u' or 'd' but also numbers like '127' or '243' so would I have to change anything in my code on Arduino side to not run into anymore errors?

I will always be getting data from Serial because Processing is shooting co-ordinates of an object on my web-cam to Arduino, so the motors can react to it.

twirap:
A problem is that the data coming from serial will not just be 'u' or 'd' but also numbers like '127' or '243' so would I have to change anything in my code on Arduino side to not run into anymore errors?

Yes.

Good on you Nick for catching that. That | should have been an &. Thank you! I truly appreciate all the help I got on this because I was stuck on it from a while! Learned lots of interesting lessons.

twirap:
Instead of ==1 or ==2 I did try to do "u" "d" but I got the same error with the comparison, so I thought maybe I have to make it all numbers rather than single characters. Now that I know I should be using ' instead of " I will replace it with 'u' and 'd' instead.

Not at all. You needed 'u' and 'd' not "u" and "d". Same problem.

Yes, you are right. :slight_smile:

A problem is that the data coming from serial will not just be 'u' or 'd' but also numbers like '127' or '243' so would I have to change anything in my code on Arduino side to not run into anymore errors?

Are you sending single bytes or not? Do you mean one byte whose value is 127, or three bytes? If three bytes you will need delimiters, otherwise how will you know the difference between:

1 27
12 7
127