How do I fix this up so that if command p/P does not get 5 digits, or it gets a bad digit, it dumps me out of the
first , overall, if()?
Right now, if I get a P command, I can get a report of a bad digit, or a bad position when a non 0-9 is entered, or when not all 5 digits are received, but then I can't seem to move along to another command without hitting 0 and Enter a couple of times, or I'm kind of left in limbo with recovery a question.
Thanks.
unsigned long maxPosition = 13350; // max extension, ~42 inch
unsigned long minPosition = 0; // min retraction
// motor "position" to move to
unsigned long goToPosition;
unsigned long motorPosition[]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
// track how many steps up or down have been moved
unsigned long motorNow[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
byte goodDigit;
// Now - Position => 0, move motor down
// Now - Position <= 0, motor up
// Now - Position = 0, you are there.
byte x;
byte motorNumber = 0;
byte command = 0;
byte moveCommand;
byte goodCommand = 0;
byte goodMotor = 0;
byte upDown = 0;
byte motorToMove; // the motor commanded to move
byte motorCommand[16]; // the motor command received 1 (retract), 2 (unwind), or 3 (stop)
byte nextStep;
byte charStep[6];
byte stepNumber[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // keep track of which step each motor is on
void setup() {
// Turn on Serial
Serial.begin(115200);
Serial.println ("Ready to Go.");
}
void loop() {
// comment out Serial when using 1284P, comment out Serial1 when using PC
// test with Serial to start, comment out and uncomment Serial1 when transition
// copy this section for Serial1
if (Serial.available() > 1 ) { // read 2 bytes
/* if u/U or d/D or s/S, # 0 to 15 and upDown becomes 1,2,3
if p/P, #0 to 15, then chars, turn into a # 0 to 99,999 (13,350 current limit with platform height)
*/
command = Serial.read(); // u/U = move up, d/D = move down, s/S = stop moving,
// p/P = go to position, t/T = go to top, w/W = where now, z/Z = set motorPosition value to 0
motorToMove = Serial.read(); // read motor numberm 0 to 9, a to f/A to F
if ((command >= 'a' || command <= 'z') || (command >= 'A' || command <= 'Z') ) {
Serial.print ("Command ");
Serial.print ((char)command);
goodCommand = 1;
}
else {
goodCommand = 0;
Serial.print (" bad command ");
Serial.println ((char)command);
}
if (motorToMove >= '0' && motorToMove <= 'f') {
motorToMove = motorToMove - 48; // ASCII to decimal for 0 to 9
if (motorToMove > 9) {
motorToMove = motorToMove - 7; // further ASCII to decimal for A to F
}
if (motorToMove > 15) {
motorToMove = motorToMove - 32; // further ASCI to decimal for a to f
}
Serial.print (" Motor # ");
Serial.print (motorToMove); // should be 0 to 15 now
}
if (motorToMove >= 0 && motorToMove <= 15) {
goodMotor = 1;
}
else {
Serial.print (" bad motor #, ");
Serial.println (motorToMove);
goodMotor = 0;
}
if (goodCommand == 1 && goodMotor == 1) {
if (command == 'p' || command == 'P') { // move to Position commanded
Serial.print (" Move to ");
moveCommand = 0xB; // need 5 characters still for 99,999
// get 5 characters
while (Serial.available() < 4) {
// wait for more characters
}
// got 5, continue
goodDigit = 1;
for (x = 0; x < 5; x = x + 1) {
charStep[x] = Serial.read();
charStep[x] = charStep[x] - 48; // read order is EDCBA
if (charStep[x] < 0 || charStep[x] > 9 ) {
Serial.print (" bad digit ");
goodDigit = 0;
Serial.print (charStep[x], DEC);
}
if (goodDigit == 1) {
goToPosition = (charStep[0] * 10000UL) + (charStep[1] * 1000UL) + (charStep[2] * 100UL) + (charStep[3] * 10UL) + charStep[4];
if (goToPosition >= maxPosition) {
goToPosition = maxPosition; // set max extension
}
if (goToPosition <= minPosition) {
goToPosition = minPosition;
}
Serial.print (" position: ");
Serial.println (goToPosition);
motorPosition[motorToMove] = goToPosition; // set the commanded motor's position
// motor x
if (motorNow[motorToMove] > motorPosition[motorToMove]) { // move up
motorCommand[motorToMove] = 1; // retract
}
else if (motorNow[motorToMove] < motorPosition[motorToMove]) { // move down
motorCommand[motorToMove] = 2; // extend
}
else if (motorNow[motorToMove] == motorPosition[motorToMove]) {
// same motorPosition, do nothing
motorCommand[motorToMove] = 3; // stop
}
}
else {
Serial.println (" bad position ");
}
}
}
}
}
}