State engine-Library LED question! Fade up-Hold-then fade down.Is this possible?

Iv been playing with a state engine led sequencer and some <LedFlasher.h> and <LedFader.h> librarys that Mr Gammon has made. Had my sketch up and running after lots of pulling out of hair, and debugging.

With lots of help on the forum i have it working now, and i now can understand how a state engine works and the order of how to place the states (state machine variables) (do State Change) (initial State) .

Its hard for me, but im trying my very-very best… Old dogs and new tricks… (thats me)… Sorry…

I love librarys it makes Arduino much more accessible to the likes of me and other people who like to tinker and play with Arduino code.

Ive had fun with my son building Arduino Uno robots, and we have made a obstacle avoiding robot a line following robot, and a arduino android bluetooth HC-05 module smart car. And played with the code to power them with a servo or L298N motor modules.

I have been looking for a library that i can insert into my state engine LED sequencer that can (FADE UP) a led, then hold the led at maximum brightness (for a set time) then fade it down.

If i could make a library for the up and coming young… and old… (like me!) programmers this is how i would like it to be

-pin—dimup------min--------max--------(holdon)------time----dimdown-max—min on? stop?

–3----millis-------0----------200---------millis--------millis-------------200----0 false, true);

(1) Dim up…set time in seconds. dim up the LED to 0 to 200 in brightness.

(2) Set time to (holdon) set the time you want the LED to stay on the set brightness of LED.

(3) Dim down LED 200 down to 0 in brightness, in the seconds you have set.

Is it possible to make a library like this ?

I don’t know if Mr. Gammon’s library will work that way, surely it does. But it is definitely possible to make a state machine (not engine) that can do that. You have three states. Fading up, holding, and fading down.

Try this:
Before setup():

unsigned long beginTime =0;
int currentBrightness = 0; //for starting dark, otherwise set to whatever level of brightness 
     //you want at start.
  //keeps track of the brightness of the LED throughout the program.

When you need to adjust the brightness in your code:

modifyBrightness("time Between Steps","target Brightness"); //target Brightness is 
                            //anything from 0 to 255. 
                            //Use the number you want. Similarly with time Between Steps.
//For example modifyBrightness(100,255); to go to full brightness in 1/10 of a second steps.
if (currentBrightness =="targetBrightness"){ //reached target brightness 
                        //(whether that's 0 or some other target)
                        //again, use the number you are aiming for.
next state;
}

Add this function underneath your other code:

//Function definition
void modifyBrightness(int stepDelay, int target){
  if (millis() - beginTime >= stepDelay) {  //due for brightness change?
    beginTime = millis();  //save current time
    if (currentBrightness < target){  //Too dull
      currentBrightness++;  //increase brightness
    }
    if (currentBrightness > target){ //Too bright
      currentBrightness--;  //decrease brightness
    }
    analogWrite(LED, currentBrightness); //Light LED 
                //(connected to a PWM pin) to current brightness.
  }
}

Thanks Henry_Best, is that code for a library? or some code i have to insert in to my sketch? and if so im only tinker with arduino . I do not know how to make a library or even how to edit a library! and that is why i use ready made librarys like Mr gammons <LedFlasher.h> and <LedFader.h> librarys that have been posted for all of us to use.

And thanks Delta_G , a state machine (not a state engine) Sorry! To late ive posted it now .

Charles Babbage’s state engine (mechanical) computer 1837…Analytical mechanical Engine

Alan Turing’s computer (the finite state machine) 1941…Valve electric Computer

OK! I should of said in my (first post) that i have a problem with Mr gammons <LedFader.h> library.

When i run my program on my breadboard i have a strange anomaly, Orange led fade up, but with no fade down, just OFF? is it a conflict with the red led going on?

digitalWrite (LEDredPin, HIGH); digitalWrite (LEDredPin, LOW);

But when i run a test <LedFader.h> library program to test the leds and resistors and timing on my breadboard. I works perfectly on fade up, and fade down .

Why is this so? Why does the library program work fine when i test it as a (standalone) test on my breadboard!

But when i incorporate the library into my sketch it has the strange anomaly of (Orange led fade up, but with no fade down, just OFF?) Why?

Heres my sketch, its a bit ragged around the edges. and it looks a mess . (BUT IT WORKS) and thats all i care about . so no LOL’s and WTF is that . it works for me.

All i would like is a library that can fade up–for 2 seconds–hold–then fade down for 2 seconds…
without it just going off on fade down.

Please.

 #include <LedFader.h>
 #include <LedFlasher.h>

     // pin assignments
 const byte StrobePin = 12;    // PWM  does what it does!!(flash)and keeps on flashing..  
 const byte LEDorangePin = 9;  // PWM   fades up (but does not fade down??? WHY???
 const byte LEDredPin = 8;     // Goes on for one second just before the orange goes out 
 const byte FloodlightPin = 7; // Goes on, and (stays) on at the very, very, start

 // Fader                    pin       min    max   millis     on?    stop?
 LedFader LEDorangeFader (LEDorangePin, 0,    200,  6000,    false,   true); //= 6 secs

 // Flasher                pin          off-time      on-time       on?
 LedFlasher strobe    (StrobePin,         900,         100,      false); // = One second

 // states for the state machine
 typedef enum
  {
  initialState,
  wantFloodlight,         // ALWAYS STAYS (ON) after the start up sequence. 
  wantStrobe,             // ALWAYS STAYS (ON) after the start up sequence. 
  wantLEDredstartup,      // Goes on for one sec when the orange led is on (at end)
  wantLEDorangestartup,   // Startup Orange mode
  wantLEDorangeon,        // Orange mode (ON)
  wantLEDredon,           // Red LED orange mode,Goes on when orange led is on (at end)
  wantLEDorangeoff,       // Orange mode (OFF)
  wantLEDredoff,          // Red LED orange mode,Goes (off) when orange led goes out..
 
   } states;

 // state machine variables
 states state = initialState;
 unsigned long lastStateChange = 0;
 unsigned long timeInThisState = 1000;

 void setup ()
  {

 pinMode (FloodlightPin, OUTPUT);
 pinMode (StrobePin, OUTPUT);
 pinMode (LEDredPin, OUTPUT);
 
 
  // set up faders, flashers 
  LEDorangeFader.begin ();
  
  strobe.begin ();
  
  }  // end of setup
       
 void doStateChange ()
  {
  lastStateChange = millis ();    // when we last changed states
  timeInThisState = 1000;         // default one second between states

  switch (state)
   {
  case initialState:
  state = wantFloodlight;
  break;
         
 case wantFloodlight:
 digitalWrite (FloodlightPin, HIGH);
 state = wantStrobe;
 break;
 
 case wantStrobe:
 strobe.on();
 state = wantLEDorangestartup;
 break;
 
 case wantLEDorangestartup:
      LEDorangeFader.on();
      state = wantLEDredon;
      timeInThisState = 6000;
      break;
         
 case wantLEDredon:
 digitalWrite (LEDredPin, HIGH);
 state = wantLEDorangeoff;         
 timeInThisState = 2000;
 break;
         
 case wantLEDorangeoff:   
 digitalWrite (LEDredPin, LOW);
 LEDorangeFader.off();
 state = wantLEDorangestartup;
 break;
   
         
   }  // end of switch on state
  }  // end of doStateChange

 void loop ()
  {
   if (millis () - lastStateChange >= timeInThisState)
     doStateChange ();
   // update fader, flasher
   LEDorangeFader.update ();
   strobe.update ();
 
   }  // end of loop