# Why does Processing send so many signals at once?

**URL:** https://discourse.processing.org/t/why-does-processing-send-so-many-signals-at-once/31420
**Category:** Electronics (Arduino, etc.)
**Tags:** homework
**Created:** [July 24, 2021, 10:43am UTC](https://discourse.processing.org/t/why-does-processing-send-so-many-signals-at-once/31420 "2021-07-24T10:43:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Adilic0](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@Adilic0](https://discourse.processing.org/u/Adilic0)
#### Post date: [July 24, 2021, 10:43am UTC](https://discourse.processing.org/t/why-does-processing-send-so-many-signals-at-once/31420/1 "2021-07-24T10:43:38Z")

</div>

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

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

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

---

<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: [July 24, 2021, 11:28am UTC](https://discourse.processing.org/t/why-does-processing-send-so-many-signals-at-once/31420/2 "2021-07-24T11:28:35Z")

</div>

Hello,

Welcome aboard!

Take a look at this topic:

> [@Processing to Arduino Serial Library Example](https://discourse.processing.org/t/processing-to-arduino-serial-library-example/11143/5):
>
> I modified the existing “SimpleWrite” example to: save and check for the state of mouseOverRect() print state to console only writes serial data once a delay() to wait for Arduino to reset once connected to it; in my case, it is an Arduino Mega 2560 R3. I kept the spirit of the original example intact and left it as a “Simple Write” program. My edit of “SimpleWrite” example so it only sends data once: /\*\* \* Simple Write. \* \* Check if the mouse is over a rectangle and writes the status …

If you are using the Processing environment you can also use:  
[https://processing.org/reference/println\_.html](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:

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

```

`:)`

---

<div class="post-metadata">

### Author: ![Adilic0](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@Adilic0](https://discourse.processing.org/u/Adilic0)
#### Post date: [July 24, 2021, 11:45am UTC](https://discourse.processing.org/t/why-does-processing-send-so-many-signals-at-once/31420/3 "2021-07-24T11:45:54Z")

</div>

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.

---

<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: [July 24, 2021, 11:52am UTC](https://discourse.processing.org/t/why-does-processing-send-so-many-signals-at-once/31420/4 "2021-07-24T11:52:16Z")

</div>

> [@Adilic0](#):
>
> I am a beginner. Maybe my mistakes are very basic.

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/](https://processing.org/)

And examples that come with Processing:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/74f47a1e3026fa826143c89baa41f9da8ddb9ac8.png)

Check out the GUI examples!

One example in there may help with your nested` if()` statements:

> **[Button / Examples](https://processing.org/examples/button.html)**
>
> Click on one of the colored shapes in the center of the image to change the color of the background.

Your code may work _as is_ but can be combined into one` if()`

`:)`

---

<div class="post-metadata">

### Author: ![Adilic0](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@Adilic0](https://discourse.processing.org/u/Adilic0)
#### Post date: [July 24, 2021, 12:01pm UTC](https://discourse.processing.org/t/why-does-processing-send-so-many-signals-at-once/31420/5 "2021-07-24T12:01:18Z")

</div>

This is really helpful.  
thanks a lot 🙂
