Why does Processing send so many signals at once?

I am trying to make a Breakout clone game and try to connect the Arduino to control the LED when the ball hit the bricks

and I wrote the codes like this

``````void checkCollision(){

if ( ball.x > bar.x && ball.x < bar.x + bar.barWidth ){
if ( ball.y > bar.y && ball.y < bar.y + bar.barHeight ){
ball.vY = -ball.vY;
}
}

for (int i = 0; i < MAX_BLOCKS; i++){
if  ( blocks[i].hitFlag == false ){
if ( ball.x  > blocks[i].x &&  ball.x  <  blocks[i].x + blocks[i].blockWidth ){
if ( ball.y > blocks[i].y  &&  ball.y  <  blocks[i].y + blocks[i].blockHeight ){
ball.vY = -ball.vY;
blocks[i].hitFlag = true;

``````

this is to realize the function of disappearing after the collision between ball and bar and bricks.

and I want the LED to change color every time the collision happened so I need to send the signal to Arduino, using these codes

``````import processing.serial.*;

Serial serial;

void Color() {

if ( ball.x > bar.x && ball.x < bar.x + bar.barWidth ){
if ( ball.y > bar.y && ball.y < bar.y + bar.barHeight ){

serial.write("R");

}
}

for (int i = 0; i < MAX_BLOCKS; i++){

if ( ball.x  > blocks[i].x &&  ball.x  <  blocks[i].x + blocks[i].blockWidth ){
if ( ball.y > blocks[i].y  &&  ball.y  <  blocks[i].y + blocks[i].blockHeight ){

serial.write("L");

``````

but the problem I found is that every time the ball hit the bricks the processing sends a lot of R or L to Arduino instead of a single R or L that I expected.
i mean although it can work and achieve the effect I want, its processing efficiency is too low, because it has to be sent many times in each collision, which leads to the collision being stuck.

I am a beginner. Maybe my mistakes are very basic. I hope someone can help me.

Hello,

Welcome aboard!

Take a look at this topic:

If you are using the Processing environment you can also use:
https://processing.org/reference/println_.html

It can be very useful for examining and debugging code.
Only use it only when needed! It can slows things down.

Example:

``````int counter;

void setup()
{
frameRate(1); //Slows things down for this example
println("Setup: ", counter, frameCount);
}

void draw()
{
background(0);
println("Draw: ", counter, frameCount);
counter++;
}
``````

`:)`

1 Like

I am very grateful for your reply. Thank you for taking your time to answer this may be a very elementary question. I am studying it carefully.
Thank you very much again.

We were all beginners and learn from our experiences and will be making mistakes. I am still making them!

Mistakes are opportunities to learn from.

Serial communications can be a challenge to new users.

And even a challenge for experienced ones at times!

There are resources (tutorials, references, examples, etc.) here:
https://processing.org/

And examples that come with Processing:

Check out the GUI examples!

One example in there may help with your nested` if()` statements:
https://processing.org/examples/button.html
Your code may work as is but can be combined into one` if()`

`:)`