Hi , I have a little problem. So I'm trying to make a space invaders type game with light sensors as controls. Whenever the the sensor's input drops below 500 (because you covered it) the arduino prints a word corresponding with the sensor you covered. Now in processing I read the incoming serial data and run a function based on that.
Here's the processing code for the filtering function:
void serialEvent(Serial p){
input = p.readStringUntil('\n');
if(input == null){
println(input);
}else{
println("not null");
if(input.equals("left")){
println("LEFT");
spaceship.move("left");
}
if(input == ("right")){
println("RIGHT");
spaceship.move("right");
}
if(input == ("shoot")){
println("SHOOT");
spaceship.shoot();
}
}
}
And here's the arduino code:
#define led1 12
#define led2 11
#define led3 10
#define ldr1 A0
#define ldr2 A1
#define ldr3 A2
void setup() {
// put your setup code here, to run once:
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
float readldr1 = analogRead(ldr1);
float readldr2 = analogRead(ldr2);
float readldr3 = analogRead(ldr3);
if(readldr1 > 500){
Serial.println("right");
}else{
Serial.println("empty");
}
if(readldr2 > 500){
Serial.println("shoot");
}else{
Serial.println("empty");
}
if(readldr3 > 500){
Serial.println("left");
}else{
Serial.println("empty");
}
}
In the Serial Monitor, I receive all three left, right and shoot. In the console window of processing, I only get null and not null. So it doesn't compare correctly to the if statements inside the not null statement. What am I doing wrong?
ps.: I have noticed that when I print the input variable i get for example:
"right
"
instead of "right"
Also all the other code works because the game ran perfectly when I used keypresses instead.
Can someone please help?
Thanks in advance,
Gyut