Arduino and procesing

Hello,

So I've got a processing code that should display the last 5 code lines that where run in the arduino so the code for processing is

import processing.serial.*;

Serial myPort;
String val5 = "0";
String val4 = "0";
String val3 = "0";
String val2 = "0";
String val1 = "0";
String val6 = "0";
String val7 = "0";

void setup()
{
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);
  fullScreen();
  background(0);
  noStroke();
  fill(102);
}

void draw()
{

  if ( myPort.available() > 0) 
  { 
    val6 = myPort.readStringUntil('\n');
    if (val6.equals("1") == true) { 
      val7 = "if(protocol > 1) {";
      println("Equal");
    }

    if (val6.equals("2") == true) { 
      val7 = "  if(protocol == 1) {";
    }

    if (val6.equals("3") == true) { 
      val7 = "  time = millis();";
    }    
    if (val6.equals("4") == true) { 
      val7 = "  time = millisstart;";
    }    
    if (val6.equals("5") == true) { 
      val7 = "  protocol = 2; }";
    }        


    background(0);
    val5 = val4;
    val4 = val3;
    val3 = val2;
    val2 = val1;
    val1 = val7;

    textSize(30);

    text(val5, 10, height/4 - 120);
    text(val4, 10, height/4 - 90);
    text(val3, 10, height/4 - 60);
    text(val2, 10, height/4 - 30);
    text(val1, 10, height/4);
  }
}

And the plan was that arduino send a 1 which meant to processing that the code line if(protocol > 1) { was about to run and then if the arduino send a 2 processing would show that the arduino is about to run if(protocol == 1) { and the previous code line would move one up until that happend 5 times after which it would go out of the screen

And this is the arduino code

Serial.println("1");
  if(protocol > 1) { //protocol timing
    Serial.println("2");
    if(protocol == 1) {
      Serial.println("3");
      time = millis();
      Serial.println("4");
      time = millisstart; //millis start
      Serial.println("5");
      protocol = 2; }

This isn't the whole code but the other parts weren't import to the procces since I didn't give them any numbers yet.

Hope someone can help :frowning:

  if(protocol > 1) { //protocol timing
    Serial.println("2");
    if(protocol == 1) {

The value of 2 will be printed only if protocol is greater than 1. There isn't a snowball's chance in hell of the value then being 1.

Put every { on a new line, and use Tools + Auto Format, to see stupid mistakes like this.

void draw()
{

  if ( myPort.available() > 0)
  {
    val6 = myPort.readStringUntil('\n');

You should NOT be doing serial input in draw(). Create a serialEvent() function to be called when there is serial data to process. Use the bufferUntil() method, in setup(), to define when the serialEvent() function should be called.

Thank you for your response,

I was aware that It wouldn't enter the if statement but before the if I placed a Serial.printIn("1"); so processing still should've put the if statement on the screen and repeated it 5 times?

Also thanks for the tips I will use them

I forgot to say that it still doesn't work after making the adjustments? so maybe someone has some more tips?

I forgot to say that it still doesn't work after making the adjustments?

Is that a question or a statement?

You also forgot to post your changed code, so you can forget about expecting answers.

My bad, i didn’t change the Arduino code so and i’ve attached the proccesing code below, altough while changing the code I ran into a new problem.

In my previous versions of the code I used this to translate the serial data

myPort.readStringUntil('\n');

but with the serialEvent example they use

  inString = p.readString();

but you cant add (’\n’) since no parameters are not expected.

Here’s the code

import processing.serial.*;
Serial myPort;
int lf = 10;
String inString;
String val5 = "0";
String val4 = "0";
String val3 = "0";
String val2 = "0";
String val1 = "0";


void setup()
{
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);
  myPort.bufferUntil(lf); 
  fullScreen();
  background(0);
  noStroke();
  fill(102);
}


void draw()
{
  background(0);

  val5 = val4;
  val4 = val3;
  val3 = val2;
  val2 = val1;
  val1 = val1;

  textSize(30);

  text(val5, 10, height/4 - 120);
  text(val4, 10, height/4 - 90);
  text(val3, 10, height/4 - 60);
  text(val2, 10, height/4 - 30);
  text(val1, 10, height/4);
}



void serialEvent(Serial p) { 
  inString = p.readString(); // <------- problem! 

  if (inString.equals("1") == true) { 
    val1 = "if(protocol > 1) {";
  }

  if (inString.equals("2") == true) { 
    val1 = "  if(protocol == 1) {";
  }

  if (inString.equals("3") == true) { 
    val1 = "  time = millis();";
  }    
  if (inString.equals("4") == true) { 
    val1 = "  time = millisstart;";
  }    
  if (inString.equals("5") == true) { 
    val1 = "  protocol = 2; }";
  }
}

thanks in advance

but with the serialEvent example they use

It is an example. It is perfectly OK to use readStringUntil() in serailEvent().

Although, it is more normal to use bufferUntil() in setup(), so that serialEvent() is only fired when the appropriate character arrives. That way, readString() is a better choice.

  inString = p.readString(); // <------- problem!

Why is it a problem? The String class has a trim() method that dumps all white space. When you send data followed by carriage return and line feed, and buffer the data until the line feed arrives, readString() is going to read the carriage return as part of the data. “1” and “1\n” are not equal.

Thank you so much!

I got it working now by adding trim

:slight_smile: