Problems in interaction between Arduino and Processing

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.

//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);
 }
}

Hello,

Welcome!

Please format your code:
https://discourse.processing.org/faq#format-your-code

It helps us help you.

:)

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.

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

Thank you, corrected that.