Onebutton library doubleclick

Hello All,

I have this script that works blinking police lights, then I tried using the one button library double-click to run the police lights and it doesn't do the blinking.

Can someone tell me what is wrong?

Thanks for any help

The first script is the working script without the onebutton library



/*
* John Guarnieri altered code to fit my needs 02_08_2022 
* https://www.youtube.com/watch?v=rUtDfadf8Jk
* LED Blink with millis() with different time interval of ON and OFF time
* Written by Ahmad Shamshiri for Robojax.com and Robojax YouTube channel
* written on Aug 05, 2019 in Ajax, Ontario, Canada at 12:41
* Watch video instruction:https://youtu.be/rUtDfadf8Jk
* Watch introduction video to millis(): https://youtu.be/u2HsiTS8niQ
*/
const int fog_light_low_beam = 6;
const int fog_light_low_beam_indicator = 7;
const int fog_light_high_beam = 8;
const int fog_light_high_beam_indicator = 9;


const long onDuration_1 = 250;// OFF time for LED//orig 100
const long offDuration_1 = 200;// ON time for LED //orig 500 lower this number for a faster blink

const long onDuration_2 = 250;// OFF time for LED// orig 100
const long offDuration_2 = 200;// ON time for LED//orig 500 lower this number for a faster blink


int LEDState_1 = HIGH;// initial state of LED
int LEDState_2 = LOW;// initial state of LED

long rememberTime_1 = 0;// this is used by the code
long rememberTime_2 = 0;

void setup() {
pinMode(fog_light_low_beam,OUTPUT);// define LEDpin as output
pinMode(fog_light_low_beam_indicator,OUTPUT);// define LEDpin as output
//
pinMode(fog_light_high_beam,OUTPUT);// define LEDpin as output
pinMode(fog_light_high_beam_indicator,OUTPUT);// define LEDpin as output

digitalWrite(fog_light_low_beam,LEDState_1);// set initial state
digitalWrite(fog_light_low_beam_indicator,LEDState_1);// set initial state
//
digitalWrite(fog_light_high_beam,LEDState_2);// set initial state
digitalWrite(fog_light_high_beam_indicator,LEDState_2);// set initial state
}//end setup

void loop() 
{

//  LED blink with millis()

if( LEDState_1 == HIGH )
{
   if( (millis() - rememberTime_1) >= onDuration_1)
      {   
        LEDState_1 = LOW;// change the state of LED
        rememberTime_1 = millis();// remember Current millis() time
      }
   }
   else
   {   
       if( (millis()- rememberTime_1) >= offDuration_1){     
       LEDState_1 = HIGH;// change the state of LED
       rememberTime_1 = millis();// remember Current millis() time
   }
   }//if else

  //  LED blink with millis()
  digitalWrite(fog_light_low_beam,LEDState_1);// turn the LED ON or OFF
  digitalWrite(fog_light_low_beam_indicator,LEDState_1);// turn the LED ON or OFF


  if( LEDState_2 ==LOW )
  {
    if( (millis() - rememberTime_2) >= onDuration_2){   
    LEDState_2 = HIGH;// change the state of LED
    rememberTime_2 = millis();// remember Current millis() time
  }
     }
     else
     {   
        if( (millis()- rememberTime_2) >= offDuration_2)
        {     
          LEDState_2 = LOW;// change the state of LED
          rememberTime_2 = millis();// remember Current millis() time
        }
  }//if

  //  LED blink with millis()
  digitalWrite(fog_light_high_beam,LEDState_2);// turn the LED ON or OFF
  digitalWrite(fog_light_high_beam_indicator,LEDState_2);// turn the LED ON or OFF

}// loop ends

//======================================================================
onebutton script here
//======================================================================

//Arduino OneButton Library http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
//3 Way to use a button using OneButton Library https://efcomputer.net.au/blog/3-way-to-use-a-button-using-onebutton-library/
//Sample for using the OneButton library
//Here is a simple sample that uses the OneButton library to change the default led on pin 13 when double clicking a button attached on pin A1.

/*
S01_SimpleOneButton
Simple OneButton sketch that shows how to ???

The circuit:
* Connect a pushbutton to pin A1 (ButtonPin) and ground
* and see results on pin 13 (StatusPin).
* 03.03.2011 created by Matthias Hertel
*/

#include "OneButton.h"


#define LEDon  HIGH
#define LEDoff  LOW
//
#define DISABLED  false
#define ENABLED  true






// Police
//long
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 100;    // the debounce time; increase if the output flickers

unsigned long
timeStart_3;  //short press time check   
bool turn_on_the_police_lights = DISABLED;
bool the_police_lights_are_on = DISABLED;
//Police lights
const long onDuration_1 = 250;// OFF time for LED//orig 100
const long offDuration_1 = 200;// ON time for LED //orig 500 lower this number for a faster blink

const long onDuration_2 = 250;// OFF time for LED// orig 100
const long offDuration_2 = 200;// ON time for LED//orig 500 lower this number for a faster blink

int Long_Switch_Press_police_lights_Time = 2500; //2.5 seconds
int LEDState_1 = HIGH;// initial state of LED
int LEDState_2 = LOW;// initial state of LED

long rememberTime_1 = 0;// this is used by the code
long rememberTime_2 = 0;
//end Police
// Setup a new OneButton on pin A1.  
//OneButton button(A1);// original


//OneButton button(5); //original

OneButton switch_turn_on_the_high_beam_fog_lights(5); //original
//buttons
const byte button_blink_the_fog_lights = 5;
//const byte switch_turn_on_the_high_beam_fog_lights = 5;


//LED's
const int fog_light_low_beam = 6;
const int fog_light_low_beam_indicator = 7;
const int fog_light_high_beam = 8;
const int fog_light_high_beam_indicator = 9;
// setup code here, to run once:
void setup() {
// enable the standard led on pin 13.
pinMode(fog_light_low_beam,OUTPUT);
pinMode(fog_light_low_beam_indicator,OUTPUT);
pinMode(fog_light_high_beam,OUTPUT);
pinMode(fog_light_high_beam_indicator,OUTPUT);

// link the doubleclick function to be called on a doubleclick event.   
switch_turn_on_the_high_beam_fog_lights.attachDoubleClick(doubleclick);
} // setup


// main code here, to run repeatedly: 
void loop() {
// this keeps watching for the push button:
switch_turn_on_the_high_beam_fog_lights.tick();

// You can implement other code in here or just wait a while 
delay(10);
} // loop


// this function will be called when the button was pressed 2 times in a short timeframe.
void doubleclick() {
if ( (millis() - lastDebounceTime) > debounceDelay) //debounce button press
lastDebounceTime = millis(); //set the current time
//static int m = LOW;
// reverse the LED 
//m = !m;
//digitalWrite(13, m);

run_police_lights();
} // doubleclick


//===================================================
//Police
void run_police_lights()
{

  if (!the_police_lights_are_on)
  {
        
 
     turn_on_the_police_lights = ENABLED;
     
    
    the_police_lights_are_on = ENABLED;
    Serial.println("the police lights are on"); 
  //  LED blink with millis()

  if( LEDState_1 == HIGH )
  {
     if( (millis() - rememberTime_1) >= onDuration_1)
     {   
        LEDState_1 = LOW;// change the state of LED
        rememberTime_1 = millis();// remember Current millis() time
     }
  }
  else
  {   
      if( (millis()- rememberTime_1) >= offDuration_1){     
      LEDState_1 = HIGH;// change the state of LED
      rememberTime_1 = millis();// remember Current millis() time
  }
  }//if else

  //  LED blink with millis()
  digitalWrite(fog_light_low_beam,LEDState_1);// turn the LED ON or OFF
  digitalWrite(fog_light_low_beam_indicator,LEDState_1);// turn the LED ON or OFF


  if( LEDState_2 ==LOW )
  {
    if( (millis() - rememberTime_2) >= onDuration_2){   
    LEDState_2 = HIGH;// change the state of LED
    rememberTime_2 = millis();// remember Current millis() time
  }
     }
     else
     {   
        if( (millis()- rememberTime_2) >= offDuration_2)
        {     
          LEDState_2 = LOW;// change the state of LED
          rememberTime_2 = millis();// remember Current millis() time
        }
  }//if

  //  LED blink with millis()
  digitalWrite(fog_light_high_beam,LEDState_2);// turn the LED ON or OFF
  digitalWrite(fog_light_high_beam_indicator,LEDState_2);// turn the LED ON or OFF
  
  //turn_on_the_police_lights = DISABLED;      
 
  }
  else
  
  {
   
    the_police_lights_are_on = DISABLED;
    //Police_lights_are_on_stop_all_other_functions = DISABLED;
    digitalWrite(fog_light_high_beam,LEDoff);
    digitalWrite(fog_light_high_beam_indicator,LEDoff);
    digitalWrite(fog_light_low_beam,LEDon);
    digitalWrite(fog_light_low_beam_indicator,LEDon);
    
  }

  

   

} //end void

you haven't registered your function as callback.
So the onebutton lib doesn't know that you would like to have doubleclick() called if a double click occures.

it seems you have 2 loops(), 2 setups(). This isn't the way a sketch should look like. You have to combine code in ONE loop(), ONE setup()

Look at the BlinkMachine example that comes with the library. Your double click routine needs to just toggle a flag and have all your blinking code inside loop().

That's because @stspringer posted 2 separate sketches in the same pair of code tags

NOTE

Oh ok thanks blh64 I will try that

Thanks blh64 that worked Where do I find all the functions of onebutton library like button state or whatever.

Problem:

When I run the double-click to start the police lights and then I double click again to stop the police lights, sometimes the double click button "5" is left "LOW" when it should be left "HIGH"

For example, if I double click "out" "second double click" of running the police lights and all 4 LED's are on, and then I click button 4 the blink LED's button, only the first 2 led's will blink when all 4 LED's should blink, the reason is the button 5 the double-click button is left "LOW" when it should be "HIGH", if I press button 5 with a single click, and then press the blink button "4" then all 4 LED's will blink as they should.

I hope this is clear, you will have to run it a few time to see what I am talking about. It doesn't happen all the time, it is not consistent, but it does happen.


//I edited code from Blackfin "Hold button for 5 seconds for an LED to turn and stay on"  https://forum.arduino.cc/t/hold-button-for-5-seconds-for-an-led-to-turn-and-stay-on/574197/3
//John Guarnieri script 01_17_2022 Harley Davidson Probeam Fog Lights blink at 100ms and also poer off all fog lights with long button press
//using OneButton library for police lights double click
#include "OneButton.h"
//
#define DISABLED  false
#define ENABLED  true



OneButton switch_turn_on_the_police_lights(5); //original
bool start_police_lights_now = DISABLED;

#define LEDon  HIGH
#define LEDoff  LOW


//int
int blinkInterval = 100; //100ms
int short_key_press_time = 30;
int Long_Switch_Press_Time = 2500; //2.5 seconds


//unsigned long
unsigned long previousBlink;
unsigned long currentMillis;
unsigned long previousMillis;

unsigned long
timeStart_1; //long press time check  

unsigned long
timeStart_2;  //short press time check   


//buttons
const byte button_blink_the_fog_lights = 4;
const byte switch_turn_on_the_high_beam_fog_lights = 5;

//LED's
const int fog_light_low_beam = 6;
const int fog_light_low_beam_indicator = 7;
const int fog_light_high_beam = 8;
const int fog_light_high_beam_indicator = 9;

//long
long lastDebounceTime = 0;  // the last time the output pin was toggled


long debounceDelay = 50;    // the debounce time; increase if the output flickers


