RayLivingston's code doesn't compile. I'm working on a similar project (a XY gantry with a thermocouple that moves according to a computer request and sends the readout temperature when requested through serial).
Here's how I implemented, adapting RayLivingston's code. I'm posting the entire code so you can adapt it to whatever you're trying to do: (see ParseLine() function for the fix)
Again, this code is tested and works. Only the first character before the equals sign is parsed.
#include "max6675.h"
#include "AccelStepper.h"
#include "Wire.h"
int thermoDO = 12;
int thermoCS = 10;
int thermoCLK = 13;
int XStep = 2;
int XDir = 3;
int XEnable = 4;
int YStep = 6;
int YDir = 7;
int YEnable = 8;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
AccelStepper stepperX(1, XStep, XDir); // 1 = Driver
AccelStepper stepperY(1, YStep, YDir); // 1 = Driver
char buffer [32];//buffer for serial read
int cnt = 0;
boolean ready = false;
void setup() {
Serial.begin(9600);
//Stepper setup
stepperX.setMaxSpeed(75);
stepperX.setAcceleration(250);
stepperX.setEnablePin(XEnable);
stepperX.setPinsInverted(false, false, true); //invert logic of enable pin
stepperX.enableOutputs();
//Stepper setup
stepperY.setMaxSpeed(75);
stepperY.setAcceleration(250);
stepperY.setEnablePin(YEnable);
stepperY.setPinsInverted(false, false, true); //invert logic of enable pin
stepperY.enableOutputs();
}
void loop() {
//Waits for serial command
if (ready){
ParseLine();
ready = false;
}else{
while (Serial.available()){
char c = Serial.read();
buffer[cnt++] = c;
if ((c == '\n') || (cnt == sizeof(buffer)-1))
{
buffer[cnt] = '\0';
cnt = 0;
ready = true;
}
}
}
}
void ParseLine(){
//Parses the input line and does the action or returns
char * strtokIndx;
char key[8];
int nSteps;
strtokIndx = strtok(buffer, "="); // Everything up to the '=' is the instruction
strcpy(key, strtokIndx);
strtokIndx = strtok(NULL, "\n"); // Everything else is the color value
nSteps = atoi(strtokIndx);
//Valid commands:
//X=200 does 200 relative steps in X axis
//Y=100 does 100 relative steps in Y axis
//T=? Requests temperature in serial ("?" can be anything, it is not parsed)
if (key != NULL){
switch (key[0])
{
case 'X':
stepperX.move(nSteps);
break;
case 'Y':
stepperY.move(nSteps);
break;
case 'T':
Serial.println(thermocouple.readCelsius());
break;
}
}
}