Hello, i hope someone can help me.
I send via serial connection a String like
a1000b3000 to my arduino board.
The Arduino board receives the input stream and separates the String into 1000 and 3000 with the help of isdigit().
My problem is:
If i send a String including a signed digit like
a-1000b-3000, it does not work anymore.
Is there an easy way to solve this problem?
My Code is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//Motor 1
#define DIR_PIN1 4
#define STEP_PIN1 5
//Motor 2
#define DIR_PIN2 3
#define STEP_PIN2 2
// the possible states of the state-machine
typedef enum { NONE, Mot_1, Mot_2 } states;
// current state-machine state
states state = NONE;
// current partial number
int unsigned currentValue;
int steps = 0;
void setup ()
{
pinMode(DIR_PIN1, OUTPUT);
pinMode(STEP_PIN1, OUTPUT);
pinMode(DIR_PIN2, OUTPUT);
pinMode(STEP_PIN2, OUTPUT);
Serial.begin (9600);
state = NONE;
} // end of setup
void processMotor1 (const unsigned int value)
{
// do something with MOTOR1
Serial.print ("MOTOR1 = ");
Serial.println (value);
rotate1(currentValue, .5);
} // end of processRPM
void processMotor2 (const unsigned int value)
{
// do something with MOTOR2
Serial.print ("MOTOR2 = ");
Serial.println (value);
rotate2(currentValue, .5);
} // end of processSpeed
void handlePreviousState ()
{
switch (state)
{
case Mot_1:
processMotor1 (currentValue);
break;
case Mot_2:
processMotor2 (currentValue);
break;
} // end of switch
currentValue = 0;
} // end of handlePreviousState
void processIncomingByte (const byte c)
{
if (isdigit (c))
{
currentValue *= 10;
currentValue += c - '0';
} // end of digit
else
{
// The end of the number signals a state change
handlePreviousState ();
// set the new state, if we recognize it
switch (c)
{
case 'a':
state = Mot_1;
break;
case 'b':
state = Mot_2;
break;
default:
state = NONE;
break;
} // end of switch on incoming byte
} // end of not digit
} // end of processIncomingByte
void loop ()
{
if (Serial.available ())
processIncomingByte (Serial.read ());
} // end of loop
void rotate1(int steps, float speed)
{
//rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
int dir = (steps > 0)? HIGH:LOW;
steps = abs(steps);
digitalWrite(DIR_PIN1,dir);
float usDelay = (1/speed) * 70;
for(int i=0; i < steps; i++){
digitalWrite(STEP_PIN1, HIGH);
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN1, LOW);
delayMicroseconds(usDelay);
}
}
void rotate2(int steps, float speed)
{
//rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
int dir = (steps > 0)? HIGH:LOW;
steps = abs(steps);
digitalWrite(DIR_PIN2,dir);
float usDelay = (1/speed) * 70;
for(int i=0; i < steps; i++){
digitalWrite(STEP_PIN2, HIGH);
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN2, LOW);
delayMicroseconds(usDelay);
}
}
Thank you!