Help Sending Data From Processing to Arduino UNO.

I’m trying to control Stepper motor using processing.I’m new to processing and have written to control speed of stepper motor using slider in processing and the direction using processing GUI and then send the value to Arduino via serial port the Extract the values from the received String and then use it to control the motor . I’m sending 4 integer value by adding them to a string and separating them using comma ",’ I’m sending the values only when any value changes like of the slider i keep comparing new string and old string and if they are not same data is sent serially. but on the other side if the value is sent then arduino takes some seconds to process the change. any method to speed up the serial communications . I’m Using Arduino Uno below are the codes for both

CODES.zip (6.82 KB)

Members are often reluctant to download and open zip files. It is a pain in the butt to do it, too. If you post the code as described in the forum, you are more likely to get help.

The serial input basics tutorial may be helpful.

Sure I’m New to the forum this is my first post here are the codes,
Processing:

/*ListenTonothing*/

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

Serial serial_port = null;        // the serial port

//Initialize serial port buttons
Button btn_serial_up;              // move up through the serial port list
Button btn_serial_dn;              // move down through the serial port list
Button btn_serial_connect;         // connect to the selected serial port
Button btn_serial_disconnect;      // disconnect from the serial port
Button btn_serial_list_refresh;    // refresh the serial port list

String serial_list;                // list of serial ports
int serial_list_index = 0;         // currently selected serial port 
int num_serial_ports = 0;          // number of serial ports in the list
int pulses=0;
ControlP5 cp5;
ControlFont font;
int sliderValue = 255;//slider variable
String output=" ",poutput="00000000";
int dir=0;
int exp=0;
void setup() 
{
  background(255,204,153);
  size(600,400);
  //create buttons
  btn_serial_up = new Button("^", 140, 10, 40, 20);
  btn_serial_dn = new Button("v", 140, 50, 40, 20);
  btn_serial_connect = new Button("Connect", 190, 10, 100, 25);
  btn_serial_disconnect = new Button("Disconnect", 190, 45, 100, 25);
  btn_serial_list_refresh = new Button("Refresh", 190, 80, 100, 25);
  
  // get the list of serial ports on the computer
  serial_list = Serial.list()[serial_list_index];
  // get the number of serial ports in the list
  num_serial_ports = Serial.list().length;
  noStroke();
  cp5 = new ControlP5(this);
  font = new ControlFont(createFont("Calibri",1),12); // ControlFont(the font, font size)
  // add a horizontal sliders, the value of this slider will be linked
  // to variable 'sliderValue' 
  cp5.addSlider("sliderValue")
     .setPosition(65,150)
     .setRange(0,255)
     .setFont(font)
     .setCaptionLabel("Speed POT")
     .setColorCaptionLabel(255)
     ;
  fill(255,178,102);
  rect(55,140,175,30);
  // create a toggle and change the default look to a (on/off) switch look
  cp5.addToggle("toggle")
     .setPosition(55,200)
     .setCaptionLabel("CCW                      CW")
     .setColorCaptionLabel(255) 
     .setSize(75,30)
     .setValue(true)
     .setMode(ControlP5.SWITCH)
     ;
  cp5.addButton("pulse")
     .setPosition(150,200)
     .setValue(0)
     .setColorBackground(255)
     .setHeight(30)
     .setWidth(80)
     .setCaptionLabel("Pulse")
     ;
  ///////////////////////////////////////Experiment Button///////////////////////////////////////
  cp5.addButton("exp2")
     .setPosition(330,10)
     .setValue(0)
     .setColorBackground(255)
     .setHeight(80)
     .setWidth(120)
     .setCaptionLabel("Full Step Single\n\nPhase Free Run")
     ;
  cp5.addButton("exp3")
     .setPosition(462,10)
     .setValue(0)
     .setColorBackground(255)
     .setHeight(80)
     .setWidth(120)
     .setCaptionLabel("Full Step Single\n\nPhase Step Run")
     ;
  cp5.addButton("exp4")
     .setPosition(330,102)
     .setValue(0)
     .setColorBackground(255)
     .setHeight(80)
     .setWidth(120)
     .setCaptionLabel("Full Step Two\n\nPhase Free Run")
     ;
  cp5.addButton("exp5")
     .setPosition(462,102)
     .setValue(0)
     .setColorBackground(255)
     .setHeight(80)
     .setWidth(120)
     .setCaptionLabel("Full Step Two\n\nPhase Step Run")
     ;
  cp5.addButton("exp6")
     .setPosition(330,194)
     .setValue(0)
     .setColorBackground(255)
     .setHeight(80)
     .setWidth(120)
     .setCaptionLabel("Half Step \n\nFree Run")
     ;
  cp5.addButton("exp7")
     .setPosition(462,194)
     .setValue(0)
     .setColorBackground(255)
     .setHeight(80)
     .setWidth(120)
     .setCaptionLabel("Half Step \n\nStep Run")
     ;
  cp5.addButton("exp1")
     .setPosition(390,286)
     .setValue(0)
     .setColorBackground(255)
     .setHeight(80)
     .setWidth(120)
     .setCaptionLabel("Wobble\n\n    Mode")
     ;   
exp=0;
}

void draw() 
{
  btn_serial_up.Draw();
  btn_serial_dn.Draw();
  btn_serial_connect.Draw();
  btn_serial_disconnect.Draw();
  btn_serial_list_refresh.Draw();
  // draw the text box containing the selected serial port
  DrawTextBox("Select Port", serial_list, 10, 10, 120, 60);
  if (serial_port != null) 
  { 
    output=dir+","+exp+","+sliderValue+","+pulses+'\n';
    if(poutput.equals(output) == false)
    {
      serial_port.write(output);
      poutput = output;
      print(output);
      println(poutput);
    }
  }
}
void mousePressed() 
{
  // up button clicked
  if (btn_serial_up.MouseIsOver()) 
  {
    if (serial_list_index > 0) 
    {
      // move one position up in the list of serial ports
      serial_list_index--;
      serial_list = Serial.list()[serial_list_index];
    }
  }
  // down button clicked
  if (btn_serial_dn.MouseIsOver()) 
  {
    if (serial_list_index < (num_serial_ports - 1)) 
    {
      // move one position down in the list of serial ports
      serial_list_index++;
      serial_list = Serial.list()[serial_list_index];
    }
  }
  // Connect button clicked
  if (btn_serial_connect.MouseIsOver()) 
  {
    if (serial_port == null) 
    {
      // connect to the selected serial port
      serial_port = new Serial(this, Serial.list()[serial_list_index], 9600);
    }
  }
  // Disconnect button clicked
  if (btn_serial_disconnect.MouseIsOver()) 
  {
    if (serial_port != null) 
    {
      // disconnect from the serial port
      serial_port.stop();
      serial_port = null;
    }
  }
  // Refresh button clicked
  if (btn_serial_list_refresh.MouseIsOver()) 
  {
    // get the serial port list and length of the list
    serial_list = Serial.list()[serial_list_index];
    num_serial_ports = Serial.list().length;
  }
}

// function for drawing a text box with title and contents
void DrawTextBox(String title, String str, int x, int y, int w, int h)
{
  fill(255);
  rect(x, y, w, h);
  fill(0);
  textAlign(LEFT);
  textSize(14);
  text(title, x + 10, y + 10, w - 20, 20);
  textSize(12);  
  text(str, x + 10, y + 40, w - 20, h - 10);
}

// button class used for all buttons
class Button 
{
  String label;
  float x;    // top left corner x position
  float y;    // top left corner y position
  float w;    // width of button
  float h;    // height of button
  
  // constructor
  Button(String labelB, float xpos, float ypos, float widthB, float heightB) 
  {
    label = labelB;
    x = xpos;
    y = ypos;
    w = widthB;
    h = heightB;
  }
  
