Go Down

Topic: Newbie question about error: invalid conversion (Read 598 times) previous topic - next topic

eelke

Hey all,

Two days ago I started playing around with arduino, so I am completely new to the programming of the arduino board. also I don't know processing. (I do fine with webbased programming though, like actionscript & php)

However, I did get the blinking led sketch to work, and amended it to say "hello world" in morse, controlling a Relaysboard and a 230v lightbulb. All very exciting for my taste, however I need it to do a lot more: my Arduino's planned to become the beating hardware heart of my graduation installation.

So, now I run into a problem. My following code is accepted by Arduino 0003, by rejected by 0004. It says:
Code: [Select]
In function 'void loop()':
error: invalid conversion from 'const char*' to 'unsigned char

This is the code I have. Can anybody tell me what I am doing wrong?
Code: [Select]
int joyPin = 2;
int joyVal = 0;

void setup()
{
 beginSerial(9600);
 pinMode(joyPin, INPUT);  
}
void loop()
{
 joyVal = digitalRead(joyPin);
 if (joyVal == HIGH){
   serialWrite("A");
 }  
 else{
   serialWrite("B");
 }
 delay(100);
}




eelke

Gosh, changing "serialWrite("blabla");" to "Serial.print("blabla");" has a magical effect on this problem.

Excuse me for my noobness ;)

mellis

Don't worry, it's actually a subtle issue.  In C and C++, "A" is a string, i.e. a pointer to sequence of characters.  A single character is written with single quotes, e.g. 'A'.  In C, functions cannot be overloaded, i.e. you can't have a version of serialWrite for strings and another for characters.  C will, however, happily convert a string like "A" to a character (which is what serialWrite is designed for) - but it won't do so in the way you expect.  That is, "A" gets converted into the character whose ASCII code equals the address of the string.  In C++, you can overload functions, so there's a version of Serial.print() for both characters and strings (and integers, etc) that does what you'd expect.

Go Up