//bool
bool blinkState = DISABLED;
bool First_Long_Press = ENABLED; 
bool First_Short_Press = ENABLED;
bool Do_Not_Run_First_Short_Press_Flag = DISABLED;
bool the_high_beam_fog_lights_are_on = DISABLED;
bool All_The_Fog_Lights_Are_Off = DISABLED;
bool high_beam_pin_flag = DISABLED;
bool second_long_press_dont_turn_on_the_high_beam_fog_lights = DISABLED;



// Police


//Police lights
const long onDuration_1 = 250;// OFF time for LED//orig 100
const long offDuration_1 = 200;// ON time for LED //orig 500 lower this number for a faster blink

const long onDuration_2 = 250;// OFF time for LED// orig 100
const long offDuration_2 = 200;// ON time for LED//orig 500 lower this number for a faster blink

int Long_Switch_Press_police_lights_Time = 2500; //2.5 seconds
int LEDState_1 = HIGH;// initial state of LED
int LEDState_2 = LOW;// initial state of LED

long rememberTime_1 = 0;// this is used by the code
long rememberTime_2 = 0;
//end Police



bool
bCheckingSwitch_1; //long press check switch_turn_on_the_high_beam_fog_lights

bool
bCheckingSwitch_2;  //short press check switch_turn_on_the_high_beam_fog_lights
byte
  currSwitch, //high beam fog light button state changes
  lastSwitch;


void setup() 
{  
 //need this for serial monitor output
 // link the doubleclick function to be called on a doubleclick event. 
 switch_turn_on_the_police_lights.attachDoubleClick(doubleclick);
 switch_turn_on_the_police_lights.setDebounceTicks(100);
 // set debouncing time
 switch_turn_on_the_police_lights.setDebounceTicks(debounceDelay);
 
 
 
 
 pinMode(fog_light_low_beam,OUTPUT);
 pinMode(fog_light_low_beam_indicator,OUTPUT);
 pinMode(fog_light_high_beam,OUTPUT);
 pinMode(fog_light_high_beam_indicator,OUTPUT);

 pinMode(button_blink_the_fog_lights,INPUT_PULLUP);
pinMode( switch_turn_on_the_high_beam_fog_lights,INPUT_PULLUP );
lastSwitch = digitalRead( switch_turn_on_the_high_beam_fog_lights );
bCheckingSwitch_1 = DISABLED;

bCheckingSwitch_2 = DISABLED;
//turn on the led
digitalWrite(fog_light_high_beam,LEDoff);
digitalWrite(fog_light_high_beam_indicator,LEDoff);
digitalWrite(fog_light_low_beam,LEDon);
digitalWrite(fog_light_low_beam_indicator,LEDon);
  

Serial.begin(115200);
} //end void setup

void loop() 
{

switch_turn_on_the_police_lights.tick();
if(start_police_lights_now)
{
   run_police_lights();
   
}
else //start_police_lights_now = DISABLED
{
  
  if ( (millis() - lastDebounceTime) > debounceDelay) //debounce button press
  {
    currSwitch = digitalRead(switch_turn_on_the_high_beam_fog_lights);
    lastDebounceTime = millis(); //set the current time
    //call the function check_high_beam_fog_light_switch()
     check_high_beam_fog_light_switch();
  } 
  
   blink_the_fog_lights(); 
   
}   
} //end void loop

void check_high_beam_fog_light_switch()
{
//if the switch is low now and doesn't match its
//previous reading, it means the switch has changed
//either unpressed-to-pressed or pressed-to-unpressed
 
if( currSwitch != lastSwitch )
{   
   //if low now, the change was unpressed to pressed
   if( currSwitch == LOW )
   {
      //in that case, get the time now to start the
      //5-sec delay...
      timeStart_1 = millis();
      timeStart_2 = millis(); 
      //police lights
     // timeStart_3 = millis(); 
      //...indicate we're now timing a press...
      bCheckingSwitch_1 = ENABLED;
      bCheckingSwitch_2 = ENABLED;
      //Serial.println("short press ");
      //Serial.println(timeStart_2);
   }  //if
   else
   {
      //was a rising edge (button being released)
      //cancel checking the switch timing now since 
      //it's not being held hown
     
          bCheckingSwitch_1 = DISABLED;
          bCheckingSwitch_2 = DISABLED;
       
      //Serial.println("Short_press release");
   }//end else
    
   //and save the lastSwitch = currSwitch;
   lastSwitch = currSwitch;
     
}//end if

  
//if we haven't seen a rising edge, keep checking the
//timing
if( bCheckingSwitch_1 )//long press happening here switch_turn_on_the_high_beam_fog_lights
{
   //Serial.println("short press happening here");
   
    toggle_OFF_and_ON_all_the_fog_lights_long_press();
  
   
  
}//end if

if( bCheckingSwitch_2 )//short press happening here switch_turn_on_the_high_beam_fog_lights
{
   
   //this flag happens if all the fog lights were turned off, 
   //you dont want to turn on any fog lights until 
   //another long press happens to turn all the fog lights back on
   if(!Do_Not_Run_First_Short_Press_Flag)
   {
      toggle_OFF_and_ON_just_the_high_beam_fog_lights(); 
   }
  
}//end if   
}   //end void check_high_beam_fog_light_switch()




