This will be my first ever submission to the forum as I am royally stumped, I had made a functional version of the code that had first used the parseFloat and that had the unintended effect of halting the code per line of serial update. I successfully can interpret the serial data again this time using the serial input basics examples to parse my data, unfortunately with liveWeightFunc unsuppressed I stop receiving serial data and if I modify the while statement the stepper motor runs without stopping. I'm confused because I can print the variable but as soon as I try to compare liveWeight to targetWeight the code breaks. My understanding of the void loops vs external type functions are limited. Any help would be greatly appreciated.
#include <TMCStepper.h>
//#include <AccelStepper.h>
#define EN_PIN 5 // Enable
#define DIR_PIN 2 // Direction
#define STEP_PIN 3 // Step
#define SW_SCK 4 // Software Slave Clock (SCK)
#define SERIAL_PORT Serial1 // TMC2208/TMC2224 HardwareSerial port
#define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2
#define R_SENSE 0.11f //SilentStepStick series use 0.11
TMC2209Stepper driver(&SERIAL_PORT, R_SENSE, DRIVER_ADDRESS); // Hardware Serial
//AccelStepper stepperA = AccelStepper(stepperA.DRIVER, STEP_PIN, DIR_PIN);
constexpr uint32_t steps_per_round = 200*16;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float liveWeight = 0; //Live Weight
boolean newData = false;
float targetWeight = 0.5; //Target
int steps = 0; // Steps to Target
void setup() {
Serial.begin(9600); // Serial Monitor
Serial2.begin(9600, SERIAL_8N1, 8, 9); // RS232 8RX 9TX ON THE CONVERTER
delay(50);
pinMode(EN_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(EN_PIN, HIGH);
SERIAL_PORT.begin(115200); // HW UART drivers
driver.begin(); // SPI: Init CS pins and possible SW SPI pins
driver.toff(5); // Enables driver in software
driver.rms_current(200); // Set motor RMS current
driver.microsteps(16); // Set microsteps to 1/8th
//driver.en_pwm_mode(true); // Toggle stealthChop on TMC2130/2160/5130/5160
driver.en_spreadCycle(true); // Toggle spreadCycle on TMC2208/2209/2224
driver.pwm_autoscale(true); // Needed for stealthChop
// stepperA.setMaxSpeed(steps_per_round*20); // 100mm/s @ 80 steps/mm
// stepperA.setAcceleration(5000); // 2000mm/s^2
// stepperA.setEnablePin(EN_PIN);
// stepperA.setPinsInverted(false, false, true);
// stepperA.enableOutputs();
// stepperA.setCurrentPosition(0);
}
bool shaft = false;
void loop() {
recvWithStartEndMarkers(); // Serial Input format is " 0.000g"
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
//targetWeightFunc();
//if(targetWeight, 3 > liveWeight, 3){
// digitalWrite(EN_PIN, LOW);
// while (1) {
// digitalWrite(DIR_PIN, HIGH);
// digitalWrite(STEP_PIN, LOW);
// delayMicroseconds(100);
// digitalWrite(STEP_PIN, HIGH);
// delayMicroseconds(100);
// }
//}
//else{
// digitalWrite(EN_PIN, HIGH);
//}
//shaft = !shaft;
//driver.shaft(shaft);
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = ' ';
char endMarker = 'g';
char rc;
while (Serial2.available() > 0 && newData == false) {
rc = Serial2.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\n'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, "g");
liveWeight = atof(strtokIndx); // convert this part to a float
}
//============
void showParsedData() {
Serial.println(liveWeight, 3);
}
//void targetWeightFunc() {
// if(liveWeight, 3 < targetWeight, 3){
// digitalWrite(EN_PIN, LOW);
// while (1) {
// digitalWrite(DIR_PIN, HIGH);
// digitalWrite(STEP_PIN, LOW);
// delayMicroseconds(100);
// digitalWrite(STEP_PIN, HIGH);
// delayMicroseconds(100);
// }
// }
// else{
// digitalWrite(EN_PIN, HIGH);
// }
// shaft = !shaft;
// driver.shaft(shaft);
//}