Processing gui buttons and arduino

Hello everyone,

First of all I wish everyone a great christmas and a healthy and happy new year.

I am trying to make a GUI with a “start” and “stop” button in processing with controlp5.
I want the “start” button to start my void loop on the arduino, and I want the “stop” button to stop the void loop and let the display say “done”. I tried some things and I searched a lot on the internet.

In the arduino code, I tried to make a void knop that could make sure the buttons work.
It doesn’t work and I don’t know how to make it work.

If anyone can help me with this I would be very happy.
Both arduino and processing files are as attachments.

sketch_191209a.pde (953 Bytes)

sketch_dec02a.ino (9.56 KB)

please put your code in code tags. it's much easier for people to answer you when we don't have to download and create a new project just to take a peek at your issue.

That is not how Arduino code works. setup() gets called once and then loop() gets called repeatedly so your knop code never gets called.

You need to restructure your code. Rename your loop() function to something else and then rename your knop() function to loop(). Or, just combine the two together.

Each time through loop(), you need to see if Serial input is available. If it is, read it and act on it. If it is not, don't do anything.

Thank you very much!

Now I managed to let my previous "loop()" now called something else to start when I press the "start" button. But my "stop" button still does not what I want it to do. Here is the code of my previous "knop()" now called "loop()".

void loop() { 
if (Serial.available()){ 
    int val = Serial.read();
    
    if(val== 1) {
    shuttleruntest();
    }
    else if(val== 2) {
          ledsAan();
          display.setSegments(done);
        }
      }
  }

Do you have an idea of how I can let this work?

 if(val== 1) 
else if(val== 2)

Processing is probably sending data in ASCII so the above tests should be for the characters '1' and '2', not the numbers 1 and 2.

Robin2's serial input basics tutorial may be of interest.

I tried to make it work with char but it still does the same. The start button is working but the stop button is not :frowning:

Post the latest version of your code so that we can keep up.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

arduino code for the buttons.

void loop() { 
if (Serial.available()> 0){ 
    char val = Serial.read();
    
    if(val== '1') {
    shuttleruntest();
    }
    else if(val== '2') {
          ledsAan();
          display.setSegments(done);
         }
      }
  }

processing code.

import controlP5.*;
import processing.serial.*;
ControlP5 controlP5;

Serial port;

void setup() {
printArray(Serial.list());
port = new Serial(this, "COM3", 9600);
 
  size(700, 1000);
  background(0, 225, 255); 

  controlP5 = new ControlP5(this);
  controlP5.addButton("START")
           .setValue(1)
           .setPosition(300,300)
           .setSize(100, 30)
           .setColorBackground(0xffaa0000)
           .setColorActive(0xff660000);


  controlP5.addButton("STOP")
           .setValue(1)
           .setPosition(300,400)
           .setSize(100, 30)
           .setColorBackground(0xffaa0000)
           .setColorActive(0xff660000);
           
}


void draw() {
  fill(255, 255, 255); 
  rect(175, 100, 350, 800);

  textSize(50);
  fill(255, 0, 0);
  text("Pacer15", 255, 200);
  
  
}

void START() {
  port.write('1');

}

 void STOP() {
  port.write('2'); 
}

Sometimes it helps to write a very simple sketch to see what is going on with a part of a program. So I set up my Uno with a software serial port to connect with Processing so that I could display what the Processing code sends in serial monitor.

#include <SoftwareSerial.h>

SoftwareSerial ss(4, 7);

const byte ledPin = 13;

void setup()
{
   Serial.begin(115200);
   ss.begin(9600);
   pinMode(ledPin, OUTPUT);
}

void loop()
{
   if (ss.available())
   {
      char incoming = ss.read();
      Serial.println(incoming);
      if (incoming == '1')
      {
         digitalWrite(ledPin, HIGH);
      }
      if (incoming == '2')
      {
         digitalWrite(ledPin, LOW);
      }
   }
}

The LED on pin 13 responds to the input from the Processing program. The Start button turns on the LED and the Stop button turns the LED off. So the Processing code and using ‘x’ for the comparison works. The issue must be in another part of your code.

display.setSegments(done);

What is that statement supposed to do? Edit: I looked at the library and understand that part now.

Please post the entire Arduino sketch. Snippets usually do not help as they leave out information and are often not the area where the real problem resides.

I really appreciate that you help me!

I tried to post the full version of my code here but my message exceeded the maximum allowed length (9000 characters). I will upload my files instead.

I am new to arduino and this forum so I really appreciate all the advice from everyone.

sketch_shuttlerunprocessing.pde (934 Bytes)

sketch_shuttlerun.ino (9.6 KB)

The for and while loops in the shuttleruntest() block execution while they run and they take a very long time to complete. The serial input is not being read while the shuttleruntest() function is running so the program cannot read serial input nor respond to the buttons.

I understand the problem now, thank you very much.
Is there a way to stop the shuttleruntest() by pressing the "stop" button?

You will need to rewrite the shuttleruntest() function so that it is non blocking. A state machine approach may be what s needed. Instead of using for and while loops, let the looping be done by the loop() function.

As a starter for getting rid of your blocking loops. Look at the BlinkWithoutDelay example in the IDE. That is the basic principle.