void toggle_OFF_and_ON_all_the_fog_lights_long_press()
{
//if the millis count now is 2500 or more higher
//than it was when the press was detected (timeStart_1),
//turn on the LED

if( (millis() - timeStart_1 ) >= Long_Switch_Press_Time)          //long press 5 seconds turn off all the fog lights

  if (First_Long_Press) //switch_turn_on_the_high_beam_fog_lights
  {
     //because the low beam fog lights are always on go low first
     digitalWrite(fog_light_low_beam,LEDoff);
     digitalWrite(fog_light_low_beam_indicator,LEDoff);
     digitalWrite(fog_light_high_beam,LEDoff);
     digitalWrite(fog_light_high_beam_indicator,LEDoff);
     high_beam_pin_flag = DISABLED;
     First_Long_Press = DISABLED;
     bCheckingSwitch_1 = DISABLED;
     //latest add seems to work
     bCheckingSwitch_2 = DISABLED;
     currSwitch = 1;
     All_The_Fog_Lights_Are_Off = ENABLED;
     //this flag happens if all the fog lights were turned off, 
     //you dont want to turn on any fog lights until 
     //another long press happens to turn all the fog lights back on
     Do_Not_Run_First_Short_Press_Flag = ENABLED;
     Serial.println("Long press high beam fog lights are OFF");
     //Serial.println("high_beam_pin_flag"); 
     //Serial.println(high_beam_pin_flag); 
         
  }
  else //second long press here switch_turn_on_the_high_beam_fog_lights
  {
     digitalWrite( fog_light_low_beam,LEDon);
     digitalWrite( fog_light_low_beam_indicator,LEDon);
            
     //fix I set lastSwitch to HIGH because after turning off all the fog lights
     //and then turning all the fog lights back on, when I pressed the turn on the high beam button it was
     //already in the LOW position so you had to press it twice to turn on the high beam fog lights, setting lastSwitch = HIGH; fixed it
     //lastSwitch was LOW screwing me up so set it to HIGH
     //this lastSwitch = HIGH; fires off a short press if currSwitch, and/or lastSwitch, "the high beam fog light button state changes", get out of sync on a button press, in some situatons. 
  
     lastSwitch = HIGH;
     Serial.println("Long press high beam fog lights are ON");
     second_long_press_dont_turn_on_the_high_beam_fog_lights = ENABLED;
     high_beam_pin_flag = ENABLED;
     First_Long_Press = ENABLED;
     All_The_Fog_Lights_Are_Off = DISABLED;
    
     //FIX
     //this is the fix set "First_Short_Press = false;" for when First_Short_Press 
     //was = to "true" "HIGH" and should have been false "LOW" if
     //it were true at the wrong time the high beams would
     //only turn off on a second short press, and not the first short press which is normal operation
     //on and off on the first short press
    
     First_Short_Press = DISABLED; //LOW
           
     bCheckingSwitch_1 = DISABLED;
     //latest add seems to work
     bCheckingSwitch_2 = DISABLED;
     currSwitch = 1;
     //turn off the flag
     Do_Not_Run_First_Short_Press_Flag = DISABLED;         
  }
}// end void toggle_OFF_and_ON_all_the_fog_lights_long_press()


void toggle_OFF_and_ON_just_the_high_beam_fog_lights()  //short press
{
//if the millis count now is 2500 or more higher
//than it was when the press was detected (timeStart_1),
//turn on the LED
if( (millis() - timeStart_2 ) >= short_key_press_time          //short press time
)
  //Serial.println("First_Short_Press");
  //Serial.println(First_Short_Press);
  //Serial.println("Do_Not_Run_First_Short_Press_Flag");
  //Serial.println(Do_Not_Run_First_Short_Press_Flag);
  if (First_Short_Press && !Do_Not_Run_First_Short_Press_Flag )//if First_Short_Press true set the fog_light_high_beam, HIGH "ON"
  {
     //because the low beam fog lights are always on go low first
     second_long_press_dont_turn_on_the_high_beam_fog_lights = DISABLED;
     digitalWrite( fog_light_high_beam,LEDon);
     digitalWrite( fog_light_high_beam_indicator,LEDon);
     high_beam_pin_flag = ENABLED;
     the_high_beam_fog_lights_are_on = ENABLED;
     First_Short_Press = DISABLED;
     bCheckingSwitch_2 = DISABLED;
     currSwitch = 1;
     //testing here
     second_long_press_dont_turn_on_the_high_beam_fog_lights = DISABLED;
     Serial.println("Short press high beam fog lights are ON");
       
  }
  else //if First_Short_Press = false turn set the fog_light_high_beam, LOW "OFF"
  {
     //Serial.println("Do_Not_Run_First_Short_Press_Flag");
     //Serial.println(Do_Not_Run_First_Short_Press_Flag);
           
     digitalWrite( fog_light_high_beam,LEDoff);
     digitalWrite( fog_light_high_beam_indicator,LEDoff);
              
     high_beam_pin_flag = DISABLED;
     the_high_beam_fog_lights_are_on = DISABLED;
     First_Short_Press = ENABLED;
     bCheckingSwitch_2 = DISABLED; 
     currSwitch = 1;
  
     Serial.println("Short press high beam fog lights are OFF"); 
             
  }              
} //end void toggle_OFF_and_ON_just_the_high_beam_fog_lights()


void blink_the_fog_lights()
{
 if (!All_The_Fog_Lights_Are_Off)//NOT all the fog lights are off so proceed, else don't proceed
 {
    currentMillis = millis(); // better to store in variable, for less jitter
    if (currentMillis - previousMillis >= blinkInterval) 
    {   // enough time passed yet?
       previousMillis = currentMillis; // sets the time we wait "from"
  
       if (digitalRead(button_blink_the_fog_lights)==LOW) //blink fog light switch pressed LOW
       {   
          if (high_beam_pin_flag)//YES true high beam fog lights are ON you can blink both low and high fog lights
          {         
             
             if (!second_long_press_dont_turn_on_the_high_beam_fog_lights)//NOT
             {
                Serial.println("blinking low beams and high beams");
                digitalWrite(fog_light_low_beam,!digitalRead(fog_light_low_beam)); // shortcut to toggle the LED
                digitalWrite(fog_light_low_beam_indicator,!digitalRead(fog_light_low_beam_indicator)); // shortcut to toggle the LED 
                digitalWrite(fog_light_high_beam,!digitalRead(fog_light_high_beam)); // shortcut to toggle the LED
                digitalWrite(fog_light_high_beam_indicator,!digitalRead(fog_light_high_beam_indicator)); // shortcut to toggle the LED 
             }               
             else
             {
                Serial.println("blinking just the low beams");
                digitalWrite(fog_light_low_beam,!digitalRead(fog_light_low_beam)); // shortcut to toggle the LED
                digitalWrite(fog_light_low_beam_indicator,!digitalRead(fog_light_low_beam_indicator)); // shortcut to toggle the LED 
             }
         }      
          else  //false the high beam fog lights are off just blink the low beam fog lights
          { 
             Serial.println("blinking just the low beams");
             digitalWrite(fog_light_low_beam,!digitalRead(fog_light_low_beam)); // shortcut to toggle the LED
             digitalWrite(fog_light_low_beam_indicator,!digitalRead(fog_light_low_beam_indicator)); // shortcut to toggle the LED 
          }      
       }
       else  //blink switch released not pressed HIGH
       {
    
          if (!high_beam_pin_flag)//NOT means false the high beam fog lights are off
          {   
             digitalWrite(fog_light_low_beam,LEDon);
             digitalWrite(fog_light_low_beam_indicator,LEDon);  
          }
          else  //true means the high beam fog lights are ON
          {
             digitalWrite(fog_light_low_beam,LEDon);
             digitalWrite(fog_light_low_beam_indicator,LEDon);  
             if (!second_long_press_dont_turn_on_the_high_beam_fog_lights)//NOT second long press so turn on the high beam fog lights
            {
               digitalWrite(fog_light_high_beam,LEDon);
               digitalWrite(fog_light_high_beam_indicator,LEDon); 
            }
            else //true second_long_press_dont_turn_on_the_high_beam_fog_lights, so do NOT turn on the high beam fog lights
            {
               digitalWrite(fog_light_high_beam,LEDoff);
               digitalWrite(fog_light_high_beam_indicator,LEDoff);
            }              
         }
       }  //end (digitalRead(button_blink_the_fog_lights)==LEDoff)  
    }//end millis
 }//end all the fog lights are not off
} //end void blink_the_fog_lights()

// this function will be called when the button was pressed 2 times in a short timeframe.
void doubleclick() {


if (!start_police_lights_now)
{
  start_police_lights_now = ENABLED; 
}
else
{
  start_police_lights_now = DISABLED; 
}
} //end  doubleclick


//Police
void run_police_lights()
{

  
  if(start_police_lights_now)
  {  
     
    
    
    Serial.println("the police lights are on"); 
  //  LED blink with millis()

  if( LEDState_1 == HIGH )
  {
     if( (millis() - rememberTime_1) >= onDuration_1)
     {   
        LEDState_1 = LOW;// change the state of LED
        rememberTime_1 = millis();// remember Current millis() time
     }
  }
  else
  {   
      if( (millis()- rememberTime_1) >= offDuration_1){     
      LEDState_1 = HIGH;// change the state of LED
      rememberTime_1 = millis();// remember Current millis() time
  }
  }//if else

  //  LED blink with millis()
  digitalWrite(fog_light_low_beam,LEDState_1);// turn the LED ON or OFF
  digitalWrite(fog_light_low_beam_indicator,LEDState_1);// turn the LED ON or OFF


  if( LEDState_2 ==LOW )
  {
    if( (millis() - rememberTime_2) >= onDuration_2){   
    LEDState_2 = HIGH;// change the state of LED
    rememberTime_2 = millis();// remember Current millis() time
  }
     }
     else
     {   
        if( (millis()- rememberTime_2) >= offDuration_2)
        {     
          LEDState_2 = LOW;// change the state of LED
          rememberTime_2 = millis();// remember Current millis() time
        }
  }//if

  //  LED blink with millis()
  digitalWrite(fog_light_high_beam,LEDState_2);// turn the LED ON or OFF
  digitalWrite(fog_light_high_beam_indicator,LEDState_2);// turn the LED ON or OFF
  
  //turn_on_the_police_lights = DISABLED;      
 
  }
  else
  
  {
   
    
    
    if (!start_police_lights_now)
    {
      digitalWrite(fog_light_high_beam,LEDoff);
      digitalWrite(fog_light_high_beam_indicator,LEDoff);
      //digitalWrite(fog_light_low_beam,LEDon);
      //digitalWrite(fog_light_low_beam_indicator,LEDon);
    }
    
    
    
  }

  

} //end void

Which is correct? you seem to be using pin 5 for 2 things. You should also make your switch a OneButton object as well so no need to do all that debouncing.

I guess it's because I added the onebutton library to do a double click, does that mean I can't use my original code const byte switch_turn_on_the_high_beam_fog_lights = 5; I have to have all my button presses done by the onebutton library?

const byte switch_turn_on_the_high_beam_fog_lights = 5; //original
OneButton switch_turn_on_the_police_lights(5); //not the original

I want button 5 to do 3 jobs, turn on and off the high beam fog lights, turn on and off the police lights "with a doublclick" turn off all the fog lights with a long press 2.5 seconds.

It all works except for the button 5 being left LOW at times when it should be HIGH

How do I check the state of a button with onebutton library?

You have the source code for OneButton. Look in Documents\Arduino\libraries\OneButton\src (Windows).

Note that since you want 3 events to happen, in order to get to the double-click event, a single-click event will also happen

Thank you,

So how do you get around skipping the single click while doing the double-click? Use bool flags?

I did a quick check and it appears the 1 click does not come before the double click.

#include <OneButton.h>

#define PIN_INPUT 2
#define PIN_LED 13


// Setup a new OneButton on pin PIN_INPUT
// The 2. parameter activeLOW is true, because external wiring sets the button to LOW when pressed.
OneButton button(PIN_INPUT, true);

int ledState = LOW;

void setup()
{
  Serial.begin(115200);
  Serial.println("One Button Example with polling.");

  pinMode(PIN_LED, OUTPUT); // sets the digital pin as output
  digitalWrite(PIN_LED, ledState);
  button.attachClick(singleClick);
  button.attachDoubleClick(doubleClick);
  button.attachLongPressStart(longStart);
  button.attachDuringLongPress(longDuring);
  button.attachLongPressStop(longStop);
}

unsigned long pressStart;

void loop()
{
  button.tick();

  delay(10);
}

void longStart() {
  pressStart = millis();
}

void longStop() {
  Serial.print("Long pressed for ");
  Serial.print(millis() - pressStart);
  Serial.println(" msec");
}

