Teensy 4.1 not storing code after power cycle

Patrick69

Member
Hello, as from the title, after i uploaded the code to Teensy, then power cycled it, the Teensy does not run the code and behave as if it does not have a code at all. I've seen people from Adafruit forum saying to add a delay and LED blinking at the start of the code and it'll work. But after i added it, the Teensy only runs up to the LED blinking. For reference, my code is below:


C++:
#include "sbus.h"  // SBUS library for SBUS receiver


// SBUS object for writing SBUS
bfs::SbusTx sbus_tx(&Serial6); // Serial 3 for Arduino Mega, 6 for Teensy
bfs::SbusRx sbus_rx(&Serial6);
bfs::SbusData data;  // SBUS data structure

// PWM and direction pins for motor control
const int motor1PWMPin = 7;       // Enable pin (EN1) for Motor 1
const int motor1DirPin1 = 5;      // Direction pin 1 (IN1) for Motor 1
const int motor1DirPin2 = 6;      // Direction pin 2 (IN2) for Motor 1
const int motor2PWMPin = 2;       // Enable pin (EN2) for Motor 2
const int motor2DirPin1 = 3;      // Direction pin 1 (IN3) for Motor 2
const int motor2DirPin2 = 4;      // Direction pin 2 (IN4) for Motor 2

// Timer for blinking LED
unsigned long int timex;



void setup() {
  delay(3000);

   pinMode(13, OUTPUT);
  digitalWrite(13, HIGH); delay(200);  // Blink once for confirmation
  digitalWrite(13, LOW); delay(200);
  digitalWrite(13, HIGH); delay(200); 
  digitalWrite(13, LOW);



  // Motor control pins setup
  pinMode(motor1PWMPin, OUTPUT);
  pinMode(motor1DirPin1, OUTPUT);
  pinMode(motor1DirPin2, OUTPUT);
  pinMode(motor2PWMPin, OUTPUT);
  pinMode(motor2DirPin1, OUTPUT);
  pinMode(motor2DirPin2, OUTPUT);

  Serial.begin(115200);
  while (!Serial) {}

  // Initialize SBUS communication
  sbus_rx.Begin();
  sbus_tx.Begin();
  Serial.println("SBUS Initialized");
}

void loop() {


  if (sbus_rx.Read()) {  // Read SBUS input
    data = sbus_rx.data();  // Store received SBUS data
    digitalWrite(13, HIGH);  // Turn on LED to indicate successful reading

    // Map SBUS channels to motor power control values
    int forwardBackwardPower = map(data.ch[1], 174, 1811, -255, 255);  // Channel 2 for forward/backward
    int leftRightPower = map(data.ch[0], 174, 1811, -255, 255);        // Channel 1 for turning

    // Calculate motor speeds for turning
    int motor1Speed = forwardBackwardPower + leftRightPower;  // Left motor
    int motor2Speed = forwardBackwardPower - leftRightPower;  // Right motor

    // Ensure motor speed values are within PWM range
    motor1Speed = constrain(motor1Speed, -255, 255);
    motor2Speed = constrain(motor2Speed, -255, 255);

    Serial.print("Motor 1 Speed: ");
    Serial.println(motor1Speed);
    Serial.print("Motor 2 Speed: ");
    Serial.println(motor2Speed);

    // Set motor speeds and directions via PWM
    setMotorPWM(motor1PWMPin, motor1DirPin1, motor1DirPin2, motor1Speed);  // Motor 1
    setMotorPWM(motor2PWMPin, motor2DirPin1, motor2DirPin2, motor2Speed);  // Motor 2

    /* Set the SBUS TX data to the received data */
    sbus_tx.data(data);
    /* Write the data to the motors */
    sbus_tx.Write();
  } else {
    blinkStatusLED();  // Blink LED if no valid SBUS data
  }
}

// Function to control motor speed and direction via PWM with L298
void setMotorPWM(int pwmPin, int dirPin1, int dirPin2, int speed) {

 
 
 
  if (speed >= 0) {
    digitalWrite(dirPin1, HIGH);    // Set direction for forward
    digitalWrite(dirPin2, LOW);
    analogWrite(pwmPin, speed);     // Set PWM speed
  } else {
    digitalWrite(dirPin1, LOW);     // Set direction for reverse
    digitalWrite(dirPin2, HIGH);
    analogWrite(pwmPin, -speed);    // Set PWM speed (absolute value)
  }
}

// Function to blink onboard LED if no valid SBUS data
void blinkStatusLED() {

 

  if (millis() - timex > 500) {  // Blink every 500 ms
    digitalWrite(13, !digitalRead(13));
    timex = millis();
  }
}


From the code, if the Teensy does store the code, the LED will blink at 0.5 seconds after the initial delay and LED flashing. But it doesn't after the power cycle.
 
while (!Serial) {}
If not connected to a Serial terminal, this code will hang forever...

I usually do something like:
Code:
while (!Serial && (millis() < 5000)) {}
Which will wait for up to 5 seconds for the serial terminal to start, else it will continue...
You can choose different timeouts here...
 
If not connected to a Serial terminal, this code will hang forever...

I usually do something like:
Code:
while (!Serial && (millis() < 5000)) {}
Which will wait for up to 5 seconds for the serial terminal to start, else it will continue...
You can choose different timeouts here...
I see! The Teensy works now after power cycle. Thank you so much!
 
Back
Top