robertcoxon:
I have posted this request on another sub forum and was pointed in this direction so here goes, this is a simple cut & paste from my other post ![:slight_smile: :slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=12)
Hi all, going to be cheeky.
I bought 2 arduinos to mount in a model helicopter to flash and fade some nav lights.
Here is the cheeky bit.
I have watched videos and read examples and forum posts but the programming is beyond me as far as state machines go and i have found no post with an indication of how i can fade and blink led's in a loop that would only be powered up for 15 mins max.
Is it too cheeky to ask if someone is willing to write a bit of code for me so if nothing else, i can pick apart what is happening in sections eg: a loop with two clearly separate bits of code, with one double flashing led with a 1/2 second delay between flashes then pause say 1.5 to 2 seconds and repeat and a 2nd bit of code to fade in and out two led's at say 2 seconds to fade in then 2 seconds to fade out. these 2 sets of led's behaviour is not required to be in unison, they need to overlap their 'state' independently. this whole 'loop' would need to run up to 15 minutes because that's more than enough time that my flight battery will provide.
I just can't get my head round the language and when i bought the arduinos, i thought the language would be easier for me to understand.
Thanks for your time, regards,
Robs brain cell on behalf of Rob
Cheers
You'll have to define the hardware completely.
How are you driving the LEDs?
Example:
2 LEDs,
one leg of LED(anode) directly connected to a one UNO digital pin, the the other leg(cathode) thru a 1k ohm resistor to ground.
Pins used (D3,D5) (PWM capable pins);
#define LED1 3
#define LED2 5
void setup(){
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
}
static uint32_t led1Tick=0, led2Tick=0; // next millis() decision point, only initialized once
static uint8_t led1Brightness=0, led2Brightness=0; // current brightness (PWM value)
static uint8_t led1Pattern=0, led2Pattern=0; // user defined meaning for next change
static uint32_t led1Duration=500, led2Duration=300; // duration of current segment in milliseconds
void loop(){
if((millis()-led1Tick)>led1Duration){ // current stage is over move to next stage
switch(led1Pattern) {
case 0: // turning off
if(led1Brightness>0){ // led is still on
led1Brightness = led1Brightness / 2; // go down, will eventually become zero
}
else { // led is off turn it back on
led1Brightness = 1; // start
led1Pattern = 1; // change to ramp up pattern;
}
analogWrite(LED1,led1Brightness); // set new brightness level
led1Tick=millis(); // mark new start time
break; // exit out of case
case 1: // turning on
if(led1Brightness<128) {// brighten LED
led1Brightness = led1Brightness * 2; // twice as bright
}
else if(led1Brightness==128) { // brightness can range from 0 to 255, 128*2 = 256 outof range
led1Brightness=255; // maximum brightness
}
else { // fully on, switch back to dimming mode
led1Brightness = 128;
led1Pattern = 0; // change to ramp down pattern;
}
analogWrite(LED1,led1Brightness); // set new brightness level
led1Tick = millis(); // mark new segment start time;
break;
default : led1Pattern = 0; //unknown mode go back to on/off
} // end of switch
}// end of
if((millis()-led2Tick)>led2Duration){ // current stage is over move to next stage
switch(led2Pattern) {
case 0: // turning off
if(led2Brightness>0){ // led is still on
led2Brightness = led2Brightness / 2; // go down, will eventually become zero
}
else { // led is off turn it back on
led2Brightness = 1; // start
led2Pattern = 1; // change to ramp up pattern;
}
analogWrite(LED2,led2Brightness); // set new brightness level
led2Tick=millis(); // mark new start time
break; // exit out of case
case 1: // turning on
if(led2Brightness<128) {// brighten LED
led2Brightness = led2Brightness * 2; // twice as bright
}
else if(led2Brightness==128) { // brightness can range from 0 to 255, 128*2 = 256 outof range
led2Brightness=255; // maximum brightness
}
else { // fully on, switch to blinking
led2Brightness = 16;
led2Pattern = 3; // change to blink pattern;
}
analogWrite(LED2,led2Brightness); // set new brightness level
led2Tick = millis(); // mark new segment start time;
break;
case 3: // bink, brightness is now use to time the number of blinks
// Turn on or off LED2. If brightness is even, LED is On
// if brightness is odd LED is Off
digitalWrite(LED2,(led2Brightness&1)==0);
if(led2Brightness > 0) led2Brightness = led2Brightness -1;
else { // done blinking, back to dimming
led2Brightness = 255; // maximum brightness
led2Pattern = 0; // start dimming
}
led2Tick = millis();
break;
default : led2Pattern = 0; //unknown mode go back to on/off
} // end of switch
}// end of
}
This is simple example of no-wait delays. Basically the code sees if a duration has elapsed then makes decisions bases on the current status of Pattern, Brightness.
This code may have syntax errors, I haven't tried compiling it, just off the cuff.
chuck.