MultiFlash.h library and "state - machines"

Hi,
I’ve been using this ‘no delay’ library and code and it’s great but would like to be able to change the original ON & OFF times used in the setup from within the “main void loop” when the program is running , prior to calling the update function & also enable / disable the flasher ? Is that possible or can any one help.
Appropriate bit f sketch code attached below
Thanks
MikeH

//*
This programme use a forward and reverse sonar sensors on a childs electric car to prevent collisions.
the sonar code and associated action to switch off/on drive motors , the accelerator and apply brakes etc is writen and all OK.
(PLEASE IGNORE ANY MINOR SYNTAX ERRORS IN THE LISTING AS iHAVE CUT AND PASTED FROM THE ORIGNAL SCRIPT)
so I have not included it (other then to show where these blocks of code are located in the total scriptscript).
Since timing is critical it uses “state based code” ie. no delays hence the use of NewPing.h , Bounce2.h & LedFlasher.h ,
as delays cannot be tollerated critical ping process of NewPing.h
//*

// pre-amble
#include <LedFlasher.h> // used because its a “state processing” with no delays
#include <NewPing.h> // used for ping sensors also “state processing” - will not tolerate use of any delays in main loop
#include <Bounce2.h> //used to debounce , various other input signals for the car control eg . brake , accelerator pedal - also “state processing”

#define FORWARD_FLASHER_PIN 8 // input pin number for forward pin LED drive
#define REVERSE_FLASHER_PIN 9 // input pin number for reverse pin LED drive

// ledflasher object definitions
LedFlasher FORWARD_FLASH(FORWARD_FLASHER_PIN, 250, 250);
LedFlasher REVERSE_FLASH(REVERSE_FLASHER_PIN, 500, 500);

// global variable declaration (only ones appropriate to this issue shown)

int FORWARD_DISTANCE; // measured variable returned from the forward sonar ping routine
int REVERSE_DISTANCE; // measured variable returned from the forward sonar ping routine
int FORWARD_TRIP_DISTANCE_FORFLASH // Distance when forward led flashes starts
int REVERSE _TRIP_DISTANCE_FOR FLASH // Distance when reverse led flashes starts

// start set-up loop =====================================================================
void setup() {

pinMode(FORWARD_FLASH_PIN,OUTPUT); //define output
pinMode(REVERSE_FLASH_PIN,OUTPUT); //define output

// START - setup code for the fwd and rev sonar dectors -----------------------------
// etc . etc which works and return two global values
// FORWARD_DISTANCE
// REVERSE_DISTANCE
// END - setup code for sonar detectors --------------------------------------------------

// start the ledflashers
FORWARD_FLASH.begin();
REVERSE_FLASH.begin()

}
// END setup loop=======================================================================-

//START main loop ====================================================================
void loop() {

// START ping code -----------------------------------------------------------------------------------
// pin code here to obtain FORWARD_DISTANCE & REVERSE_DISTANCES
/ FORWARD_DISTANCE as int value is between 300 & 0
// REVERSE_DISTANCE as int value is between 300 & 0
//END ping code ---------------------------------------------------------------------------------------

// START bounce2 code -----------------------------------------------------------------------------
// code here to debounce other inputs … not applicable to this issue
//END boung 2 code ---------------------------------------------------------------------------------

//START my flasher code ----------------------------------------------------------------------------------------
{
//--------------------------------------------------------------------------------------------------------------
if (FORWARD_DISTANCE <= FORWARD_TRIP_DISTANCE_FORFLASH )
{ FORWARD_FLASH.update(); }
else
{ digitalWrite(FORWARD_FLASH_PIN,LOW);}

//--------------------------------------------------------------------------------------------------------------
if (REVERSE_DISTANCE <= REVERSE_TRIP_DISTANCE_FORFLASH )
{ REVERSE_FLASH.update(); }
else
{ digitalWrite(REVERSE_FLASH_PIN,LOW);}
//------------------------------------------------------------------------------------------------------------------

//* this code above is fine and works OK.
but what I would like to do is to be able to change the ON & OFF times of the flasher code such that
as the respective FORWARD & REVERSE distance become smaller the flash times can be changed (ie become faster).
I can create numerous flasher definitions in the pre-amble with different times and then call them for different distance.
but this seems a lot of additional repetative code and “clumsey”
I was wondering if I could use a simple bit of code to dynamically “re-write” the stored ON & OFF times originally specified ,
from the ‘void loop’ immediately before calling the “flasherupdate()” call.
… ideally using an algorith to calculate the the times required for a given distance , or to be read from a user pre-defined look-up table/array
Iit seems to me (lol) that the critical issue is being able to dynamically change the On & OFF times initially defined - or am I missing something about how the library can be used as it stands ???

… and also enable and dissable the flasher running in the loop when not required. .
Currently I do this by forcing the ouput to “LOW” in the “else” loop ? which also may be a bit “clumsey” ?
//*

} // END my flasher code //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

}
// END main loop =====================================================================

