Different behaviour between Processing and Arduino Serial Monitor

Dear all,

I have just started experiencing with Processing, and ran into the following problem.
I am trying to blink the built-in LED on an Arduino board (Mega2560), and am experiencing different behavior between the Processing GUI and the Arduino Serial Monitor.

What my code tries to do is blink the LED with a delay of 2000 when button ‘a’ is pressed, and do the same with a delay of 50 when button ‘z’ is pressed.

My code is working as expected when inputting the char ‘a’ or ‘z’ in the Serial Monitor in Arduino IDE, but there are problems when trying to use the Processing GUI. In the latter case, the LED is not blinking and the TX led on the Arduino is stuck (constantly lit).

Here are my codes:

Arduino:

int pinLed = 13;
char option;
int del = 2000;

void setup() {
  // put your setup code here, to run once:
  pinMode(pinLed, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available())
  {
    option = Serial.read();
  }

  switch (option)
  {
    case 'a':
      del = 2000;
      Serial.println("a");
      break;
    case 'z':
      del = 50;
      Serial.println("z");
      break;
    default:
      digitalWrite(pinLed, HIGH);
      delay(del);
      digitalWrite(pinLed, LOW);
      delay(del);
      break;
  }
}

Processing:

import processing.serial.*;
import controlP5.*; // import controlP5 library

Serial myPort;

String val;

ControlP5 cp5; // create controlP5 object
PFont font;
PFont titlefont;

void setup()
{
  size(600, 900); // window size (width, height)

  printArray(Serial.list());  // prints all available serial ports

  myPort = new Serial(this, "COM3", 9600); // Arduino connected to COM3 port

  cp5 = new ControlP5(this);
  font = createFont("calibri light", 20);       // custom fonts for buttons
  titlefont = createFont("calibri light", 40);  // for title

  cp5.addButton("a")    // button name
    .setPosition(230, 80)   // x, y coord. of top left corner
    .setSize(120, 90)       // (width, height)
    .setFont(font)
    ;

  cp5.addButton("z")    // button name
    .setPosition(230, 200)   // x, y coord. of top left corner
    .setSize(120, 90)      // (width, height)
    .setFont(font)
    ;
}

void draw()
{
  background(255, 255, 255); //background color
  fill(0, 0, 0);
  textFont(titlefont);
  text("LED BLINK", 210, 40);
}

// Adding some functions to buttons

void a() {
  myPort.write('a');
  println("a");
}

void z() {
  myPort.write('z');
  println("z");
}


What could be the problem?
Thank you in advance for your answers!

Hello,

It works here with LED 13 as long as I have a connection to Arduino.

You should comment the Serial.println() on the Arduino; you are sending data back to Processing but there is no code to receive this.
Do not use the Arduino monitor or plotter when connecting Arduino to Processing over one serial connection.

:)

2 Likes

Hi glv,

Thank you for your answer! You are right about commenting Serial.println() on Arduino.

However, the buttons still not work. The LED is blinking when I run the Processing sketch, but when I hit the Z button, nothing is happening with the LED.
I tried it out with if and else if, insted of a switch case in my Arduino code, and that one is working with Processing as well. I am just wondering what could be the difference.

I did have issues with buttons as well and it only worked here while connected to the Arduino.

I will explore later today…

I am not a CP5 user but may explore this further.

A related post without CP5:

Sometimes it helps to isolate the problem.

:)

hi

iam using slider and this code work with me

void bob() {  
  
 
  
     if (Serial.available() >= 2)
 
  {
    int value2;
    char command = Serial.read();
    switch (command)
    {
    case 'a': value2 = Serial.parseInt();
      CYCLE_TIME = value2;
     
      {
    
   
     
     
      break;

    case 'b': value2 = Serial.parseInt();
     TX_PULSE = value2;
       
   break;
      
  
      

   
     }

try this code or modify it as your demand

   if (Serial.available())
  {
   int value2;
    
 char   option = Serial.read();
  }

  switch (option)
  {
    case 'a':value2 = Serial.parseInt();
     
      a =value2
      break;
    case 'z':value2 = Serial.parseInt();
      
      z=value2
      break;
       }

i used this to turn led on or off

void ON() {
port.write(1);
}

void OFF() {
port.write(2);
}

 


and Arduino

void loop(){

if(Serial.available()){

int val = Serial.read();

if(val == 1){
digitalWrite(13, HIGH); //turn on
}
if(val == 2){ //if 2 received
digitalWrite(13, LOW); //turn off
}

}
}

There is a problem, that I have run into, and was the issue in this topic. I found the problem using RPi talking to a Chinese Arduino Mega. If you send to the Arduino serial when it’s not ready, the serial is messed up and doesn’t work. When you connect to it with your Processing sketch, that resets the Arduino, and then there’s a period when it’s not ready. So try waiting two seconds after connecting before sending any serial characters. That might be too long, but see if it fixes the problems, then reduce. I’ve never seen this problem with Arduino Uno or Nano, but suspect it’s there on Mega and Due.

Now there’s another problem with ControlP5 might be active here. This topic says that when you create a CP5 button it runs its action. In your case this might be sending serial to a not-ready-Arduino.