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 =====================================================================