Hi, I've been struggling with a piece of code for a couple of days and I'm almost there but not quite. I wonder if anyone can tell me what I'm doing wrong.
first and foremost let me paste my code:
void loop() {
while (Serial.available()) {
delay(10);
char c = Serial.read();
string.concat(c);} // combines, or concatenates two strings into one new string.
// so, 'c' is appended to the string named 'string'
if (string.length() >0) {
Serial.println(string);
string.toCharArray(array, sizeof(array)); // copies the string's characters to the supplied buffer.
// in this case, the buffer is called 'array'
int n;
n = atoi(array); // converts the string to an integer
// the motor library I am using requres an integer as input
}
incomingByte = Serial.read();
if (incomingByte == 'Y') {
string.toCharArray(array, sizeof(array)); // copies the string's characters to the supplied buffer.
// in this case, the buffer is called 'array'
int n;
n = atoi(array); // converts the string to an integer
// the motor library I am using requres an integer as input
Ystepper.move(n);
Ystepper.runToPosition();
}
incomingByte = Serial.read();
if (incomingByte == 'Y') {
string = "0";
}
}
the problem I am having is as follows:
the data "char c = Serial.read();" is being sent from Processing. its a simple text box into which I am entering a value (distance) which instructs a motor to travel the given value. if i enter '1000' and send it to Arduino, it puts this information into the string called "string". so far all is good. the problem arises when I want to enter a new value, say '55'. What I suspect is happening is that this new value is being added on to the end of the first value, so it becomes 100055. What I would like to do is to clear the string before I enter a new value. I have attempted to achieve this via a "CLEAR" button on the processing side, which is received on the arduino side as follows:
incomingByte = Serial.read();
if (incomingByte == 'Y') {
string = "0";
In effect, the string is set to 0, and it wont allow me to enter any new values in.
This is my interpretation of the events, I may well be wrong. Perhaps you would have a look at the code and see if you agree. What can I do to reset the string before entering a new value?
I hope others find this thread useful.
Best, Arthur