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.
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.
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?
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.
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: