Previous Posts:
TI and Würth Elektronik LED RoadTest+ application and un-boxing - Blog 1 - Nov 10
LED Road Test - Software issues - Blog 2 - Nov 26
LED Road Test - LED PCB issues - Blog 3 - Dec 5
LED Road Test - Some words on timing - Blog 4 - Dec 15
LED Road Test - Launchpad user interface - Blog 5 - Dec 19
LED Road Test - Solder the LEDs - Blog 6 - Dec 28
LED Road Test - Deadline approaching - Blog 7 - Jan 15
LED Road Test - Making a ring light - Blog 8 - Jan 16
Related links:
search for a PCB for Würth power leds
flashcontroller screen-recording
Timing software
In my forth blog I explained the timing diagram and in the fifth blog I described the MSP430 user interface, but the timing wasn't implemented by then.
So today I will explain how I programmed the timing. Luckily due to the existence of the delayMicroseconds() function in Energia, this is not a big issue.
Remember all the timing values I have:
List of parameters:
- flash duration
- flash delay
- trigger duration
- trigger delay
- interval
- internal/external trigger
In the software we first declare the led output pin and trigger input pin:
int ledPin = 12; int triggerInPin = 11;
Set the appropriate INPUT and OUTPUT in the setup function:
void setup() {
// start serial port at 9600 bps:
Serial.begin(9600);
establishContact(); // send the initialization data OUT
pinMode(ledPin, OUTPUT);
pinMode(triggerInPin, INPUT);
}
We put the flash including delay and duration in a function:
void led_flash() {
delayMicroseconds(flash_delay);
digitalWrite(ledPin, HIGH);
delayMicroseconds(flash_duration);
digitalWrite(ledPin, LOW);
}
This function will be called from the main loop:
while (Serial.available() == 0){ //Loop until some serial data is recieved.
if (!external_trigger) {
delayMicroseconds(interval);
led_flash();
}
}
In the case of an external trigger I use an interrupt, which fires the led_flash function on a rising edge of the optocoupler which is connected to the triggerInPin:
void set_external_trigger(String Par) {
if (Par.equals("on")) {
external_trigger = true;
attachInterrupt(triggerInPin, led_flash, RISING); // Interrupt is fired whenever input give rising edge
} else if (Par.equals("off")) {
external_trigger = false;
detachInterrupt(triggerInPin);
}
}
The complete program now is:
// Flash Controler using MSP430FR4133 LaunchPad
// (c) Gerrit Polder, 2015, 2016
String Command;
String Parameter;
long flash_duration = 0;
long flash_delay = 0;
long trigger_duration = 0;
long trigger_delay = 0;
long interval = 0;
bool external_trigger = false;
int ledPin = 12;
int triggerInPin = 11;
void establishContact() {
Serial.println();
Serial.println(" _____ _ _ ");
Serial.println(" | ___| | __ _ ___| |__ ");
Serial.println(" | |_ | |/ _` / __| '_ \\ ");
Serial.println(" | _| | | (_| \\__ \\ | | | ");
Serial.println(" |_|___|_|\\__,_|___/_| |_| _ _ ");
Serial.println(" / ___|___ _ __ | |_ _ __ ___ | | | ___ _ __ ");
Serial.println(" | | / _ \\| '_ \\| __| '__/ _ \\| | |/ _ \\ '__|");
Serial.println(" | |__| (_) | | | | |_| | | (_) | | | __/ | ");
Serial.println(" \\____\\___/|_| |_|\\__|_| \\___/|_|_|\\___|_| ");
Serial.println("\nSerial command shell v1.0");
Serial.println("Copyright (c) 2015 Gerrit Polder");
Serial.println("__________________________________________\n\n");
}
void help(){
Serial.println();
Serial.println("Serial command shell v1.0");
Serial.println("Copyright (c) 2015 Gerrit Polder");
Serial.println("help - This help info");
Serial.println("reboot - Resets variables to their initial values");
Serial.println("status - Displays status information");
Serial.println("flashduration val - set flash duration val [us]");
Serial.println("flashdelay val - set flash delay val [us]");
Serial.println("triggerduration val - set trigger duration val [us]");
Serial.println("triggerdelay val - set trigger delay val [us]");
Serial.println("flashdelay val - set flash delay val [us]");
Serial.println("externaltrigger [on/off] - set external trigger");
Serial.println("interval val - set interval val [us]");
}
void reboot(){
setup();
}
void status(){
Serial.println();
Serial.println("Flash: duration: " + String(flash_duration) + " [us] delay: " + String(flash_delay) + " [us]");
Serial.println("Trigger: duration: " + String(trigger_duration) + " [us] delay: " + String(trigger_delay) + " [us]");
if (external_trigger) {
Serial.println("External trigger");
} else {
Serial.println("Interval: " + String(interval) + " [us]");
}
}
void set_external_trigger(String Par) {
if (Par.equals("on")) {
external_trigger = true;
attachInterrupt(triggerInPin, led_flash, RISING); // Interrupt is fired whenever input give rising edge
} else if (Par.equals("off")) {
external_trigger = false;
detachInterrupt(triggerInPin);
}
}
void led_flash() {
delayMicroseconds(flash_delay);
digitalWrite(ledPin, HIGH);
delayMicroseconds(flash_duration);
digitalWrite(ledPin, LOW);
}
void invalid_input(){
Serial.println("Error in input command. Type ""help"" for available commands.");
}
void setup() {
// start serial port at 9600 bps:
Serial.begin(9600);
establishContact(); // send the initialization data OUT
pinMode(ledPin, OUTPUT);
pinMode(triggerInPin, INPUT);
}
void loop() {
Command = "";
Parameter = "";
Serial.print("\n> ");
while (Serial.available() == 0){ //Loop until some serial data is recieved.
if (!external_trigger) {
delayMicroseconds(interval);
led_flash();
}
}
char command_temp = 0;
bool read_parameter = false;
while (command_temp != '\r' && command_temp != '\n'){
while (Serial.available() < 1){}
command_temp = Serial.read();
Serial.print(command_temp); // echo
if (command_temp <= 32){
//This is the end of the base command, and the start of arguments
read_parameter = true;
} else if (read_parameter){
Parameter = Parameter + command_temp;
} else {
Command = Command + command_temp;
}
}
if (Command.equals("status")){
status();
} else if (Command.equals("help")){
help();
} else if (Command.equals("reboot")){
reboot();
} else if (Command.equals("flashdelay")){
flash_delay = Parameter.toInt();
} else if (Command.equals("flashduration")){
flash_duration = Parameter.toInt();
} else if (Command.equals("triggerdelay")){
trigger_delay = Parameter.toInt();
} else if (Command.equals("triggerduration")){
trigger_duration = Parameter.toInt();
} else if (Command.equals("externaltrigger")){
set_external_trigger(Parameter);
} else if (Command.equals("interval")){
interval = Parameter.toInt();
} else if (Command.equals("")) {
//no command, do nothing
}
else {
invalid_input();
}
}
Thats it for now, be aware that the camera trigger out yet isn't implemented, but that will be very easy to add given the example code above. Later today I will publish the wrap-up of the whole project for the tenth blog post.
stay tuned.