Sending Variables from Processing to Arduino

Hi there!

I wrote the following Scripts to control a Stepper-Motor via Processing:

The Arduino Script looks like this:

/*
 * interface map with Processing to light up LEDs on arduino
 * Works with Processing sketch 'pdxMap'
 */
#include <Stepper.h>

// Map our pins to constants to make things easier to keep track of
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;

// The amount of steps for a full revolution of your motor.
// 360 / stepAngle
const int STEPS = 200;

Stepper myStepper(STEPS, dirA, dirB);

int Pin[] = {0,2,3,4,5,6,7,8,9};  //Array of pins to controll lights
int value = 0;  // variable to keep the actual value

void setup() 
{ 
 //Initialize pins as OUTPUTS and begin serial connection
 for(int i=1; i<9 ; i++){
  pinMode(Pin[i], OUTPUT); 
 }
  // Set the RPM of the motor
  myStepper.setSpeed(mm);

  // Turn on pulse width modulation
  pinMode(pwmA, OUTPUT);
  digitalWrite(pwmA, HIGH);
  pinMode(pwmB, OUTPUT);
  digitalWrite(pwmB, HIGH);

  // Turn off the brakes
  pinMode(brakeA, OUTPUT);
  digitalWrite(brakeA, LOW);
  pinMode(brakeB, OUTPUT);
  digitalWrite(brakeB, LOW);


  Serial.begin(9600);

} 
 
void loop() 
{
  int input = Serial.read();  // read serial 
  switch (input){
    case 1:  //If processing passes a '1' do case one
      if(input == 1){
     
    
  // Pause
        

  // Move the motor X amount of steps the other way
          myStepper.step(-STEPS);
          Serial.println(-5*STEPS);
  // Pause
         
        
       }
       break;
    case 2:
      if(input == 2){  //If Processing passes a '2' do case two
      myStepper.step(STEPS);
      Serial.println(5*STEPS);
       }
       break;
    
  }
}

And the crucial part of my Processing Script like this:

import g4p_controls.*;
import processing.serial.*;
Serial myPort;
int a = 200;


public void setup(){
  size(480, 320, JAVA2D);
  createGUI();
  customGUI();
  // Place your setup code here
   println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.buffer(1);

public void button1_click1(GButton source, GEvent event) { //_CODE_:button1:475985:
  println("button1 - GButton event occured " + System.currentTimeMillis()%10000000 );
  label1.setText ("Hebt");
   myPort.write(1); 
} //_CODE_:button1:475985:

public void button2_click1(GButton source, GEvent event) { //_CODE_:button2:790988:
  println("button2 - GButton event occured " + System.currentTimeMillis()%10000000 );
  label1.setText ("Senkt");
  myPort.write(2); 
} //_CODE_:button2:790988:

public void textfield1_change1(GTextField source, GEvent event) { //_CODE_:mm:249761:
  println("textfield1 - GTextField event occured " + System.currentTimeMillis()%10000000 );
} //_CODE_:mm:249761:



// Create all the GUI controls. 
// autogenerated do not edit
public void createGUI(){
  G4P.messagesEnabled(false);
  G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
  G4P.setCursor(ARROW);
  if(frame != null)
    frame.setTitle("Input");
  button1 = new GButton(this, 288, 48, 80, 30);
  button1.setTextBold();
  button1.setText("Heben");
  button1.setLocalColorScheme(GCScheme.GREEN_SCHEME);
  button1.addEventHandler(this, "button1_click1");
  button2 = new GButton(this, 288, 104, 80, 30);
  button2.setTextBold();
  button2.setText("Senken");
  button2.setLocalColorScheme(GCScheme.RED_SCHEME);
  button2.addEventHandler(this, "button2_click1");
  label1 = new GLabel(this, 384, 72, 80, 40);
  label1.setTextBold();
  label1.setOpaque(true);
  mm = new GTextField(this, 8, 120, 32, 16, G4P.SCROLLBARS_NONE);
  mm.setOpaque(true);
  mm.addEventHandler(this, "textfield1_change1");
}

// Variable declarations 
// autogenerated do not edit
GButton button1; 
GButton button2; 
GLabel label1; 
GTextField mm;

I made the Processing Script with the help of the awesome GUIbuilder ( Quarks Place )

What i was able to do so far: When i press "button1", the Stepper drives a fixed amount of steps in one direction, "button2" in the other direction.
As i'm quite new to all of this, i lack the knowledge of some basics.
I basically want to type a number in my Textfield1

public void textfield1_change1(GTextField source, GEvent event) { //_CODE_:mm:249761:
  println("textfield1 - GTextField event occured " + System.currentTimeMillis()%10000000 );
} //_CODE_:mm:249761:

which defines my Variable "mm" which works so far.
Now i want to have this Variable as my motor speed on my Arduino

myStepper.setSpeed(mm)

I know it can't work like i this, it is just for the illustration.
As far as i understood it, i can just send specific digits or combination of digits over to my Arduino uni via serial I/O. (Like "A", or in my case 1 and 2)
Now what method could i use to make my Arduino recognize, that the number i just sent over (lets say i typed "80" in my textfield1), is the number he should put in the setSpeed brackets?
Is there a way at all?

Thank you for your time and answers! I hope i could make myself clear, if not feel free to ask.

If you limit commands to using character (A-Z, a-z) and values to numbers (0-9) then something like excerpt below will do it. The code is expecting something like n500

  while (Serial.available() > 0){                           // Serial available? Read it.
    unsigned int cmdData;
    byte cmdByte = Serial.read();                           // Read command
    if (cmdByte >= 'A' && cmdByte <= 'Z'){
      cmdByte |= 0X20;                                      // Convert to lower case
    }
    switch(cmdByte) {
        case 'n'
        Serial.print(F("NOISE >"));
        cmdData = Serial.parseInt();                        // Read number value
        noInterrupts();
        noiseThreshold = (unsigned long)cmdData;
        interrupts();
        Serial.print(cmdData);
        Serial.println(F("<"));
        break;

Yeah, that's would be in easy solution, thanks for that.
The problem is, as my GUI grows, there will be a lot of values (e.g. delays, amount of steps, ...). I could of course use different intervalls for different functions, like 1-100 for the speed, 101-1000 for the delays etc., but isn't there a more elegant solution to make the arduino distinct them from one another?
Can i just use Integer Values or fixed comma numbers, too?
Sorry for the beginner questions, i haven't learned a new programming language for 15 years.

The example was only an excerpt, just expand the commands as needed. You could us something like s100 for 100 steps, d10 for delay 10 and so on. They can also be concatenated onto a single line so you could have s100 d10.

thank you very much good sir, i will try that out