void longDuring() {
  Serial.print("Long continuing for ");
  Serial.print(millis() - pressStart);
  Serial.println(" msec");
}

void singleClick()
{
  Serial.println("x1");

  ledState = !ledState; // reverse the LED
  digitalWrite(PIN_LED, ledState);
}



void doubleClick()
{
  Serial.println("x2");

  ledState = !ledState; // reverse the LED
  digitalWrite(PIN_LED, ledState);
}

which produced (single click, double click and then click & hold)

One Button Example with polling.
x1
x2
Long continuing for 9 msec
Long continuing for 19 msec
Long continuing for 31 msec
Long continuing for 41 msec
Long continuing for 51 msec
Long continuing for 61 msec
Long continuing for 72 msec
Long continuing for 82 msec
Long continuing for 92 msec
...
Long pressed for 2883 msec

I am using onebutton just for the double-click in my script button 5 so you think I am ok with that?

no.

Ok I will use onebutton for both buttons thanks

Ok I am using the onebutton library for my two buttons in my script and I can't get all the LEDs to blink I can blink just two of the four LEDs can someone tell why void blink_the_fog_lights_now()
will not blink my high beam fog light LED's.

button 4 blinks the low beam fog lights, and the high beam fog lights "if they are ON", button 5 turns toggles on and off, the high beam fog lights, if the high beam fog lights are on they should blink with a button 4 press, they do NOT can't figure out why.

Also something weird, when I press any button 4 or 5 there seems to be a 1000ms "1 second" delay before the LED's come on. I am not used to seeing that delay when I do NOT use a library. Like if I press button 4 to blink the LED's 1 second, has gone by before the LED's blink. Hope I am clear on this issue, its not that big a deal but I am curious if it is the library that is the slowdown.

I altered this script I found for my application One button library long press - #3 by yogitaw
Thank you

//https://forum.arduino.cc/t/one-button-library-long-press/909389/3
//BEGIN Declarations

#include "OneButton.h"

// Setup a new OneButton push buttons on pin 4 and 5
OneButton blink_the_fog_lights(4,true); //original
OneButton switch_turn_on_the_high_beam_fog_lights(5, true);


//define
#define DISABLED  false
#define ENABLED  true
#define LEDon  HIGH
#define LEDoff  LOW

//unsigned long
unsigned long previousBlink;
unsigned long currentMillis;
unsigned long previousMillis;


//LED's
const int fog_light_low_beam = 6;
const int fog_light_low_beam_indicator = 7;
const int fog_light_high_beam = 8;
const int fog_light_high_beam_indicator = 9;

//bool
bool ok_blink_the_fog_lights = DISABLED;
bool start_police_lights_now = DISABLED;
bool blinkState = DISABLED;
bool First_Long_Press = ENABLED; 
bool First_Short_Press = ENABLED;
bool Do_Not_Run_First_Short_Press_Flag = DISABLED;
bool the_high_beam_fog_lights_are_on = DISABLED;
bool All_The_Fog_Lights_Are_Off = DISABLED;
bool high_beam_pin_flag = DISABLED;
bool second_long_press_dont_turn_on_the_high_beam_fog_lights = DISABLED;



//Police lights
const long onDuration_1 = 250;// OFF time for LED//orig 100
const long offDuration_1 = 200;// ON time for LED //orig 500 lower this number for a faster blink

const long onDuration_2 = 250;// OFF time for LED// orig 100
const long offDuration_2 = 200;// ON time for LED//orig 500 lower this number for a faster blink

int Long_Switch_Press_police_lights_Time = 2500; //2.5 seconds
int LEDState_1 = HIGH;// initial state of LED
int LEDState_2 = LOW;// initial state of LED

long rememberTime_1 = 0;// this is used by the code
long rememberTime_2 = 0;
//end Police

//int
int debounceDelay = 100;
int blinkInterval = 100; //100ms
int short_key_press_time = 30;
int Long_Switch_Press_Time = 2500; //2.5 seconds


//END Declarations



// setup code here, to run once:
void setup()
{
pinMode(fog_light_low_beam,OUTPUT);
pinMode(fog_light_low_beam_indicator,OUTPUT);
pinMode(fog_light_high_beam,OUTPUT);
pinMode(fog_light_high_beam_indicator,OUTPUT);

digitalWrite(fog_light_high_beam,LEDoff);
digitalWrite(fog_light_high_beam_indicator,LEDoff);
digitalWrite(fog_light_low_beam,LEDon);
digitalWrite(fog_light_low_beam_indicator,LEDon);



// Setup the Serial port. see http://arduino.cc/en/Serial/IfSerial
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

/*
// link the button 1 functions.
button_1.attachClick(click1);
button_1.attachDoubleClick(doubleclick1);
button_1.attachLongPressStart(longPressStart1);
button_1.attachLongPressStop(longPressStop1);
button_1.attachDuringLongPress(longPress1);
*/

// link the blink_the_fog_lights functions.
//blink_the_fog_lights.attachClick(click);
//blink_the_fog_lights.attachLongPressStart(longPressStart);
blink_the_fog_lights.attachDuringLongPress(longPress);
blink_the_fog_lights.attachLongPressStop(stop_blinking_the_fog_lights);

// link the switch_turn_on_the_high_beam_fog_lights functions.
switch_turn_on_the_high_beam_fog_lights.attachClick(click_2);


//set  push buttons debounce
switch_turn_on_the_high_beam_fog_lights.setDebounceTicks(debounceDelay);
blink_the_fog_lights.setDebounceTicks(debounceDelay);
} // setup

