
Originally Posted by
artistinfla
This works fine as it is, WITH the USB connected (even with no call to Serial, just Serial1) and receiving data from RX pin. Once I remove the USB it does nothing.
Obviously something is wrong. I don't see anything really wrong in your code, but that doesn't necessarily mean there isn't some subtle issue.
The very first thing to try is adding a non-delay LED blink to your program. Here's a complete copy of your program, with a LED blink added at the end of loop().
Code:
/*
Reads a serial input string until it sees a newline.
Parses the string to get the correct data, then converts the string
to number and moves stepper the number of steps
created 02 Jan 2018
by Mark Noll
*/
// settings for max speed
// will vary according to dist traveled
#define STEP_PIN 23
#define DIRECTION_PIN 22
#define DONE_PIN 21
const byte numChars = 250;
const int nMotor = 12; //which motor (data segment to read)
int nSteps;
int nDelay;
// variables to hold the parsed data
boolean newData = false;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial1.available() > 0 && newData == false) {
rc = Serial1.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // 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, ","); // get the first part - the string
nSteps = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
nDelay = atoi(strtokIndx); // convert this part to an integer
for (int i = 2; i <= 12 ; i++) {
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
if (i == nMotor) {
nSteps = atoi(strtokIndx); // convert this part to an integer
}
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
if (i == nMotor) {
nDelay = atoi(strtokIndx); // convert this part to an integer
}
}
}
//============
void setup() {
pinMode(DIRECTION_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(DONE_PIN, OUTPUT);
// Open serial communications and wait for port to open:
Serial1.begin(500000);
while (!Serial1) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() {
int y;
// Read serial input:
while (Serial1.available() > 0) {
digitalWrite(DONE_PIN, LOW);
// get the data
recvWithStartEndMarkers();
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;
// moving so set pin HIGH
digitalWrite(DONE_PIN, HIGH);
// send signal to servo
// dertermine direction
if (nSteps <= 0) {
digitalWrite(DIRECTION_PIN, HIGH);
}
else if (nSteps > 0) {
digitalWrite(DIRECTION_PIN, LOW);
}
// get number of steps
y = abs(nSteps);
//set coordiated delay so all moves take same time, 1 frame
for (int i = 1; i <= y; i++)
{
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(nDelay);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(nDelay);
}
// done moving so set pin LOW
digitalWrite(DONE_PIN, LOW);
//delay(60);
}
}
// blink the LED, to tell if the program is running
static elapsedMillis msec;
static int ledstate=LOW;
if (msec > 500) {
msec = 0;
pinMode(13, HIGH);
digitalWrite(13, ledstate);
ledstate = (ledstate == LOW) ? HIGH : LOW;
}
}
As I quick sanity check, I ran this complete code here on a Teensy 3.2. The LED blinks.
Then I unplugged the USB and used 3.3V power to run the Teensy 3.2. It is blinking, right here on my desk.

Start with this code, to at least see if Teensy 3.2 is even running your program at all when not powered by the USB cable.
If it is, perhaps move the LED digitalWrite into the if check for Serial1 data available, so the LED lights when *any* by is received on the RX1 pin.
I'm pretty sure if you do these simple troubleshooting steps, you'll probably find out something isn't getting power (or perhaps the ground wire) or running the way it should be when the cable isn't connected.