Pages: [1]   Go Down
Author Topic: Newbie question about error: invalid conversion  (Read 521 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
greater than 3 arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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);
}


Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
greater than 3 arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Excuse me for my noobness smiley-wink
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 11
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: