small code required please

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:

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

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:

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.

Here's a snippet of using something like state machines. State machine is overstating it because it's a simple loop for each machine. The state transition is based on a time length for each state.

You have a series of things to do: turn on the flasher, wait, turn it off, wait, turn it on,...
Another series of things to do: turn fader a little, wait, ...

There are virtually two programs. Each stepping through a schedule of activities.

The common control is a loop with millisecond delay. This is to keep it simple having all numbers in milliseconds.

#define FADE_PIN put the real number here
#define FLASH_PIN put the real number here

#define FLASH_DURATION 100
int flashTimes[] =
{
FLASH_DURATION, // Start with LED going on
500, // Delay to second flash
FLASH_DURATION // Second flash
1500 // Wait for loop
};
const int FLASH_STATE_COUNT = 4;
int fadeTimes[] =
{
2000,
2000,
};
const int FADE_STATE_COUNT = 2;
int flashState;
int flashStateTimeCount;
int fadeStateTimeCount;
int fadeState;
int fadeValue;
setup()
{
// Setup up other stuff, such as the pins

flashState=0;
fadeState=0;
flashStateTimeCount = 0;
fadeStateTimeCount = 0;
digitalWrite ( FLASH_PIN, HIGH );
}
loop()
{
loopFade();
loopFlash();
delay(1);
}
loopFade()
{
int intervalSize = fadeTimes[fadeState]
int fadeValue = (int)((float)fadeCount/float(intervalSize);
if ( fadeState == 1 )
fadeValue = 256 - fadeValue;
analogWrite ( FADE_PIN, fadeValue );
fadeCount++;
if ( fadeCount >= intervalSize )
{
fadeCount = 0;
fadeState = (fadeState+1) % FADE_STATE_COUNT;
}
}
loopFlash()
{
int intervalSize = flashTimes[flashState];
flashCount++;
if ( flashCount >= intervalSize )
{
flashCount = 0;
flashState = (flashState+1) % FLASH_STATE_COUNT;
int direction = flashState % 2 == 0 ? HIGH : LOW;
digitalWrite ( FLASH_PIN, direction );
}
}