  // draw the button in the window
  void Draw() 
  {
    fill(218);
    stroke(141);
    rect(x, y, w, h, 10);
    textAlign(CENTER, CENTER);
    fill(0);
    text(label, x + (w / 2), y + (h / 2));
  }
  
  // returns true if the mouse cursor is over the button
  boolean MouseIsOver() 
  {
    if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) 
    {
      return true;
    }
    return false;
  }
}
public void exp2() 
{
  exp=2;
}
public void exp3() 
{
  exp=3;
}
public void exp4() 
{
  exp=4;
}
public void exp5() 
{
  exp=5;
}
public void exp6() 
{
  exp=6;
}
public void exp7() 
{
  exp=7;
}
public void exp1() 
{
  exp=1;
}
public void pulse() 
{
  pulses++;
}
void toggle(boolean theFlag) 
{
  if(theFlag==true) //Direction of the motor 
  {
    dir=0;
  } 
  else 
  {
    dir=1;
  }
  
}

Arduino:

/*ListenTonothing*/
#include <LiquidCrystal.h>
const int rs = 12, en = 13, d4 = 8, d5 = 9, d6 = 10, d7 = 11;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int button=1;//Direction Variable
int pulser = 2;//pin
int select = A4;//Select  Pin
int pulse = 1;
int FSSPpulse = 1;
String inString = "";
unsigned long debounce=0;//Last Button Pressed
int asd=500;//Min time between two presses
int option=1;//Exp Select Variable

//Stepper Pins
int A=4;
int A8=5;
int B=6;
int B8=7;

