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?