need help to control 2 steppers with processing

Hello everyone

i’m trying to control 2 stepper motors with processing, as for now i’m managing to control the x-axis (all this is for a cnc!).
would need your help to find the answer for the y-axis

everything works fine with the arduino code (when the serial monitor does it) but i’m not really succeeding on making it talk to processing

i hope someone will help me with this one!!!

thank you!!! :slight_smile:

ARDUINO CODE

// initialize of the Stepper library:
#include <Stepper.h>
#define RAZ 12


Stepper tete(400, 10,11, 8,9); //position z
Stepper plateau(400,  2, 3, 4, 5); /// position Y

// stock position 
int positionX = 0;
int positionY = 0;

// position wanted
int targetX = 0;
int targetY = 0;

// reception from serial port
char sInput[5];
int serInputByte = 0;

// sensor, isarrived
boolean isArrived = false;

void setup() {

  // set the motor speed
  tete.setSpeed(40);
  plateau.setSpeed(40);
  // Initialize the Serial port:
  Serial.begin(9600);

}

void loop() {


  if (Serial.available()>0) {


    char val = Serial.read();

    if ((val!='x') && (val!='y')) {
      sInput[serInputByte] = val;

      if (serInputByte<5) {
        serInputByte++;
      } 
      else {
        serInputByte = 0;
        Serial.println("ERROR MORE THAN 4 BYTES IN COMMUNICATION!");
      }   

    } 
    else {
      int number = atoi(sInput);

      if (val=='x') targetX = number; 
      else
     // (val=='y')
        targetY = number;

      isArrived = false;

      serInputByte = 0;
      sInput[0] = 0;
      sInput[1] = 0;
      sInput[2] = 0;
      sInput[3] = 0;
      sInput[4] = 0;
    }
  }

  /// multitasking steppers

  int xTarg = targetX-positionX;
  int yTarg = targetY-positionY;


  if (xTarg!=0) {

    if (xTarg>0) { // go forward if smaller


      if (positionX<1170) { /// security

        tete.step(1);
        positionX++;
      } 
      else {
        positionX = 1170;
        targetX = 1170;
        Serial.println("uuuups X > 1170, going to break the machine!!!");
      }
    } 
    else { /// backwards if bigge


      if ((digitalRead(RAZ))==0) {   /// security
        tete.step(-1);
        positionX--;
      } 
      else {
        positionX = 0;
        targetX = 0;
        Serial.println("uuuups -X, going to break the machine!!!");
      }



    }

  }


  if (yTarg!=0) {

    if (yTarg>0) {

      plateau.step(1);
      positionY++;
    } 
    else {
      plateau.step(-1);
      positionY--;
    }
  }

  /// sync with computer, isarrived
  if ((xTarg==0)&&(yTarg==0)) {
    if (isArrived!=true)
      Serial.println('o');
    isArrived = true;
  }


}
///1170


void raz() {
  /// back to zero
  while(digitalRead(RAZ)!=1) {
    tete.step(-1); 
  }
  positionX = 0;
  positionY = 0; 
}

PROCESSING CODE

import processing.serial.*;

Serial myPort;
boolean sendData = false;

int x = 0;
int y = 0;

void setup() {
  
  size(600,600);
  println(Serial.list());

myPort = new Serial(this, Serial.list()[0], 9600);

  
}

void draw() {
background(30);

x = mouseX;

 detectAndSend();
}

void detectAndSend() {
  
  char inByte = 0;
  while(myPort.available() > 0) {
   inByte =  myPort.readChar();
   if (inByte=='o') {
     sendData = true;
   } else {
     println(inByte);
   }
   
  }
  
  if (sendData==true) {
    String xStr = str(x) + 'y';
    myPort.write(xStr);
  }
  

}


 }