Can Arduino receive negative numbers via Processing?

Hi, I'm currently having an issue reading my data via processing through serial. I'm using OpenCV blob tracking to track the X axis of the biggest blob detected and converting the values of 0-640 to positive and negative values in a range of -3600 to 3600 for a stepper motor's steps. Essentially, the stepper motor will drive a fixture that is fitted on a threaded rod linear motion device, based on the x position of the blob, thus following the blobs motion. I'm sending the positive and negative values through myPort.write, though when reading in Arduino (after calling println(incSteps,DEC)), the serial monitor only prints a bunch of -1, 0 and the occasional -10. Printing in processing give me the data I want, so I'm thinking it has to be with the way I'm calling Serial.available > ? cause I've always been confused with that. Or might be that arduino does not allow for negative numbers to be sent over serial?? Anyways heres my code.

Processing:

println(int(stepFunc(pole3));
myPort.write(int(stepFunc(pole3))); //range between -3600 to 3600

Arduino:

#include <Stepper.h>

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on the motor shield
Stepper myStepper(stepsPerRevolution, 12,13);

// give the motor control pins names:
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
int incSteps;

int x = 0;
void setup() {
Serial.begin(9600);
// set the PWM and brake pins so that the direction pins // can be used to control the motor:
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);

// initialize the serial port:
Serial.begin(9600);
// set the motor speed (for multiple steps only):
myStepper.setSpeed(50);
}

void loop() {
while(Serial.available() > 0){
incSteps = Serial.read();
Serial.println(incSteps,DEC);
myStepper.step(incSteps);

}
delay(1000);
}

Thanks!

The data is being sent as strings. You are assuming, as you read it, that you are reading int values. That is not a valid assumption. Either read the data, one char at a time, and store the data in an array (NULL terminated) of chars and then use atoi() when the first non-digit arrives, or use Serial.parseInt().

I think I know what you're saying. Would it be easier to go with parseInt()?

void loop() {

incSteps = Serial.parseInt();
Serial.print("steps = "); Serial.println(incSteps, DEC);

while(Serial.available() >= 0){
trig = Serial.read();
incSteps = Serial.read();

if(trig == 'P'){ //P for Positive
myStepper.step(incSteps);
}
if(trig == 'N'){ //N for Negative
myStepper.step(-incSteps);
}
}
delay(1000);
}

?? This should work right? Could you send me an example of how to do it with parseInt()?

This should work right?

Why call parseInt() when there may be nothing to read? Only call parseInt() is there IS something to read.

      incSteps = Serial.read();

No. You are going to overwrite the value that you correctly read.

while(Serial.available() >= 0){

Why are you reading data when there is 0 data in the buffer?

ok so :

incSteps = Serial.parseInt();
Serial.print("steps = "); Serial.println(incSteps, DEC);

must be within:

while(Serial.available() > 0){

}

thus:

void loop() {

  while(Serial.available() > 0){

      trig = Serial.read(); //if(steps > 0) = 'P' //if(steps < 0) = 'N' (executed in processing)
      incSteps = Serial.parseInt();
      Serial.print("steps = "); Serial.println(incSteps, DEC);
      
      if(trig == 'P'){ //P for Positive
        myStepper.step(incSteps);
      }
      if(trig == 'N'){ //N for Negative
        myStepper.step(-incSteps);
      }
  }
  delay(1000);
}

I'm not sure if I really understand how parseInt() works? Is this correct?

You tell us. Does it work?

no can you help

no

Proof?

What is the Processing app sending to the serial port? What is the Arduino receiving?

  while(Serial.available() > 0){

      trig = Serial.read(); //if(steps > 0) = 'P' //if(steps < 0) = 'N' (executed in processing)
      incSteps = Serial.parseInt();
      Serial.print("steps = "); Serial.println(incSteps, DEC);

If there is at least one character in the serial buffer, read it and all the characters that make up the int. Got it. I'd ask how that's working for you, but I already know.