// main code here, to run repeatedly:
void loop() 
{

currentMillis = millis();
// keep watching the push buttons:
//button_1.tick();

// keep watching the push buttons:
blink_the_fog_lights.tick();
switch_turn_on_the_high_beam_fog_lights.tick();


//check the flag here set in blink_the_fog_lights. void longPress() & blink_the_fog_lights. void longPressStart() 
if (ok_blink_the_fog_lights)
{
blink_the_fog_lights_now();
}


//check the flag here set in click_2 - switch_turn_on_the_high_beam_fog_lights.attachClick(click_2); 
if (the_high_beam_fog_lights_are_on)
{

//high_beam_pin_flag = ENABLED;
digitalWrite(fog_light_high_beam, LEDon);
digitalWrite(fog_light_high_beam_indicator,LEDon);

}
else
{
//high_beam_pin_flag = DISABLED;
digitalWrite(fog_light_high_beam, LEDoff);
digitalWrite(fog_light_high_beam_indicator,LEDoff);
}

// You can implement other code in here

}  // loop



// ----- BEGIN button_1 callback functions
/*
// This function will be called when the button_1 was pressed 1 time (and no 2. button press followed).
void click1() 
{
Serial.println("Button 1 click.");


}  // click1

// This function will be called when the button_1 was pressed 2 times in a short timeframe.
void doubleclick1()
{
Serial.println("Button 1 doubleclick.");
}  // doubleclick1

// This function will be called once, when the button_1 is pressed for a long time.
void longPressStart1() 
{
Serial.println("Button_1 longPress start");
}  // longPressStart1

// This function will be called often, while the button_1 is pressed for a long time.
void longPress1() 
{
Serial.println("Button 1 longPress...");


}  // longPress1

// This function will be called once, when the button_1 is released after beeing pressed for a long time.
void longPressStop1() 
{
Serial.println("Button 1 longPress stop");
}  // longPressStop1
*/


//===========================================================================================================
//my add edit
/*
// This function will be called when the button_1 was pressed 1 time (and no 2. button press followed).
void click() 
{


Serial.println("Button 1 click.");
if (!the_high_beam_fog_lights_are_on)
{
the_high_beam_fog_lights_are_on = ENABLED; 
}
else
{
the_high_beam_fog_lights_are_on = DISABLED; 
}  

}  // click
*/  

void click_2() 
{

//
Serial.println("switch_turn_on_the_high_beam_fog_lights click_2.");
if (!the_high_beam_fog_lights_are_on)
{
the_high_beam_fog_lights_are_on = ENABLED;
Serial.println(the_high_beam_fog_lights_are_on); 
Serial.println("the_high_beam_fog_lights_are_ON");
}
else
{
the_high_beam_fog_lights_are_on = DISABLED;
Serial.println(the_high_beam_fog_lights_are_on); 
Serial.println("the_high_beam_fog_lights_are_OFF");
}  

}  // click_2

// This function will be called often, while the button_1 is pressed for a long time.
void longPress() 
{
if (!ok_blink_the_fog_lights)
{
ok_blink_the_fog_lights = ENABLED;

}

}  // longPress


// This function will be called once, when the button_1 is pressed for a long time.
void longPressStart() 
{

}

// This function will be called once, when the "blink_the_fog_lights button" is "released" after beeing pressed for a long time.
void stop_blinking_the_fog_lights() 
{
//blink_the_fog_lights button is "up" "not ptessed"
ok_blink_the_fog_lights = DISABLED;
high_beam_pin_flag = DISABLED;

//the low beam fog lights are on by default
digitalWrite(fog_light_low_beam, LEDon);
digitalWrite(fog_light_low_beam_indicator,LEDon);
Serial.println("blink_the_fog_lights button is STOPPING blinking the fog lights now");

}//stop_blinking_the_fog_lights


void blink_the_fog_lights_now()
{
Serial.println("blink_the_fog_lights_now");
if (currentMillis - previousMillis >= blinkInterval) 
{   // enough time passed yet?
previousMillis = currentMillis; // sets the time we wait "from"


//Serial.println("the_high_beam_fog_lights_are_on");
//Serial.println(the_high_beam_fog_lights_are_on);
//Serial.println("blink_the_fog_lights button is blinking the fog lights now");
//Serial.println("blinking low beams and high beams");

if (the_high_beam_fog_lights_are_on)//high beams are on blink all the fog lights
{
    Serial.println("blink_the_fog_lights_now");
    Serial.println("the_high_beam_fog_lights_are_ON");
    Serial.println(the_high_beam_fog_lights_are_on);    
    digitalWrite(fog_light_low_beam,!digitalRead(fog_light_low_beam)); // shortcut to toggle the LED
    digitalWrite(fog_light_low_beam_indicator,!digitalRead(fog_light_low_beam_indicator)); // shortcut to toggle the LED 
    digitalWrite(fog_light_high_beam,!digitalRead(fog_light_high_beam)); // shortcut to toggle the LED
    digitalWrite(fog_light_high_beam_indicator,!digitalRead(fog_light_high_beam_indicator)); // shortcut to toggle the LED  
      
 }
 else//blink just the low beams fog lights are the only ones om
 {
    Serial.println("blink_the_fog_lights_now");
    Serial.println("the_high_beam_fog_lights_are_OFF");
    Serial.println(the_high_beam_fog_lights_are_on);    
    digitalWrite(fog_light_low_beam,!digitalRead(fog_light_low_beam)); // shortcut to toggle the LED
    digitalWrite(fog_light_low_beam_indicator,!digitalRead(fog_light_low_beam_indicator)); // shortcut to toggle the LED 
 }
}

}//end blink_the_fog_lights_now()

// // ----- END button 1 callback functions

Your buttons are reacting slowly because you are setting the debounce time to 100 ms which is a very long time. If you don't set it at all, it defaults to 10ms.

Your code seems a bit overly complicated. Rather than having long presses, this code will just toggle through different states: OFF, LOW, HIGH, BOTH for each press of button 5 (on_off). Independently of that, button 4 (blink) will toggle blinking on/off.

#include "OneButton.h"

// Setup a new OneButton push buttons on pin 4 and 5
OneButton blink_button(4, true); //original
OneButton on_off_button(5, true);


//define
#define DISABLED  false
#define ENABLED  true
#define LEDon  HIGH
#define LEDoff  LOW


