user1563: I'm trying to control an led attached to my arduino via key presses on my laptop.When i hold down a key, the led should light up, and when i release the key, the led should turn off.
While my code works ATM, i am wondering why my code will not work if i replace the numbers in my code with text/strings.
Processing code(Working)
import processing.serial.*;
Serial myPort;

void setup(){
  println("The available ports are: ");
  println(Serial.list());
  myPort = new Serial(this,Serial.list()[0],9600);
  myPort.buffer(8);
  size(500, 500);
}

void draw(){
  if(keyPressed){
    if(key == 'r' || key == 'R'){
      myPort.write(1);
      println("red");
    }

  }

}

void keyReleased(){
  myPort.write(2);println("released");
}

Arduino Code(Working)
int red = 7;

void setup(){
  Serial.begin(9600);
  pinMode(red,OUTPUT);
}

void loop(){

}

void serialEvent(){
    int input = Serial.read();
  if(input == 1){
    digitalWrite(red,HIGH);
  }else if(input == 2){
    digitalWrite(red,LOW);
  }
}

Processing Code (Not working)
import processing.serial.*;
Serial myPort;

void setup(){
  println("The available ports are: ");
  println(Serial.list());
  myPort = new Serial(this,Serial.list()[0],9600);
  myPort.buffer(8);
  size(500, 500);
}

void draw(){
  if(keyPressed){
    if(key == 'r' || key == 'R'){
      myPort.write("red");
      println("red");
    }

  }

}

void keyReleased(){
  myPort.write("released");
  println("released");
}

Arduino Code(Not working)
int red = 7;

void setup(){
  Serial.begin(9600);
  pinMode(red,OUTPUT);
}

void loop(){

}

void serialEvent(){
    int input = Serial.read();
  if(input == "red"){
    digitalWrite(red,HIGH);
  }else if(input == "released"){
    digitalWrite(red,LOW);
  }
}

From the above example, i get the error ISO C++ forbids comparisons between pointers and integers.After googling the error, it was suggested that i replace the double quotes with single quotes, after which the arduino code is able to compile, but the processing code gives an error "badly formed character constant(expecting quote,got e)".
I then tried changing using double quotes for processing and single quotes for the arduino quote, but predictably, it did not work.
Why does my code work when i pass numbers to the arduino via serial communication, and my code fails when i use text strings?

user220: The problem is that you're mixing different variable types. It's kinda an oil/water scenario.
Basically, the serial.read(); command returns an integer, -1 if there's nothing in the buffer. If there is something, it will return the character code (i.e. 93 = a). Because of that, it's an integer. When you put something in single quotes, it compiles as the equivalent ISO character code.
So, when you do input == 2, it is integer [compared to] integer. When you do input == "string", you're doing integer [compared to] string, thus you get errors. BTW: a string is a type of pointer.
So, what you have to do is this:
string input = "";
if(serial.avaliable()) {
    delay(100); //Wait for all data to be received
    while(serial.avaliable()) {
        string = string + char(serial.read());
    }
}
if(input == "yay_it_works!) {
    //Done!
}


1563: <p>I'm trying to control an led attached to my arduino via key presses on my laptop.When i hold down a key, the led should light up, and when i release the key, the led should turn off.</p>

<p>While my code works ATM, i am wondering why my code will not work if i replace the numbers in my code with text/strings.</p>

<p>Processing code(Working)</p>

<pre><code>import processing.serial.*;
Serial myPort;

void setup(){
  println("The available ports are: ");
  println(Serial.list());
  myPort = new Serial(this,Serial.list()[0],9600);
  myPort.buffer(8);
  size(500, 500);
}

void draw(){
  if(keyPressed){
    if(key == 'r' || key == 'R'){
      myPort.write(1);
      println("red");
    }

  }

}

void keyReleased(){
  myPort.write(2);println("released");
}
</code></pre>

<p>Arduino Code(Working)</p>

<pre><code>int red = 7;

void setup(){
  Serial.begin(9600);
  pinMode(red,OUTPUT);
}

void loop(){

}

void serialEvent(){
    int input = Serial.read();
  if(input == 1){
    digitalWrite(red,HIGH);
  }else if(input == 2){
    digitalWrite(red,LOW);
  }
}
</code></pre>

<p>Processing Code (Not working)</p>

<pre><code>import processing.serial.*;
Serial myPort;

void setup(){
  println("The available ports are: ");
  println(Serial.list());
  myPort = new Serial(this,Serial.list()[0],9600);
  myPort.buffer(8);
  size(500, 500);
}

void draw(){
  if(keyPressed){
    if(key == 'r' || key == 'R'){
      myPort.write("red");
      println("red");
    }

  }

}

void keyReleased(){
  myPort.write("released");
  println("released");
}
</code></pre>

<p>Arduino Code(Not working)</p>

<pre><code>int red = 7;

void setup(){
  Serial.begin(9600);
  pinMode(red,OUTPUT);
}

void loop(){

}

void serialEvent(){
    int input = Serial.read();
  if(input == "red"){
    digitalWrite(red,HIGH);
  }else if(input == "released"){
    digitalWrite(red,LOW);
  }
}
</code></pre>

<p>From the above example, i get the error ISO C++ forbids comparisons between pointers and integers.After googling the error, it was suggested that i replace the double quotes with single quotes, after which the arduino code is able to compile, but the processing code gives an error "badly formed character constant(expecting quote,got e)".</p>

<p>I then tried changing using double quotes for processing and single quotes for the arduino quote, but predictably, it did not work.</p>

<p>Why does my code work when i pass numbers to the arduino via serial communication, and my code fails when i use text strings?</p>
 220: <p>The problem is that you're mixing different variable types. It's kinda an oil/water scenario.</p>

<p>Basically, the <code>serial.read();</code> command returns an integer, <code>-1</code> if there's nothing in the buffer. If there is something, it will return the character code (i.e. 93 = <code>a</code>). Because of that, <em>it's an integer.</em> When you put something in single quotes, it compiles as the equivalent ISO character code.</p>

<p>So, when you do <code>input == 2</code>, it is <code>integer [compared to] integer</code>. When you do <code>input == "string"</code>, you're doing <code>integer [compared to] string</code>, thus you get errors. BTW: a string is a type of pointer.</p>

<p>So, what you have to do is this:</p>

<pre><code>string input = "";
if(serial.avaliable()) {
    delay(100); //Wait for all data to be received
    while(serial.avaliable()) {
        string = string + char(serial.read());
    }
}
if(input == "yay_it_works!) {
    //Done!
}
</code></pre>
