/*Program tracks and changes currentPosition.*/
int incomingByte = 0; // for incoming serial data
int x = 1; // for incoming numeric data
int currentPosition = 0; //assume current position starts at zero
int secondByte = 0; /*The up and down doesn't work. parseInt always returns 0 and x gets turned to 0.
making this variable to try something else.*/
void setup() {
Serial.begin(115200); // opens serial port, sets data rate to 115200 bps
}
void loop()
{
// take action only on receiving data:
if (Serial.available() > 0)
{
// read the first incoming byte:
incomingByte = Serial.read();
Serial.print("incomingByte = ");
Serial.println(incomingByte);
// take action based on the first byte received
switch (incomingByte)
{
case 73: // ASCII I - "Idle" command
Serial.println("executing case 73 Idle command");
Serial.print("currentPosition = ");
Serial.println(currentPosition);
break;
case 85: // ASCII U - "Up" command
/*based on Labview code the U and D commands would have a numeric added to the string before that
message is sent to the Arduino. So the U command would get the number of positions to move added and become for example
U1 or U4 or whatever before being turned into a message.*/
Serial.println("executing case 85 Up command");
if (Serial.available() > 0)
{
secondByte = Serial.read();
Serial.print("secondByte = ");
Serial.println(secondByte);
switch (secondByte);
{
case 49: // ASCII 1
Serial.println("executing Up subcase 49: move position up 1");
currentPosition = currentPosition + 1;
Serial.print("currentPosition = ");
Serial.println(currentPosition);
break;
case 50: // ASCII 2
Serial.println("executing Up subcase 50: move position up 2");
currentPosition = currentPosition + 2;
Serial.print("currentPosition = ");
Serial.println(currentPosition);
break;
default:
Serial.println("invalid up command, currentPosition unchanged");
break;
} //end subcase
}
else {
Serial.println("No serial data avalable to parse");
}
break;
case 68: // ASCII D - "Down" command
Serial.println("executing case 68 Down command");
x = Serial.parseInt(); //from original code, always makes x = 0
currentPosition = currentPosition - x;
Serial.print("currentPosition = ");
Serial.println(currentPosition);
break;
case 83: // ASCII S - "Set Current Position to Zero" command
Serial.println("executing case 83 Set command");
currentPosition = 0;
Serial.print("currentPosition = ");
Serial.println(currentPosition);
break;
/*default: // for any other input
Serial.println("executing case Default - Invalid command");
Serial.println("printing a blank space on next line.");
Serial.println("");
break;
*/
}//end case
Serial.println("Outer case ended");
Serial.println("----------");
}//end if
}//end void loop