unsigned long currentMillis;
unsigned long previousMillis;
const unsigned long blinkInterval = 250;

//LED's
const int fog_light_low_beam = 6;
const int fog_light_low_beam_indicator = 7;
const int fog_light_high_beam = 8;
const int fog_light_high_beam_indicator = 9;

//bool
bool blinking = DISABLED;

enum { LIGHTS_OFF, LIGHTS_LOW_BEAM, LIGHTS_HIGH_BEAM, LIGHTS_BOTH };
int lightState;

//END Declarations

void setup()
{
  pinMode(fog_light_low_beam, OUTPUT);
  pinMode(fog_light_low_beam_indicator, OUTPUT);
  pinMode(fog_light_high_beam, OUTPUT);
  pinMode(fog_light_high_beam_indicator, OUTPUT);

  digitalWrite(fog_light_high_beam, LEDoff);
  digitalWrite(fog_light_high_beam_indicator, LEDoff);
  digitalWrite(fog_light_low_beam, LEDoff);
  digitalWrite(fog_light_low_beam_indicator, LEDoff);
  lightState = LIGHTS_OFF;

  // Setup the Serial port. see http://arduino.cc/en/Serial/IfSerial
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  blink_button.attachClick(blink_click);
  on_off_button.attachClick(on_off_click);
}


void loop()
{
  currentMillis = millis();

  // keep watching the push buttons:
  blink_button.tick();
  on_off_button.tick();

  if ( blinking ) {
    blink_the_fog_lights();
  }
}


void on_off_click()
{
  Serial.println("on_off_button click.");
  switch ( lightState ) {
    case LIGHTS_OFF:  // all lights are off so turn on low beam fog lights
      digitalWrite(fog_light_high_beam, LEDoff);
      digitalWrite(fog_light_high_beam_indicator, LEDoff);
      digitalWrite(fog_light_low_beam, LEDon);
      digitalWrite(fog_light_low_beam_indicator, LEDon);
      lightState = LIGHTS_LOW_BEAM;
      Serial.println("low ON");
      break;

    case LIGHTS_LOW_BEAM: // low beam fog lights are on so switch to just high beam fog lights
      digitalWrite(fog_light_high_beam, LEDon);
      digitalWrite(fog_light_high_beam_indicator, LEDon);
      digitalWrite(fog_light_low_beam, LEDoff);
      digitalWrite(fog_light_low_beam_indicator, LEDoff);
      lightState = LIGHTS_HIGH_BEAM;
      Serial.println("high ON");
      break;

    case LIGHTS_HIGH_BEAM: // high beam fog lights are on so switch to both on
      digitalWrite(fog_light_high_beam, LEDon);
      digitalWrite(fog_light_high_beam_indicator, LEDon);
      digitalWrite(fog_light_low_beam, LEDon);
      digitalWrite(fog_light_low_beam_indicator, LEDon);
      lightState = LIGHTS_BOTH;
      Serial.println("low and high ON");
      break;

    case LIGHTS_BOTH: // both low and high beam fog lights are on so switch everthing off
      digitalWrite(fog_light_high_beam, LEDoff);
      digitalWrite(fog_light_high_beam_indicator, LEDoff);
      digitalWrite(fog_light_low_beam, LEDoff);
      digitalWrite(fog_light_low_beam_indicator, LEDoff);
      lightState = LIGHTS_OFF;
      Serial.println("all OFF");
      break;
  }
}


void blink_click()
{
  blinking = !blinking;

  // if we are done blinking, make sure we leave the lights on
  if ( blinking == DISABLED )
  {
    if ( lightState == LIGHTS_HIGH_BEAM || lightState == LIGHTS_BOTH )
    {
      digitalWrite(fog_light_high_beam, LEDon);
      digitalWrite(fog_light_high_beam_indicator, LEDon);
    }

    if ( lightState == LIGHTS_LOW_BEAM || lightState == LIGHTS_BOTH )
    {
      // toggle low beams
      digitalWrite(fog_light_low_beam, LEDon);
      digitalWrite(fog_light_low_beam_indicator, LEDon);
    }
  }
}


void blink_the_fog_lights()
{
  if ( lightState == LIGHTS_OFF ) {
    // lights are off so nothing to do
    return;
  }

  if (currentMillis - previousMillis >= blinkInterval)
  {
    // enough time passed yet?
    previousMillis = currentMillis; // sets the time we wait "from"
    if ( lightState == LIGHTS_HIGH_BEAM || lightState == LIGHTS_BOTH )
    {
      // toggle high beams
      digitalWrite(fog_light_high_beam, !digitalRead(fog_light_high_beam)); // shortcut to toggle the LED
      digitalWrite(fog_light_high_beam_indicator, !digitalRead(fog_light_high_beam_indicator)); // shortcut to toggle the LED
    }

    if ( lightState == LIGHTS_LOW_BEAM || lightState == LIGHTS_BOTH )
    {
      // toggle low beams
      digitalWrite(fog_light_low_beam, !digitalRead(fog_light_low_beam)); // shortcut to toggle the LED
      digitalWrite(fog_light_low_beam_indicator, !digitalRead(fog_light_low_beam_indicator)); // shortcut to toggle the LED
    }
  }
}

You can also see it in action with this simulation: sketch.ino - Wokwi Arduino and ESP32 Simulator

Thank you,

But I just changed 100 ms to 10ms and I still have a 1-second delay before the led lights, on a long press blink, the low beams are LEDs 6 & 7.

Can you see why my high beams never blink? 8 & 9

Are you running the script?

I have to have button 4 blink the LED's while it is being held down, it is a momentary-on function switch
Everything works except I can't blink the high beams 8 & 9.

No. As I stated in my last response, your code is overly complicated. It would be far easier to step through different states for each buttton click. Did you try the code I posted?

My original code with no library at all, is way more complicated than using the onebutton library and my original code works!

I am just trying this library to shorten the code.

I will look at the code you suggested. I am sure my issue is a minor fix

I just looked at the script did you post that?

Did you write that script?