How do I make my delay come after my prompt?

Hello, I am working on a project for school. I am working with Processing 3 with Arduino uno (both program and board). I am writing a GUI on Processing 3, so I can control Arduino. On my GUI, I have a box for inputs and a button called “move” (which currently works if you press the enter key, and I want to make it so that I can press the button with my mouse too). My problem is that I have a delay and a prompt. When a user inputs a degree of say 25, the prompt should appear, have a delay of 5 seconds, and then disappear.

My current problem is that when I enter a number, the delay happens first, then the prompt never shows because the computer reads it as “show the prompt and remove the prompt” back to back, therefore never showing the prompt. I aim for the prompt to show, delay it for 5 seconds, then remove the prompt. I can’t seem to get this to work in the way I want it to. I’ve tried delay(), wait(), sleep(), and nested if statements to perhaps get it to work. I am a beginner, so I am not that well experienced but I am trying my best. (So please be patient with me). I have also searched the internet for hours and couldn’t find a good solution to my problem.

I would appreciate it if someone could assist me, possibly??

Specifically, I am trying to address the problem in my “keypressed” function, in the last 3 if-statements at the end.

**also, if you could tell me how to make my button be able to be pressed with my mouse, I’d appreciate that. But it’s not needed, if you don’t have the time. :slight_smile:

import controlP5.*;
import processing.serial.*;
import static javax.swing.JOptionPane.*;
import java.util.concurrent.TimeUnit;
Serial port;
ControlP5 cp5;
String d;
int angle, WGtracker = 0, Mover=0,timedelay=0,time,starttime,readback;
String url1;
boolean IBE = false, OB = false, indicator = false;
////////////////////////////////////////////

public static void wait(int ms)
{
    try
    {
        Thread.sleep(ms);
    }
    catch(InterruptedException ex)
    {
        Thread.currentThread().interrupt();
    }
}

void setup(){
 size(1000,600);
 port = new Serial(this, "COM3", 9600);
 cp5 = new ControlP5(this);
 cp5.addTextfield("Angle").setPosition(130,80).setSize(60,40);
 cp5.addButton("Move").setPosition(230,80).setSize(60,40);

}

void draw(){
 background(255,255,255);
 fill(0,0,0);
 textSize(50);
 text("A.D.A.S Control GUI",250,50);
 textSize(20);
 if(indicator == false){
 text("Enter angle\n"+"in box",10,90);
 text("Current Position: " + (WGtracker) + " degrees",400,300);
 }
 if(IBE == true){
   text("ERROR! Input Angle must be between 0 and 100",400,400);
 }
 if(OB == true){
   text("The A.D.A.S cannot exceed 0 or 100 degrees",400,400);
 }
 if (indicator == true){
   text("Please wait while the system moves",10,90);
    cp5.addTextfield("Angle").setPosition(130,80).setSize(60,40).hide();
 cp5.addButton("Move").setPosition(230,80).setSize(60,40).hide();
 }
 if(port.available() > 0){
   d = port.readStringUntil('\n');
   readback = int(d);
 }
}

void keyPressed(){
 switch(key){
  case '\n':
  url1 = cp5.get(Textfield.class,"Angle").getText();
  angle = parseInt(url1);
   //ERROR CODE TO CHECK IF USER INPUT IS BETWEEN 1 AND 100 DEGREES
  if(angle > 100 || angle < 0){
    IBE = true;
    return;
  }
  // RESETS ERROR ONCE CORRECT INPUT IS ENTERED
  IBE = false;
 
   OB = false;
   if(angle > WGtracker){
     //port.write(Float.toString(101));
     Mover = angle - WGtracker;
     //port.write(Float.toString(Mover));
     if(Mover == angle - WGtracker) { 
       indicator = true;
     }
     WGtracker = angle;
   }
   if(angle < WGtracker){
     //port.write(Float.toString(102));
     Mover = WGtracker - angle;
     //port.write(Float.toString(Mover));
     //wait(5000);
     WGtracker = angle;
   }   
   if(indicator == true) {
     delay(5000);
     indicator = false;
     }
 }
}
  • for the delay, I use a timer. Check variable timer.

  • For the mouse button I use a new function Move which calls a function submit() (that’s also called by \n now)


import controlP5.*;
import processing.serial.*;
import static javax.swing.JOptionPane.*;
import java.util.concurrent.TimeUnit;

Serial port;
ControlP5 cp5;

String d;
int angle, WGtracker = 0, Mover=0, timedelay=0, time, starttime, readback;
String url1;
boolean IBE = false, OB = false, indicator = false;
int timer=0; 

////////////////////////////////////////////

void setup() {
  size(1000, 600);
  port = new Serial(this, "COM3", 9600);
  cp5 = new ControlP5(this);
  cp5.addTextfield("Angle").setPosition(130, 80).setSize(60, 40);
  cp5.addButton("Move").setPosition(230, 80).setSize(60, 40);
}

void draw() {
  background(255, 255, 255);
  fill(0, 0, 0);
  textSize(50);
  text("A.D.A.S Control GUI", 250, 50);
  textSize(20);
  if (indicator == false) {
    text("Enter angle\n"+"in box", 10, 90);
    text("Current Position: " + (WGtracker) + " degrees", 400, 300);
  }
  if (IBE == true) {
    text("ERROR! Input Angle must be between 0 and 100", 400, 400);
  }
  if (OB == true) {
    text("The A.D.A.S cannot exceed 0 or 100 degrees", 400, 400);
  }
  if (indicator == true) {
    text("Please wait while the system moves", 10, 90);
    cp5.addTextfield("Angle").setPosition(130, 80).setSize(60, 40).hide();
    cp5.addButton("Move").setPosition(230, 80).setSize(60, 40).hide();
  }
  if (port.available() > 0) {
    d = port.readStringUntil('\n');
    readback = int(d);
  }

  if (indicator == true) {
    if (millis()-timer>1500) {     
      indicator = false;
    }
  }
}//func 

void keyPressed() {
  switch(key) {

  case '\n':
    submit(); 
    break; // end of   case '\n': block
  } //switch
} //func 

void Move() {
  // function for button 
  println("Hit");
  submit();
}

void submit() {
  url1 = cp5.get(Textfield.class, "Angle").getText();
  angle = parseInt(url1);
  //ERROR CODE TO CHECK IF USER INPUT IS BETWEEN 1 AND 100 DEGREES
  if (angle > 100 || angle < 0) {
    IBE = true;
    return;
  }
  // RESETS ERROR ONCE CORRECT INPUT IS ENTERED
  IBE = false;

  OB = false;
  if (angle > WGtracker) {
    //port.write(Float.toString(101));
    Mover = angle - WGtracker;
    //port.write(Float.toString(Mover));
    if (Mover == angle - WGtracker) { 
      indicator = true;
      timer=millis();
    }
    WGtracker = angle;
  }
  if (angle < WGtracker) {
    //port.write(Float.toString(102));
    Mover = WGtracker - angle;
    //port.write(Float.toString(Mover));
    //wait(5000);
    WGtracker = angle;
  }
}//func 

1 Like

You are a literal life saver. Thank you so much! This has been driving me crazy for the past 2 weeks that I couldn’t get it to work up until now. Thank you!!

1 Like