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);
}
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().
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.