Digistump Forums
The Digispark => Digispark Projects => Topic started by: Airmonterosa on March 12, 2017, 09:21:00 am
-
Goodmorning everyone! I'm trying to create a sketch for one strobe flasher to install on my ultralight.
What my Digispark should do is this: three fast flashes every 2 I'm using a digispark connected whith a mosfet transistor.
Thank so much for any kind of help.
Michele
-
Here is a sketch that flashes the onboard LED (currently set for Arduino). You can tie into the appropriate pin (1 for Digispark) and adjust the strobe parameters as needed.
Don't fall out of the sky. No warranties implied or otherwise given. Use at your own risk.
/*
Strobe
Turns on an LED and off in a burst of strobes then delays for a specified
period before repeating the strobe pattern.
To configure set the following parameters:
strobeNumber - number of flashes in strobe sequence
strobeOn - duration of flash in milliseconds
strobeOff - delay between flashes in milliseconds
strobeDelay - delay between strobe sequence in milliseconds
*/
// Adjust the following strobe parameters as needed
int strobeNumber = 3;
int strobeOn = 100;
int strobeOff = 200;
int strobeDelay = 2000;
int LEDpin = 13; // 13 Arduino, 1 Digispark
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LEDpin, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
for(int i=0; i<strobeNumber; i++) {
digitalWrite(LEDpin, HIGH); // turn the LED on
delay(strobeOn);
digitalWrite(LEDpin, LOW); // turn the LED off
delay(strobeOff);
}
delay(strobeDelay-strobeOff); // Because we already delayed for strobeOff (ms)
}
-
Thank so much for the very fast reponse! I'll try to adapt your sketch to my Digispark.
-
and if I wanted to add a second blinking light with a different frequency? Can i do this with a single mosfet, or I have to add another?
I have to set a second output (P1 or what?)
Many many thanks
-
A second control circuit would be necessary, on a second pin but then your code gets a little more complicated. You would have to multiplex the control somehow so that you are flashing each at a different rate. It would take some thought to come up with a solution. There may be some interrupt based methods of doing this but haven't seen or used them.
The dead simple way is to use another Digispark.
-
I don't know if this helps, but this was the code I wrote about four years ago for a led strobe controller for a quadcopter, which was based on an ATTiny85, so should be easy to transfer to a Digispark. It was plugged into an auxiliary channel of the quads receiver, so the pattern to be used was controlled by the transmitter.
I know it is a let more complicated than the sketch that exeng gave earlier, but it does a lot more, and shows one way to control multiple pins with multiple pattern sets. Unfortunately, I can't say for certain whether I finished this version of the code, and I see there is some code in it from the previous revision that is now redundant / not needed.
/*
PIN 0 / STARBOARD / RIGHT / GREEN
PIN 1 / STROBE / REAR / WHITE
PIN 2 / PORT / LEFT / RED
PIN 4 / RC_SIGNAL / RC RECEIVER
*/
#define STARBOARD 0
#define STROBE 1
#define PORT 2
#define RC_SIGNAL 4
#define NUM_PATTERNS 5
long currentMillis;
int rcSignal = 0;
byte currMode = 0;
byte lastMode = 25;
PROGMEM prog_uchar masterPatternStates[NUM_PATTERNS][3][10] = { {{0,255}, {0,255}, {0,255}},
{{1,0,255}, {0,1,0,255}, {0,1,0,255}},
{{0,255}, {1,0,1,0,255}, {0,255}},
{{1,0,1,255}, {1,0,1,255}, {0,1,0,1,255}},
{{0,1,255}, {0,1,255}, {1,0,255}}
};
PROGMEM prog_uint16_t masterPatternTimes[NUM_PATTERNS][3][10] = { {{0}, {0}, {0}},
{{50,1200}, {100,50,1100}, {200,50,1000}},
{{0}, {50,100,50,100}, {0}},
{{1000,50,500}, {500,50,1000}, {1200,75,150,75}},
{{1900,100}, {1900,100}, {1800,200}}
};
unsigned char pattern_states[3][10];
//
/* = {{0,1,0,1,0,1,0,1,0,255},
{0,1,0,1,0,1,0,1,0,255},
{0,1,0,1,0,1,0,1,0,255}}; //(1 byte) 0 to 255
uint16_t pattern_times[3][10]; //(2 bytes) 0 to 65,535
*/
int portPattern[5][10] = {
{0,0,-1},
{1,50,0,1200,-1},
{0,0,-1},
{1,1000,0,50,1,500,-1},
{0,1900,1,100,-1}
};
int starboardPattern[5][10] = {
{0,0,-1},
{0,100,1,50,0,1100,-1},
{1,50,0,100,1,50,0,100,-1},
{1,500,0,50,1,1000,-1},
{0,1900,1,100,-1}
};
int strobePattern[5][10] = {
{0,0,-1},
{0,200,1,50,0,1000,-1},
{0,0,-1},
{0,1200,1,75,0,150,1,75,-1},
{1,1800,0,200,-1}
};
unsigned long strobe[4] = {1,0,0,0};
unsigned long port[4] = {1,0,0,0};
unsigned long starboard[4] = {1,0,0,0};
void setup()
{
pinMode(PORT,OUTPUT);
pinMode(STARBOARD,OUTPUT);
pinMode(STROBE,OUTPUT);
pinMode(RC_SIGNAL,INPUT);
digitalWrite(PORT,LOW);
digitalWrite(STARBOARD,LOW);
digitalWrite(STROBE,LOW);
}
void loop()
{
rcSignal = pulseIn(RC_SIGNAL, HIGH, 25000);
if (rcSignal < 900) // spektrum smartsafe or no valid signal
{
currMode = 1; //chaser - no signal
}
else if (rcSignal >= 900 && rcSignal <= 1300) //low
{
currMode = 0; //off
}
else if (rcSignal > 1300 && rcSignal <= 1700) // middle
{
currMode = 3; //pattern 1
}
else if (rcSignal > 1700 && rcSignal <= 2100) //high
{
currMode = 4; //pattern 2
}
else //I don't understand?
{
currMode = 2;
}
if (currMode != lastMode)
{
//load new pattern table
for (int i = 0; i < NUM_PATTERNS; i++)
{
for (int j = 0; j < 3; j++)
{
pattern_states[i][j] = pgm_read_word(&masterPatternStates[currMode][i][j]);
}
}
//reset counters
// [0] mode, [1] firstRun, [2] step, [3] waitTime
port[0] = currMode; port[1] = 0; port[2] = 0;
starboard[0] = currMode; starboard[1] = 0; starboard[2] = 0;
strobe[0] = currMode; strobe[1] = 0; strobe[2] = 0;
digitalWrite(PORT,LOW);
digitalWrite(STARBOARD,LOW);
digitalWrite(STROBE,LOW);
lastMode = currMode;
}
currentMillis = millis();
//port light
if (port[1] == 0 || currentMillis >= port[3])
{
if (port[1] == 0)
port[1] = 1;
if (portPattern[port[0]][port[2]] == 0)
{
digitalWrite(PORT,LOW);
}
else //if (portPattern[port[0]][port[2]] == 1)
{
digitalWrite(PORT,HIGH);
}
port[3] = millis() + portPattern[port[0]][port[2]+1];
port[2] += 2;
if (portPattern[port[0]][port[2]] == -1)
{
port[2] = 0;
}
}
//starboard light
if (starboard[1] == 0 || currentMillis >= starboard[3]) //[3] is delay
{
if (starboard[1] == 0)
starboard[1] = 1;
if (starboardPattern[starboard[0]][starboard[2]] == 0) //[2] is on/off flag
{
digitalWrite(STARBOARD,LOW); // light off
}
else //(starboardPattern[starboard[0]][starboard[2]] == 1) //[2] is on/off flag
{
digitalWrite(STARBOARD,HIGH); // light on
}
starboard[3] = currentMillis + starboardPattern[starboard[0]][starboard[2]+1]; //read 'delay' parameter
starboard[2] += 2; //increment to next 'step'
if (starboardPattern[starboard[0]][starboard[2]] == -1) // if next 'step' is '-1' - end of set
{
starboard[2] = 0; // return to first instruction in set
}
}
if (strobe[1] == 0 || currentMillis >= strobe[3])
{
if (strobe[1] == 0)
strobe[1] = 1;
if (strobePattern[strobe[0]][strobe[2]] == 0)
{
digitalWrite(STROBE,LOW);
}
else if (strobePattern[strobe[0]][strobe[2]] == 1)
{
digitalWrite(STROBE,HIGH);
}
strobe[3] = currentMillis + strobePattern[strobe[0]][strobe[2]+1];
strobe[2] += 2;
if (strobePattern[strobe[0]][strobe[2]] == -1)
{
strobe[2] = 0;
}
}
}
-
Well Pete... saw your post this morning after dreaming about a possible solution (that's where many of my coding problems get solved). So you beat me to it.
In any case, decided to hack another example together for Michele. Seems to work as intended.
/*
* ULM Multi LED Strobe
*
* This example outputs 3 different LED patterns on 3 different output pins.
* The patterns are set by simply setting a sequence of delay values to repeat.
* These values determine the on/off delay times. For example, {100,200,100,200,100,2000} is
* an LED pattern that is ON 100ms, OFF 200ms, ON 100ms, OFF 200ms, ON 100ms, OFF 2 seconds.
*
*/
int LED1pin = 6;
int LED2pin = 7;
int LED3pin = 8;
uint16_t LED1pat[] = {100,200,100,200,100,2000}; // On/Off/On/Off... delays
uint16_t LED2pat[] = {1000,2000}; // On/Off/On/Off... delays
// Morse SOS pattern
uint16_t LED3SOS[] = {120,120,120,120,120,240,360,120,360,120,360,240,120,120,120,120,120,1000}; // dot,dot,dot pause dash,dash,dash pause dot,dot,dot long pause
uint16_t LED1size = sizeof(LED1pat)/2; // Need to know how many entries there are so we divide by 2 to get the correct number of entries not characters.
uint16_t LED2size = sizeof(LED2pat)/2;
uint16_t LED3size = sizeof(LED3SOS)/2;
uint16_t LED1idx; // For a brute force control of the pattern index.
uint16_t LED2idx;
uint16_t LED3idx;
bool LED1state; // Basically HIGH or LOW, State is toggled to control LED output.
bool LED2state;
bool LED3state;
uint32_t nextLED1chg; // Time value in millis for next scheduled LED state change.
uint32_t nextLED2chg;
uint32_t nextLED3chg;
void setup() {
// put your setup code here, to run once:
/* For debug
Serial.begin(9600);
Serial.println("ULM Multi Strobe starting");
Serial.println(LED1size);
Serial.println(LED2size);
Serial.println(LED3size);
*/
pinMode(LED1pin, OUTPUT);
pinMode(LED2pin, OUTPUT);
pinMode(LED3pin, OUTPUT);
LED1idx = 0;
LED2idx = 0;
LED3idx = 0;
LED1state = 1; // init HIGH
LED2state = 1; // init HIGH
LED3state = 1; // init HIGH
// All LEDs start on
digitalWrite(LED1pin, LED1state);
nextLED1chg = millis() + LED1pat[LED1idx]; // time for next LED state change
digitalWrite(LED2pin, LED2state);
nextLED2chg = millis() + LED2pat[LED2idx]; // time for next LED state change
digitalWrite(LED3pin, LED3state);
nextLED3chg = millis() + LED3SOS[LED3idx]; // time for next LED state change
}
void loop() {
// put your main code here, to run repeatedly:
// First pattern
if (millis() >= nextLED1chg) {
// Toggle state
LED1state = !LED1state;
digitalWrite(LED1pin, LED1state);
// Manage indexing through ON/OFF pattern
++LED1idx;
LED1idx %= LED1size;
nextLED1chg = millis() + LED1pat[LED1idx];
}
// Second Pattern
if (millis() >= nextLED2chg) {
// Toggle state
LED2state = !LED2state;
digitalWrite(LED2pin, LED2state);
// Manage indexing through ON/OFF pattern
++LED2idx;
LED2idx %= LED2size;
nextLED2chg = millis() + LED2pat[LED2idx];
}
// SOS (3rd) pattern
if (millis() >= nextLED3chg) {
// Toggle state
LED3state = !LED3state;
digitalWrite(LED3pin, LED3state);
// Manage indexing through ON/OFF pattern
++LED3idx;
LED3idx %= LED3size;
nextLED3chg = millis() + LED3SOS[LED3idx];
}
}
@Michele, the patterns are simply on/off delay times. I tried to include enough comments so it should be easy to follow. It also contains your original 3 blink pattern and two others to demonstrate 3 LEDs flashing different patterns. Tested on an UNO. Pins were picked arbitrarily. Hope this helps.
-
More than one way to skin a cat... so more the merrier ;) Hopefully the two of these will give Michele some ideas... and if yours does the job out of the box, even better! ;) That looks pretty much the way my version 2 of the code had looked... with the code I posted being the later v3. I'd started with a fixed strobing program, and then upgraded it to a selectable pattern, and then was upgrading it further to have room for more patterns and more complex patterns, which only need you to change to two arrays for sequencing.
-
I dont know how to thank you guys! In the next week my cave ll'be the test lab!!!!
The last question : 3 different strobe pattern,3 mosfet,3 output but 1 Digispark?
-
Yep, you should be able to control the 3 with 1 Digispark. You'll have to use different pins than the ones I selected for my example as they were selected for an Arduino UNO. You could use pins 0,1,2 as digital outputs, each connected to a different MOSFET driving the individual LEDs.
-
This is my flying big foot
-
You are braver than I. Let us know how the LED flashing works out for you.
-
yet take advantage of your great willingness for a second project to connect the digispark to a fuel pump that must be ON when the pressure detected by a pressure-meter falls below a certain threshold (0.2 atm, that is 1.3 volts) and must be off when the pressure reaches 0.4 atm, that is 1.7 volts.
If any of you were to visit Italy will be my guest !!!!