TeensyLC won't boot unless I reprogram it every time

Status
Not open for further replies.

mdshann

New member
I have a very simple application that is failing to boot when power is applied to the USB port on the TeensyLC. If I have the loader open and press the program button it will reboot and load my application, however if I close the loader or use another PC as a power source then it will not boot.

This is a simple application that wakes up a screen when you walk by a motion sensor.

Code:
const int MOTION_PIN = 0; // Pin connected to motion detector
const int LED_PIN = 13; // LED pin - active-high
unsigned long seconds = 1000L; // !!! SEE THE CAPITAL "L" USED!!!
unsigned long minutes = seconds * 60;

void setup() 
{
  // Serial.begin(9600);
  // The PIR sensor's output signal is an open-collector, 
  // so a pull-up resistor is required:
  pinMode(MOTION_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
}

void loop() 
{
  delay(1000);
 // int proximity = digitalRead(MOTION_PIN);
 // if (proximity == LOW) // If the sensor's output goes low, motion is detected
  if (digitalRead(MOTION_PIN) == LOW)
  {
    Keyboard.press(KEY_SCROLL_LOCK);  //press the scroll lock button
    delay(25);    //short delay
    Keyboard.release(KEY_SCROLL_LOCK); //release the scroll lock button (scroll lock is now ON)
    delay(25);    //short delay
    Keyboard.press(KEY_SCROLL_LOCK); //press the scroll lock button again
    delay(25);    //short delay
    Keyboard.release(KEY_SCROLL_LOCK); //release the scroll lock button (scroll lock is now OFF again)
    digitalWrite(LED_PIN, LOW);
    delay(5 * minutes);  //5 minute delay - don't need to do it again any time soon, monitor should already be on
  }
  else
  {
    digitalWrite(LED_PIN, HIGH);
  }
}
 
Recommend you add a distinctive LED blink at the beginning of setup(), so you can see if the Teensy is booting regardless of what your PC does with the USB.
 
That's an interesting idea. I will try that in the morning.

After tinkering a bit more after my initial post I feel that it's possible it is loading my code so fast that the PIR motion sensor may still be pulled LOW from it's own startup and therefore trips my if-else statement so quickly that it goes straight to the 5 minute delay.
 
This is very common with the Teensies (expecially with the very fast 3.6). They are so fast, sometimes peripherals are not properly initialized in setup() without some delay; I know I had this problem with some temperature sensors, solved with a delay at the end of setup().
 
Oh, yes, that is definitely a problem. You do need a delay between pinMode INPUT_PULLUP and digitalRead. Even with just a capacitance of the pin, Teensy is fast enough that your first read after pinMode will see the logic low before the voltage is slowly pulled up by the weak resistor. When you add the capacitance of a PIR sensor & wires, it'll be even slower.

But once pulled up, then left floating while reprogramming, the pins tend to remain at a prior state. That's not 100% reliable, but it is a pretty plausible explanation for why it's usually working when you warm reboot (where the code previously ran and pulled the pin up to logic high), but not upon a cold power cycle boot.

Long ago, we used to very regularly see the type of problem XFer described. Those issues were mostly "solved" by adding a 400 ms delay in Teensy's startup code. It was recently shortened to 300 ms. MEMS-based motion sensor chips were the most common. Nearly all of those take about 100 to 200 ms before the chip is ready to respond. Virtually all Arduino libraries for those chips do no checking whether the device is really ready - they just write config once without even checking whether the chip responds properly, because nearly all Arduino boards have very slow startup (usually a bootloader runs first). So we were forced to do a 300 ms slow start for Teensy.

That's a separate matter than the slowness of INPUT_PULLUP. Doesn't matter how long after startup, the moment you put the pin into that mode begins the process of the voltage rising. The faster the board, the more chance you have of reading the pin before the weak pullup has managed to get the voltage up to logic high.
 
Thanks guys, I got it working with your suggestions. I added a digitalWrite(LED_PIN, HIGH); and a 30 second delay to the setup() and it works now.
 
Status
Not open for further replies.
Back
Top