# School project problem (arduino)

**URL:** <https://discourse.processing.org/t/school-project-problem-arduino/433>\
**Category:** Electronics (Arduino, etc.)\
**Tags:** homework\
**Created:** [May 27, 2018, 8:17am UTC](https://discourse.processing.org/t/school-project-problem-arduino/433 "2018-05-27T08:17:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ada](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@Ada](https://discourse.processing.org/u/Ada)\
**Post date:** [May 27, 2018, 8:17am UTC](https://discourse.processing.org/t/school-project-problem-arduino/433/1 "2018-05-27T08:17:51Z")

</div>

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

```auto
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

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 27, 2018, 1:42pm UTC](https://discourse.processing.org/t/school-project-problem-arduino/433/2 "2018-05-27T13:42:41Z")

</div>

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

Replace this:

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

```

With:

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

```

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

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

```

---

<div class="post-metadata">

**Author:** ![Ada](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@Ada](https://discourse.processing.org/u/Ada)\
**Post date:** [May 27, 2018, 2:56pm UTC](https://discourse.processing.org/t/school-project-problem-arduino/433/3 "2018-05-27T14:56:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 27, 2018, 5:03pm UTC](https://discourse.processing.org/t/school-project-problem-arduino/433/4 "2018-05-27T17:03:52Z")

</div>

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:

```auto
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 28, 2018, 1:31am UTC](https://discourse.processing.org/t/school-project-problem-arduino/433/5 "2018-05-28T01:31:22Z")

</div>

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.

```auto
//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/String_equals_.html)  
[https://processing.org/reference/trim\_.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.

---

<div class="post-metadata">

**Author:** ![Ada](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@Ada](https://discourse.processing.org/u/Ada)\
**Post date:** [May 28, 2018, 5:25am UTC](https://discourse.processing.org/t/school-project-problem-arduino/433/6 "2018-05-28T05:25:24Z")

</div>

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