School project problem (arduino)

hi guys, i’m trying to make a school project with arduino and processing but I have a problem.
I’m trying to make a controller with 4 buttons that send the digital signal via serial to a processing game that I made, i succeded into sending the data but the problem is that i can’t undestand why the player is not moving.
here attached the two codes, hope you can find out what is the problem and thank you very much!!!

here is the processing code

import processing.serial.*;
Serial myPort;


String valori;
    


int  score=0;
float n=3, b=-3, p, o, c=0, v=0, x=200, y=200;

void setup() {
  
myPort = new Serial(this, "COM3",9600);

 size (800,800); 
 p=random(800);
 o=random(800);

}

void draw () {
  
  
 background (200,140,70);                      //punto rosso
 fill (255,40,40);
 rect(p,o,5,5);
 
 
 fill(random(255),random(255),random(255));    //giocatore
 rect (x,y,20,20);
 x=x+c;
 y=y+v; 





if (x>=p-25 && x<=p+5 && y>=o-25 && y<=o+5)  //se la palla colpisce il punto rosso il punto si sposta il punteggio aumenta di uno e la velocità aumenta
{
  o=random(800);
  p=random(800);
  score++;
n=n+0.15;
b=b-0.15;
}






 if (y>800) {y=y-800;}                  //se la palla va fuori esce dalla parte opposta della finestra
 if (x>800) {x=x-800;}
 if (y<0) {y=y+800;}
 if (x<0) {x=x+800;}



   if (myPort.available()>0) {
   valori = myPort.readStringUntil('\n');  
 }
 
if(valori != null){
if (valori=="1");{c=b; v=0;}
if (valori=="2");{c=n; v=0;}
if (valori=="3");{v=b; c=0;}
if (valori=="4");{v=n; c=0;}
}
println(valori);





if(valori!=null){
fill(255);                             // punteggio
text (valori,400,750);
textSize(35);
}
}

and here the arduino one

int val, val1, val2,val3;
int x;
void setup() {
pinMode(8,INPUT);
pinMode(9,INPUT);
pinMode(10,INPUT);
pinMode(11,INPUT);
Serial.begin(9600);

}

void loop() {
val=digitalRead(8);
val1=digitalRead(9);
val2=digitalRead(10);
val3=digitalRead(11);
if (val==HIGH) {
  x=1;
}
if (val1==HIGH) {
  x=2;
}
if (val2==HIGH) {
  x=3;
}
if (val3==HIGH) {
  x=4;
}

Serial.println(x);

delay(80);
}
1 Like

You have a ; terminating your conditional statement so c and b are never updated based on condition.

Replace this:

if (valori=="1");
{
c=b; v=0;
}

With:

if (valori=="1")
{
c=b; v=0;
}

You may also want to replace serial input with keypad input for testing:

void keyPressed() 
  {
  if (key=='1') valori="1";
  if (key=='2') valori="2";                
  if (key=='3') valori="3";
  if (key=='4') valori="4";
  }
2 Likes

thank you very much, but I still can’t make the player move, with the keypad input everything work but still it doesn’t work with the serial, any idea?

Try Serial.write() on Arduino side and myPort.read(); on Processing side.

I got that to work with your code and it moved in a square.

Once you understand that try working with sending and receiving strings of data; these always caused me some grief until I understood it better.

Arduino test code:

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

void loop()
  {
  int x;
  for (int i = 0; i <= 100; i++)
    {
    x = 2;
    Serial.write(x);
    }

  for (int i = 0; i <= 100; i++)
    {
    x = 4;
    Serial.write(x);
    }

  for (int i = 0; i <= 100; i++)
    {
    x = 1;
    Serial.write(x);
    }
  for (int i = 0; i <= 100; i++)
    {
    x = 3;
    Serial.write(x);
    }
  }

On the Processing side convert all strings to int:

int valori;

printArray(Serial.list());
myPort = new Serial(this, Serial.list()[2], 9600);
//myPort = new Serial(this, "COM3",9600);

valori = myPort.read();

print(valori);

if (valori != 0) 

if (valori==1)

if (key=='1') valori=1;

Going for a hike now!

A little further exploration on the topic…

If you are going to use println() from the Arduino it will send a \r (return) and \n (line feed) as part of the string.

//Shows where \n and \r are in the received data: 
String temp = valori;
temp = temp.replace('\n', 'n');
temp = temp.replace('\r', 'r');
println(temp);

//You must trim whitespace before compare to remove \r and \n
valori = trim(valori);
println(valori);

You may not compare strings with == and must use equals().

References:
https://processing.org/reference/String_equals_.html
https://processing.org/reference/trim_.html

I modified my write() in previous code to println() on the Arduino side and modified Processing code as per this discussion and it works.

This is just one way to do this.

1 Like

thank you very much!!!
now it is perfectly working!

1 Like