int expsel=0;
const int numberOfPieces = 4;
String pieces[numberOfPieces];
String input = "";
int counter = 0;
int lastIndex = 0;
int toggle=0;//Direction
int pwm=0;//SpeedPOT
int ppulses=0;
int flag=0;
int pulses;
void setup()
{
  pinMode(A,OUTPUT);
  pinMode(A8,OUTPUT);
  pinMode(B,OUTPUT);
  pinMode(B8,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  pinMode(3,INPUT);//interrupt
  pinMode(pulser,INPUT);//Pulser
  pinMode(A3,INPUT);//POT Delay
  pinMode(select,INPUT);
  lcd.begin(20, 4);
  lcd.setCursor(0,0);
  lcd.print("Select COM port");
  lcd.setCursor(0,1);
  lcd.print("on PC");
  debounce=millis();
  Serial.begin(9600);
  while(!Serial)
  {
      
  }
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Select");
  lcd.setCursor(0,1);
  lcd.print("Experiment:");
}

void loop() 
{
  
  if(expsel == 3)
  {
    lcd.setCursor(0,0);
    lcd.print("Full Step Single");
    lcd.setCursor(0,1);
    lcd.print("Phase Step Run");
    if(ppulses != pulses)
    {
      ppulses =  pulses;
      flag=1;
    }        
    exp7(1,1);
  }
  if(expsel == 2)
  {
    lcd.setCursor(0,0);
    lcd.print("Full Step Single");
    lcd.setCursor(0,1);
    lcd.print("Phase Free Run");
    freeFSSP();
  }
  if(expsel == 5)
  {
    lcd.setCursor(0,0);
    lcd.print("Full Step Two");
    lcd.setCursor(0,1);
    lcd.print("Phase Step Run");
    if(ppulses != pulses)
    {
      ppulses =  pulses;
      flag=1;
    }
    exp7(1,2);
  }
  if(expsel == 4)
  {
    lcd.setCursor(0,0);
    lcd.print("Full Step Two");
    lcd.setCursor(0,1);
    lcd.print("Phase Free Run");
    freeFSTP();
  }
  if(expsel == 7)
  {
    lcd.setCursor(0,0);
    lcd.print("Half Step Step");
    lcd.setCursor(0,1);
    lcd.print("Run");
    if(ppulses != pulses)
    {
      ppulses =  pulses;
      flag=1;
    }
    exp7(2,1);
  }
  if(expsel == 6)
  {
    lcd.setCursor(0,0);
    lcd.print("Half Step Free");
    lcd.setCursor(0,1);
    lcd.print("Run");
    freeHS();
  }
  if(expsel == 1)
  {
    lcd.setCursor(0,0);
    lcd.print("Wobble Mode");
    WM();
  }
  
}

void FSSPM(int i)
{
  switch(i)
  {
    case 1: digitalWrite(A,HIGH);
            digitalWrite(A8,LOW);
            digitalWrite(B,LOW);
            digitalWrite(B8,LOW);
            break;
    case 2: digitalWrite(A,LOW);
            digitalWrite(A8,HIGH);
            digitalWrite(B,LOW);
            digitalWrite(B8,LOW);
            break;
    case 3: digitalWrite(A,LOW);
            digitalWrite(A8,LOW);
            digitalWrite(B,HIGH);
            digitalWrite(B8,LOW);
            break;
    case 4: digitalWrite(A,LOW);
            digitalWrite(A8,LOW);
            digitalWrite(B,LOW);
            digitalWrite(B8,HIGH);
            break;
    default:break;        
  }
}
void stepFSSP()
{
  if(flag==1)//Full Step Single Phase Step Running
  {
    if(button==1)
    {
     FSSPpulse++;
     if(FSSPpulse>4)
     {
      FSSPpulse=1;
     }
     FSSPM(FSSPpulse); 
     delay(400);
    }
    if(button==0)
    {
     FSSPpulse--;
     if(FSSPpulse<1)
     {
      FSSPpulse=4;
     }
     FSSPM(FSSPpulse);
     delay(400); 
    }
    flag=0;
  }
}

void freeFSSP()
{
  if(button==1)//Full Step Single Phase Free Running 
  {
     for(int i=1; i<5; i++)
     {
        FSSPM(i);
        delay(delayPOT());
     }
  }
  if(button==0)
  {
     for(int i=4; i>0; i--)
     {
        FSSPM(i);
        delay(delayPOT());
     }
  }
}
void FSTP(int i)
{
  switch(i)
  {
    case 1: digitalWrite(A,HIGH);
            digitalWrite(A8,HIGH);
            digitalWrite(B,LOW);
            digitalWrite(B8,LOW);
            break;
    case 2: digitalWrite(A,LOW);
            digitalWrite(A8,HIGH);
            digitalWrite(B,HIGH);
            digitalWrite(B8,LOW);
            break;
    case 3: digitalWrite(A,LOW);
            digitalWrite(A8,LOW);
            digitalWrite(B,HIGH);
            digitalWrite(B8,HIGH);
            break;
    case 4: digitalWrite(A,HIGH);
            digitalWrite(A8,LOW);
            digitalWrite(B,LOW);
            digitalWrite(B8,HIGH);
            break;
    default:break;
  }
}

void stepFSTP()
{
  if(flag==1)//Full Step Two Phase Step Running
  {
    if(button==1)
    {
     pulse++;
     if(pulse>4)
     {
      pulse=1;
     }
     FSTP(pulse); 
     delay(400);
    }
    if(button==0)
    {
     pulse--;
     if(pulse<1)
     {
      pulse=4;
     }
     FSTP(pulse);
     delay(400); 
    }
    flag=0;
  }
}

void freeFSTP()
{
  if(button==1)//Full Step Two Phase Free Running 
  {
     for(int i=1; i<5; i++)
     {
        FSTP(i);
        delay(delayPOT());
     }
  }
  if(button==0)
  {
     for(int i=4; i>0; i--)
     {
        FSTP(i);
        delay(delayPOT());
     }
  }
}
void HS(int i)
{
  switch(i)
  {
    case 1: digitalWrite(A,HIGH);
            digitalWrite(A8,LOW);
            digitalWrite(B,LOW);
            digitalWrite(B8,LOW);
            break;
    case 2: digitalWrite(A,HIGH);
            digitalWrite(A8,HIGH);
            digitalWrite(B,LOW);
            digitalWrite(B8,LOW);
            break;       
    case 3: digitalWrite(A,LOW);
            digitalWrite(A8,HIGH);
            digitalWrite(B,LOW);
            digitalWrite(B8,LOW);
            break;
    case 4: digitalWrite(A,LOW);
            digitalWrite(A8,HIGH);
            digitalWrite(B,HIGH);
            digitalWrite(B8,LOW);
            break;       
    case 5: digitalWrite(A,LOW);
            digitalWrite(A8,LOW);
            digitalWrite(B,HIGH);
            digitalWrite(B8,LOW);
            break;
    case 6: digitalWrite(A,LOW);
            digitalWrite(A8,LOW);
            digitalWrite(B,HIGH);
            digitalWrite(B8,HIGH);
            break;       
    case 7: digitalWrite(A,LOW);
            digitalWrite(A8,LOW);
            digitalWrite(B,LOW);
            digitalWrite(B8,HIGH);
            break;
    case 8: digitalWrite(A,HIGH);
            digitalWrite(A8,LOW);
            digitalWrite(B,LOW);
            digitalWrite(B8,HIGH);
            break; 
    default:break;      
    
  }
}

void stepHS()
{
  if(flag==1)//Half Step Step Running
  {
    if(button==1)
    {
     FSSPpulse++;
     if(FSSPpulse>8)
     {
      FSSPpulse=1;
     }
     HS(FSSPpulse); 
     delay(400);
    }
    if(button==0)
    {
     FSSPpulse--;
     if(FSSPpulse<1)
     {
      FSSPpulse=8;
     }
     HS(FSSPpulse);
     delay(400); 
    }
    flag=0;
  }  
}

void freeHS()
{
  if(button==1)//Half Step Free Running 
  {
    for(int j=1; j<9; j++)
    {
      HS(j);
      delay(delayPOT());
    }   
  }
  if(button==0)
  {
    for(int j=8; j>0; j--)
    {
      HS(j);
      delay(delayPOT());
    }
  }
}
void serialEvent()
{
  if(Serial.available()>0)
  {
    char ch = Serial.read();
    if (ch == '\n') 
    {
      for (int i = 0; i < input.length(); i++) 
      {
        if (input.substring(i, i+1) == ",") 
        {
          pieces[counter] = input.substring(lastIndex, i);
          lastIndex = i + 1;
          counter++;
        }
        if (i == input.length() - 1) 
        {
          pieces[counter] = input.substring(lastIndex, i);
        }
      }
      input = "";
      counter = 0;
      lastIndex = 0;
      button = pieces[0].toInt();
      expsel = pieces[1].toInt();
      pwm = pieces[2].toInt();
      pulses = pieces[3].toInt();
    }
    else 
    {
      input += ch;
    }
  }  
}
void WM()
{
  FSSPM(1);
  delay(20);
  FSSPM(4);
  delay(20);
}
int delayPOT()
{
  int halu;
  halu=pwm;
  halu=map(halu,0,255,10,1000);
  return halu;
}
void exp7(int a,int b)//Step Movement
{
  while(a==1 && b==1)
  {
    stepFSSP();
  }
  while(a==1 && b==2)
  {
    stepFSTP();
  }
  while(a==2)
  {
    stepHS();
  }
  
}

If you described what you want the code to do.

What is not working?

The Code is working perfectly but the thing is i have used a slider to vary the speeds of the motor , so if i change the slider value the motor speed should change respectively . but the thing is that it takes time for the speed to change like there are 7 modes also for the stepper control if the mode is changed or the speed is varied the arduino takes minimum 5 sec to respond to the change. I would like to know how to speed up the process.

You are using delay() in the code. The delay() functions stops all program execution (blocks) and can make the program unresponsive. Better to use the millis() and/or micros() functions to do non-blocking timing.

Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

yeah i figured that out but Thanks a lot groundFungus for the Tip:)