It would be helpful if you posted your code

Allan.

Add two more variables,
int forward_flash_rate = 250;
int reverse_flash_rate = 500;

Then adjust these values when you want to change the rate, and use them by re-loading your flash timers.

LedFlasher FORWARD_FLASH(FORWARD_FLASHER_PIN,forward_flash_rate , forward_flash_rate);
LedFlasher REVERSE_FLASH(REVERSE_FLASHER_PIN, reverse_flash_rate , reverse_flash_rate );

etc

Allan.

Hi,
Please give us a pointer to the LED libraries you are using...

OH. It's Nick's.. I Knew That

use them by re-loading your flash timers.

That Works. I was sure I tried re-loading with different values once and it didn't work. I'm REAL happy it does because I need that function in a project. Thanks!

Hi Guys,
Thanks both for suggested solution.
See my actual test code below.
I have included a TRACE function to shows what happening.

If I comment out the “update” line it complies and runs as required.
Led flashes on & off every 3 secs and TRACE info is correct.

If I include the update line it complies OK
The New time is updated Ok ( see serial TRACE output on screen)
But TRACE now shows the output pin flipping state every cycle through the loop.
The LedFlasher object doesn’t appear to be updating.
What am I doing wrong ?

Thanks

// PREAMBLE **************************************************************************************
#include <LedFlasher.h>
int LOOP_COUNTER=1; int i=1; uint16_t START_LOOP_TIME; uint16_t END_LOOP_TIME; //used by TRACE user function

int FWD_FLASH_TIME=3000;
LedFlasher FWD_FLASHER(13,FWD_FLASH_TIME,FWD_FLASH_TIME); //initial object definition
//************************************************************************************************

//************************************************
void setup()
{

Serial.begin(115200); //for “TRACE”

pinMode(13,OUTPUT); // on board ledpin
FWD_FLASHER.begin(); // initial object setup

}
//***********************************************

//*******************************************************************************************
void loop()
{
START_LOOP_TIME=micros(); // TRACE

//FWD_FLASH_TIME=500; LedFlasher FWD_FLASHER(13,FWD_FLASH_TIME,FWD_FLASH_TIME);

// **********when I uncomment this “update” line above and re-compile , the FWD_FLASH_TIME has been updated OK
// **********but pin 13 changes now state every loop cycle which seems to be about 10-15 micro seconds
// **********see SERIAL TRACE OUTPUT

FWD_FLASHER.update();

END_LOOP_TIME=micros(); // TRACE

TRACE(); // user call

i=i+1; //loop counter for TRACE

}
//********************************************************************************************

//===============================================================================================================
void TRACE()
{
// TRACE -SERIAL MONITOR OF PIN 13
Serial.print ("LOOP COUNT “) ;Serial.print(i); Serial.print(” ");
Serial.print ("LOOP TIME microsecs = “);Serial.print (END_LOOP_TIME - START_LOOP_TIME); Serial.print(” “);
Serial.print(“FLASH_TIME=”);Serial.print(FWD_FLASH_TIME); Serial.print(” “);
Serial.print(” PIN #13 OUTPUT STATE = ");Serial.println(digitalRead(13));
delay(300); // for readability - but obviously effects the “apparent” led flasher times for on-board LED
// comment out the delay to see real effect on the LED
}
//================================================================================================================