Hi everyone,
I'm having a problem with a Teensy LC that's left me scratching my head. I am using the Teensy in a simple system to control a laser diode based on serial commands sent from a PC over USB. The Teensy receives one letter serial commands which tell it to: turn the laser on, turn the laser off, pulse the laser for a certain amount of time, or set the pulse time to XXXXX miliseconds ('n', 'f', 'p', and 'tXXXXX' respectively). To control the laser, a digital output of the Teensy is connected to the gate of a P-channel MOSFET which either connects or disconnects the laser diode input to a 5V supply based on the high/low of the digital output (I realize that the Teensy is 3.3V, but I think that is sufficient to trigger the MOSFET at 5V). Following a pulse command, the Teensy sends two signals back to the PC over serial: 'f' as soon as the laser is turned off and 'c' five seconds later.
When the laser diode is not connected (the transistor is still in place, but just doesn't have anything to control) everything works fine. For some reason, as soon as the laser diode is connected to the transistor, the Teensy seems to lose its ability to send serial data (i.e. the 'f' and 'c' following a laser pulse). Interestingly, the Teensy can still act in response to serial commands it receives to correctly turn the laser on/off/pulse. If I disconnect the laser diode, the problem still occurs until the Teensy is reset (either using the reset button or unplugging it from power). I have tried several different output pins and the problem seems to occur independent of the pin used. I have tried receiving serial commands both with the Arduino IDE serial monitor and a MATLAB script that communicates over serial (which I have used used successfully before so I believe it works fine).
Is there some condition that causes the Teensy to disable its ability to send serial commands over USB?
Thanks in advance.
Source code:
I'm having a problem with a Teensy LC that's left me scratching my head. I am using the Teensy in a simple system to control a laser diode based on serial commands sent from a PC over USB. The Teensy receives one letter serial commands which tell it to: turn the laser on, turn the laser off, pulse the laser for a certain amount of time, or set the pulse time to XXXXX miliseconds ('n', 'f', 'p', and 'tXXXXX' respectively). To control the laser, a digital output of the Teensy is connected to the gate of a P-channel MOSFET which either connects or disconnects the laser diode input to a 5V supply based on the high/low of the digital output (I realize that the Teensy is 3.3V, but I think that is sufficient to trigger the MOSFET at 5V). Following a pulse command, the Teensy sends two signals back to the PC over serial: 'f' as soon as the laser is turned off and 'c' five seconds later.
When the laser diode is not connected (the transistor is still in place, but just doesn't have anything to control) everything works fine. For some reason, as soon as the laser diode is connected to the transistor, the Teensy seems to lose its ability to send serial data (i.e. the 'f' and 'c' following a laser pulse). Interestingly, the Teensy can still act in response to serial commands it receives to correctly turn the laser on/off/pulse. If I disconnect the laser diode, the problem still occurs until the Teensy is reset (either using the reset button or unplugging it from power). I have tried several different output pins and the problem seems to occur independent of the pin used. I have tried receiving serial commands both with the Arduino IDE serial monitor and a MATLAB script that communicates over serial (which I have used used successfully before so I believe it works fine).
Is there some condition that causes the Teensy to disable its ability to send serial commands over USB?
Thanks in advance.
Source code:
Code:
#include <TimerOne.h> //TimerOne downloaded from Paul Stoffregen: [url]https://www.pjrc.com/teensy/td_libs_TimerOne.html[/url]
//Change described below is required for proper functionality
#define LASER_PIN 8
#define PRE_LASER_TIME 2000 //time to delay before activating laser in ms
#define POST_LASER_TIME 5000 //time to record after laser pulse in ms
// Sum of PRE_LASER_TIME + pulse_length + POST_LASER_TIME must be less than 4,294,967,295 ms
void setup() {
// put your setup code here, to run once:
pinMode(LASER_PIN, OUTPUT);
digitalWrite(LASER_PIN,HIGH);
pinMode(LED_BUILTIN, OUTPUT);
//Serial.println("Serial Ready");
Timer1.initialize(1000);
Timer1.stop();
Timer1.attachInterrupt(blinkLED);
Serial.begin(9600);
}
char input;
char delay_time[6];
unsigned long pulse_length = 1000;
volatile unsigned long n = 0;
unsigned int cycles = 0;
//unsigned int remainder1 = 1000;
void blinkLED(){
n++;
if (n == PRE_LASER_TIME) {
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(LASER_PIN,LOW);
}
else if (n == PRE_LASER_TIME + pulse_length) {
digitalWrite(LASER_PIN, HIGH);
digitalWrite(LED_BUILTIN, LOW);
Serial.println("f");
}
else if (n >= PRE_LASER_TIME + pulse_length + POST_LASER_TIME) {
Serial.println("c");
Timer1.stop();
n = 0;
}
}
void loop() {
// put your main code here, to run repeatedly:
/*
n: laser on
f: laser off
p: laser pulse
tXXXXX: set pulse time, where XXXXX is time in ms
NOTE: time must be a five digit number with leading zeros if necessary
EX: t05200 sets pulse time to 5200 ms or 5.2 s
*/
if (Serial.available() > 0) {
input = Serial.read();
if (input == 'n') {
delay(PRE_LASER_TIME);
digitalWrite(LASER_PIN,LOW);
digitalWrite(LED_BUILTIN, HIGH);
}
else if(input == 'f') {
digitalWrite(LASER_PIN,HIGH);
digitalWrite(LED_BUILTIN, LOW);
}
else if(input == 'p') {
Timer1.restart();
}
else if(input == 't'){
int num_read = Serial.readBytes(delay_time,5);
pulse_length = strtol(delay_time,NULL,10);
}
else if(input = 'z'){
Serial.println("a");
}
else{
Serial.println("invalid input");
}
}
}
Last edited by a moderator: