Problem With GUI for stepper motors

Hi,
I have build the XY slider with the help of two stepper motors and CNC shield now my program is working smoothly. I want to create the GUI in which any one can increase or decrease the delay. Please help me
i have used processing platform to write the code of GUI
will it be possible to use Processing for doing that

my arduino code is as given below

#define EN        8  

//Direction pin
#define X_DIR     5 
#define Y_DIR     6
//#define Z_DIR     7

//Step pin
#define X_STP     2
#define Y_STP     3 
//#define Z_STP     4 


//A4988
int delayTime=900; //Delay between each pause (uS)
int stps=2000 ;// Steps to move
int steps=5000;//steps to move in reverse direction  

void step(boolean dir, byte dirPin, byte stepperPin, int steps)

{

  digitalWrite(dirPin, dir);

  delay(100);

  for (int i = 0; i < steps; i++) {

    digitalWrite(stepperPin, HIGH);

    delayMicroseconds(delayTime); 

    digitalWrite(stepperPin, LOW);

    delayMicroseconds(delayTime); 

  }

}

void setup(){

  pinMode(X_DIR, OUTPUT); pinMode(X_STP, OUTPUT);
  pinMode(Y_DIR, OUTPUT); pinMode(Y_STP, OUTPUT);
 // pinMode(Z_DIR, OUTPUT); pinMode(Z_STP, OUTPUT);
  pinMode(EN, OUTPUT);
  //digitalWrite(EN, LOW);
}

