# Problems in interaction between Arduino and Processing

**URL:** <https://discourse.processing.org/t/problems-in-interaction-between-arduino-and-processing/29195>\
**Category:** Electronics (Arduino, etc.)\
**Tags:** homework\
**Created:** [April 7, 2021, 4:22pm UTC](https://discourse.processing.org/t/problems-in-interaction-between-arduino-and-processing/29195 "2021-04-07T16:22:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoollow](https://avatars.discourse-cdn.com/v4/letter/y/a88e4f/32.png) [@Yoollow](https://discourse.processing.org/u/Yoollow)\
**Post date:** [April 7, 2021, 4:22pm UTC](https://discourse.processing.org/t/problems-in-interaction-between-arduino-and-processing/29195/1 "2021-04-07T16:22:30Z")

</div>

Hello,  
I’m having problems in the following exercise:  
I have created a grid of 9 cells on Processing, i cyclically color in yellow the diagonal ones.  
From my Arduino code i want to send the information about which of 3 bottons i pressed, my Arduino can send 3 numbers through serial communication (i am still kinda noob with this part) that are 100 (1st button), 010 (2nd button) and 001 (3rd button).  
I know that the hardware is working and even the Arduino code, since when i try it alone i get on the serial monitor what i want. The problems start with Processing.  
I’ll use an example: when the first cell lightens and i press the corresponding button it should turn green, stop a moment and then keep with the cyclical yellow lightening. Where am I wrong? I put here my code. Thanks for help.

```auto
//initialization
import processing.serial.*;
Serial mySerial; //from serial library i import a serial object
String val= null; //variable to collect serial data
int myval; //converted imported serial data in integer from string
Cell[][] grid;
// Number of columns and rows in the grid
int cols = 3;
int rows = 3;
int k=0;

 
void setup() {
  
  size(720,720);
  grid = new Cell[cols][rows];
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      // Initialize each object
      grid[i][j] = new Cell(i*190,j*190,190,190,i+j);
 
    }
  }
  /*String myPort= Serial.list() [2]; //linking processing to the correct port (COM3) so 2.*/
  mySerial=new Serial(this,"COM3",9600);
  
}
 
void draw() {
   
   for (int i = 0; i < cols; i++) {
     for (int j = 0; j < rows; j++) {
              grid[i][j].display();
     }
   }
    
    
    grid[k][k].turntoyellow();
    delay(2000);
    if(mySerial.available()>0){ //if there's available data in the port do the following code
    val= mySerial.readStringUntil(int('\n'));//note:this strips data from the port, once processing takes it it no longer is anywhere else
          if (val!=null){ 
           myval=int(val);
           print(val);
           switch(myval){
            case 100:
                      if(k==1)
                      {grid[k][k].turntogreen();
                      delay(1000); 
                      grid[k][k].display();
                       } 
                       break;
            case 010:
                      if(k==2){grid[k][k].turntogreen();
                      delay(1000); 
                      grid[k][k].display();
                       } 
                       break;
            case 001:
                    if(k==3){grid[k][k].turntogreen();
                      delay(1000);  
                      grid[k][k].display();
                       }
                       break;
                     }
          
                     }
          }               
    
    k=k+1;
    if(k==3){
      k=0;
    }
  }

 
// A Cell object
class Cell {
  // A cell object knows about its location in the grid as well as its size with the variables x,y,w,h.
  float x,y; // x,y location
  float w,h; // width and height
  float angle; // angle for oscillating brightness
  
  
 
  // Cell Constructor
  Cell(float tempX, float tempY, float tempW, float tempH, float tempAngle) {
    x = tempX;
    y = tempY;
    w = tempW;
    h = tempH;
    angle = tempAngle;
    
  }

 
 void display() {
    stroke(255);
    // Color calculated using sine wave
    fill(130);
    rect(x,y,w,h);
 
  }
 void turntogreen(){
   stroke(255);
   fill(0,255,0);
   rect(x,y,w,h);
 }
 void turntoyellow(){
   stroke(255);
   fill(255,255,0);
   rect(x,y,w,h);
 }
}

```

---

<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:** [April 7, 2021, 4:48pm UTC](https://discourse.processing.org/t/problems-in-interaction-between-arduino-and-processing/29195/2 "2021-04-07T16:48:46Z")

</div>

Hello,

Welcome!

Please format your code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

It helps us help you.

`:)`

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [April 7, 2021, 10:45pm UTC](https://discourse.processing.org/t/problems-in-interaction-between-arduino-and-processing/29195/3 "2021-04-07T22:45:01Z")

</div>

Hi @Yoollow, I haven’t studied all your code, but you normally need “break;” after each “case” action. See the example in the help for “switch”. Without that it drops-through and executes the following case’s actions as well.

```auto
        case "100":
                  if(k==1)
                  {grid[k][k].turntogreen();
                  delay(1000);
                  
                   } 
                  break;

```

---

<div class="post-metadata">

**Author:** ![Yoollow](https://avatars.discourse-cdn.com/v4/letter/y/a88e4f/32.png) [@Yoollow](https://discourse.processing.org/u/Yoollow)\
**Post date:** [April 8, 2021, 7:18am UTC](https://discourse.processing.org/t/problems-in-interaction-between-arduino-and-processing/29195/4 "2021-04-08T07:18:01Z")

</div>

Thank you, corrected that.
