ASCII vs Text

Hi

I wrote basic code to recieve a string and print it on screen.

String color;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available()){
    color = (Serial.read());
    Serial.print(color);
  }
}

I type "red". It prints the ASCII numbers for it(114101100). How can I get it to print "red" rather than the ASCII code?

That's apparently how String deals with creating a String from an int.

To create get it to treat the value as a character it may be as simple as casting to char:

color = (char) Serial.read();

Alternatively you could get rid of the String and just print the character:

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available()){
    Serial.print(Serial.read(), CHAR);
  }
}

You also have to make a for loop if you want to make color = "red"

N314: ``` [glow=yellow,2,300]String color;[/glow]

void loop() {   if(Serial.available()){     color = (Serial.read());     Serial.print(color);   } }

String is an object. string is a char string, which is an array.

Since you aren't catting the characters together, in this case, define color as a char.

Simple code to capture what is sent from the serial monitor as a string.

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    readString="";
  } 
}