void loop(){
  step(false, X_DIR, X_STP, stps); //X, Clockwise
  delay(20000); // dipping time in solution 
  step(true, X_DIR, X_STP, stps); //X, Counterclockwise
  delay(1000);
    step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  delay(1000);
  
  step(false, X_DIR, X_STP, stps); //X, Clockwise
  delay(20000); // dipping time distill water
  step(true, X_DIR, X_STP, stps); //X, Counterclockwise
  delay(1000);
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  delay(1000);
  
  step(false, X_DIR, X_STP, stps); //X, Clockwise
  delay(20000); // dipping time in solution
  step(true, X_DIR, X_STP, stps); //X, Counterclockwise
  delay(1000);
  step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
  delay(1000);
  
  step(false, X_DIR, X_STP, stps); //X, Clockwise
  delay(20000); // dipping time distilled water
  step(true, X_DIR, X_STP, stps); //X, Counterclockwise
  delay(1000);
  
step(true, Y_DIR, Y_STP, steps); //Y, Counterclockwise
/*step(true, Y_DIR, Y_STP, stps); //Y, Counterclockwise
step(true, Y_DIR, Y_STP, stps); //Y, Counterclockwise
step(true, Y_DIR, Y_STP, stps); //Y, Counterclockwise*/
delay(1000);
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

You are going to have to take all the “magic numbers” out of your code - for example

 delay(20000); // dipping time in solution

Create variables with meaningful names for all those values.

Then you can add code to your Arduino program to receive a number (or several numbers) from Processing to update the value in one or more of your variables.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

I am not familiar with Processing.

…R

PS … When posting code please use the code button </>

so your code looks like this

and is easy to copy to a text editor See How to use the Forum

Also please use the AutoFormat tool to indent your code for easier reading.

I only remember a library that makes an Arduino a Processing slave, so that everything except basic I/O has to be done in Processing. Try to find an example that allows any Arduino program communicate with Processing.

Thanks guy’s
That work fine with serial communication

Now i want to build a GUI by using processing for same problem which i have mentioned earlier
i wrote a code for that also when i execute the code i got the window of GUI
but that window is unable to communicate with arduino and my system

Please guide me
Arduino Code
#define EN 8

//Direction pin
#define X_DIR 5
#define Y_DIR 6
//#define Z_DIR 7

//Step pin
#define X_STP 2
#define Y_STP 3
//#define Z_STP 4

//A4988
int delayTime=900; //Delay between each pause (uS)
int stps=2000 ;// Steps to move
int a,b,c,d,e;
void step(boolean dir, byte dirPin, byte stepperPin, int steps)

{

digitalWrite(dirPin, dir);
delay(100);
for (int i = 0; i < steps; i++) {
digitalWrite(stepperPin, HIGH);
delayMicroseconds(delayTime);
digitalWrite(stepperPin, LOW);
delayMicroseconds(delayTime);
}
}

void setup()
{
pinMode(X_DIR, OUTPUT); pinMode(X_STP, OUTPUT);
pinMode(Y_DIR, OUTPUT); pinMode(Y_STP, OUTPUT);
// pinMode(Z_DIR, OUTPUT); pinMode(Z_STP, OUTPUT);
pinMode(EN, OUTPUT);
//digitalWrite(EN, LOW);
Serial.begin(9600) ;
}

void loop()
{
Serial.begin(9600) ;
while(Serial.available()==0){}
a=Serial.parseInt();
Serial.begin(9600) ;
while(Serial.available()==0){}
b=Serial.parseInt();
Serial.begin(9600) ;
while(Serial.available()==0){}
c=Serial.parseInt();
while(Serial.available()==0){}
d=Serial.parseInt();
Serial.begin(9600) ;
while(Serial.available()==0){}
e=Serial.parseInt();

for(int i=1; i<=a; i++)
{

step(false, X_DIR, X_STP, stps); //X, Clockwise

if(Serial.available()>=0)
{
step(false, X_DIR, X_STP, stps); //X, Clockwise
b=Serial.read();
delay(b); // dipping time in solution
step(true, X_DIR, X_STP, stps); //X, Counterclockwise
delay(1000);
step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
delay(1000);
step(false, X_DIR, X_STP, stps); //X, Clockwise

step(false, X_DIR, X_STP, stps); //X, Clockwise
c=Serial.read();
delay(c); // dipping time in solution
step(true, X_DIR, X_STP, stps); //X, Counterclockwise
delay(1000);
step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
delay(1000);

step(false, X_DIR, X_STP, stps); //X, Clockwise
d=Serial.read();
delay(d); // dipping time in solution
step(true, X_DIR, X_STP, stps); //X, Counterclockwise
delay(1000);
step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
delay(1000);

step(false, X_DIR, X_STP, stps); //X, Clockwise
e=Serial.read();
delay(e); // dipping time in solution
step(true, X_DIR, X_STP, stps); //X, Counterclockwise
delay(1000);
step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
delay(1000);
}

step(true, Y_DIR, Y_STP, stps); //Y, Counterclockwise
step(true, Y_DIR, Y_STP, stps); //Y, Counterclockwise
step(true, Y_DIR, Y_STP, stps); //Y, Counterclockwise
step(true, Y_DIR, Y_STP, stps); //Y, Counterclockwise*/
delay(1000);
}

}

Processing Code is

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

Serial port;

ControlP5 cp5; //create ControlP5 object
PFont font, FONT;
String text;
int a,b,c,d,e;

void setup(){ //same as arduino program
textAlign(CENTER,CENTER);

size(1000, 7000); //window size, (width, height)

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

//port = new Serial(this, “COM3”, 9600); //i have connected arduino to com3, it would be different in linux and mac os

//lets add buton to empty window

cp5 = new ControlP5(this);
font = createFont(“Times New Roman”, 20); // custom fonts for buttons and title
FONT = createFont(“Times New Roman”, 10);

cp5.addButton(“Enter the Number of dipping cycles”) //“red” is the name of button

.setPosition(100, 150) //x and y coordinates of upper left corner of button
.setSize(500, 70) //(width, height)
.setFont(font)
;
cp5.addTextfield(“a”).setPosition(700,150).setSize(100,70).setFont(font);

cp5.addButton(“Enter the Dipping time in First Precourser”) //“red” is the name of button

.setPosition(100, 250) //x and y coordinates of upper left corner of button
.setSize(500, 70) //(width, height)
.setFont(font)
;
cp5.addTextfield(“b”).setPosition(700,250).setSize(100,70).setFont(font);

cp5.addButton(“Enter the Dipping time in Distilled Water1”) //“yellow” is the name of button
.setPosition(100, 350) //x and y coordinates of upper left corner of button
.setSize(500, 70) //(width, height)
.setFont(font)
;
cp5.addTextfield(“c”).setPosition(700,350).setSize(100,70).setFont(font);

cp5.addButton(“Enter the Dipping time in Second Precourser”) //“blue” is the name of button
.setPosition(100, 450) //x and y coordinates of upper left corner of button
.setSize(500, 70) //(width, height)
.setFont(font)
;
cp5.addTextfield(“d”).setPosition(700,450).setSize(100,70).setFont(font);

cp5.addButton(“Enter the Dipping time in Distilled Water2”) //“alloff” is the name of button
.setPosition(100, 550) //x and y coordinates of upper left corner of button
.setSize(500, 70) //(width, height)
.setFont(font)

;
cp5.addTextfield(“e”).setPosition(700,550).setSize(100,70).setFont(font);

cp5.addButton(“Submit”).setPosition(380, 650).setSize(150, 100).setFont(font);

}

void draw(){ //same as loop in arduino

background(#FFCE71); // background color of window (r, g, b) or (0 to 255)

//lets give title to our window
fill(0,2, 0); //text color (r, g, b)
textFont(font);
text(“Welcome to Solid State Physics Research Laboratory, Department of Physics, ACA”, 480, 30); // (“text”, x coordinate, y coordinat)
text(“Sequential Ionic Layer Adsorption Reaction (SILAR) deposition System “, 470, 80);
textFont(FONT);
text(” NOTE - Dipping time in milisecond [1sec=1000]”, 800,130);
}

//lets add some functions to our buttons
//so whe you press any button, it sends perticular char over serial port
void Submit()
{

port.write(‘a’);
port.write(‘b’);
port.write(‘c’);
port.write(‘d’);
port.write(‘e’);

}

Hi,

Code Tags please!

Tom… :slight_smile:

To make it easy for people to help you please modify your post and use the code button </>

so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor. The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.

…R