Onebutton library how to turn a led on and off with a 2.5 second longpress

Hello All,
Using the onebutton library how do you turn OFF an led with a long-press button press, of 2.5 seconds and then turn ON the led with another long-press of 2.5 seconds using the same button.

Thanks for any help

Hello

What have you tried ?

You don't need a library to do that.

1 Like

Hi Blackfin,
I know that I am all set with doing that without a library, I just started playing with the onebutton library and was wondering how to do it with the library.

I was just hoping I could get a code snippet from someone.

I have tried attachDuringLongPress, attachLongPressStop I can turn off a led with a long press but I can't figure how to turn it back on with a long press. I did all this without library but just curious about doing it with onebutton library.
I just need a small code snippet from someone

I want button 5 to do the job on the long press

Here is my code using the library where I can blink the LED's with a long-press and stop blinking the less with a long-press stop.

I want to turn them all off and keep them off with a 2.5 second long press, then with another long-press 2.5 seconds turn them back on.



//Declarations
////blh64 Faraday https://wokwi.com/arduino/projects/323950857541386835
#include "OneButton.h"

// Setup a new OneButton push buttons on pin 4 and 5
OneButton button_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 currentMillis;
unsigned long previousMillis;
const unsigned long blinkInterval = 100;

//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;
bool just_blink_the_low_beams = DISABLED;
bool blink_both_the_low_beams_and_the_high_beams = DISABLED;
bool foglightsareoff = DISABLED;
//enum { LIGHTS_OFF, LIGHTS_LOW_BEAM, LIGHTS_HIGH_BEAM, LIGHTS_BOTH };//original
enum {LIGHTS_LOW_BEAM, LIGHTS_BOTH};//my add edit

//lightState works with enum
int lightState;

//END Declarations


//setup
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); //my edit or add
digitalWrite(fog_light_low_beam_indicator, LEDon);//my edit or add

//this lightstate fires off first
lightState = LIGHTS_BOTH;

// 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
}

//button_blink_the_fog_lights.attachClick(start_the_blink_short_click);//original
button_blink_the_fog_lights.attachDuringLongPress(start_the_blink_short_click);
button_blink_the_fog_lights.attachLongPressStop(stop_the_blinking);
//
switch_turn_on_the_high_beam_fog_lights.attachClick(turn_on_the_high_beam_fog_lights);
//switch_turn_on_the_high_beam_fog_lights.attachDuringLongPress(turn_off_all_the_fog_lights);



//switch_turn_on_the_high_beam_fog_lights.setPressTicks(2500);
//setClickTicks(int)
//bool isLongPressed()
//int getPressedTicks()


}//setup

//loop
void loop()
{
//don't forget to set the start time with millis()
currentMillis = millis();

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

if ( blinking ) {
  blink_the_fog_lights();
}


}//loop


void turn_on_the_high_beam_fog_lights()
{
 Serial.println("switch_turn_on_the_high_beam_fog_lights click.");
 switch (lightState) 
 {
    case LIGHTS_LOW_BEAM: // low beam fog lights are on so switch to just high beam fog lights
       //digitalWrite(fog_light_high_beam, LEDon);// original
       //digitalWrite(fog_light_high_beam_indicator, LEDon);// original
    
       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_BOTH;
       just_blink_the_low_beams = ENABLED;
       blink_both_the_low_beams_and_the_high_beams = DISABLED;
       Serial.println("high ON");
       break;
  
    case LIGHTS_BOTH: // both low and high beam fog lights are on so switch everthing off
       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_LOW_BEAM;
       just_blink_the_low_beams = DISABLED;
       blink_both_the_low_beams_and_the_high_beams = ENABLED;
       Serial.println("all OFF");
       break;
 }// switch (lightState
}//turn_on_the_high_beam_fog_lights


void start_the_blink_short_click()
{
 blinking = !blinking;

 if ( blinking == DISABLED )
 {
    blinking = ENABLED;
 }//if
}//start_the_blink_short_click


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 the value of "lightState" equals the value of "LIGHTS_HIGH_BEAM" "OR" the value of "lightState" equals the value of "LIGHTS_BOTH"
    if (blink_both_the_low_beams_and_the_high_beams)//all the fog lights are on
    {
       // 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
       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//just the low beams 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 
    }//if else
 }//if
}//blink_the_fog_lights


void stop_the_blinking()
{
  blinking = DISABLED;//stop the blinking
  //leave on or off the proper fog lights when the blinking stops
  if (blink_both_the_low_beams_and_the_high_beams)
  {
     digitalWrite(fog_light_low_beam, LEDon);
     digitalWrite(fog_light_low_beam_indicator, LEDon);
     digitalWrite(fog_light_high_beam, LEDon);
     digitalWrite(fog_light_high_beam_indicator, LEDon);
  }
  else
  {
     digitalWrite(fog_light_low_beam, LEDon);
     digitalWrite(fog_light_low_beam_indicator, LEDon);  
  }//if else
}//stop_the_blinking








I got the long-press working but why does millis() not work but delay(2500) does work?

here is the script

/*

// 01.03.2014 created by Matthias Hertel
// ... and working.

/* Sample output:

Starting TwoButtons...
Button 1 click.
Button 2 click.
Button 1 doubleclick.
Button 2 doubleclick.
Button 1 longPress start
Button 1 longPress...
Button 1 longPress...
Button 1 longPress...
Button 1 longPress stop
Button 2 longPress start
Button 2 longPress...
Button 2 longPress...
Button 2 longPress stop

*/

#include "OneButton.h"

// Setup a new OneButton on pin A1.
OneButton button1(5, true);
//define
#define DISABLED  false
#define ENABLED  true
#define LEDon  HIGH
#define LEDoff  LOW


unsigned long currentMillis;
unsigned long previousMillis;

int Long_Switch_Press_Time = 2500; //2.5 seconds

bool first_long_press_happened = DISABLED;


const int fog_light_low_beam = 6;
// setup code here, to run once:
void setup() 
{
// 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.
button1.attachClick(click1);
button1.attachDoubleClick(doubleclick1);
button1.attachLongPressStart(longPressStart1);
button1.attachLongPressStop(longPressStop1);
button1.attachDuringLongPress(longPress1);
button1.setDebounceTicks(50);
pinMode(fog_light_low_beam, OUTPUT);
digitalWrite(fog_light_low_beam, LEDon); //my edit or add

} // setup

// main code here, to run repeatedly:

void loop() {
// keep watching the push buttons:
button1.tick();
currentMillis = millis();
//timeStart_1 = millis();
// You can implement other code in here or just wait a while
delay(10);
} // loop

// ----- button 1 callback functions

// This function will be called when the button1 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 button1 was pressed 2 times in a short timeframe.
void doubleclick1() {
Serial.println("Button 1 doubleclick.");
} // doubleclick1

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

{
 
 
 //currentMillis = millis(); // better to store in variable, for less jitter
// if (currentMillis - previousMillis >= 5000) 
// {
// }

 delay(2500);
 Serial.println("Button 1 longPress start");
 // longPressStart1
 if (!first_long_press_happened)
 {
   
   first_long_press_happened = ENABLED;
  
 } 
 else if (first_long_press_happened)
 {
  first_long_press_happened = DISABLED; 
 }
 
}//longPressStart1


// This function will be called often, while the button1 is pressed for a long time.
void longPress1()
{
 Serial.println("Button 1 longPress...");
 if (first_long_press_happened)
 {
    digitalWrite(fog_light_low_beam, LEDoff); //my edit or add
    Serial.println("first_long_press_happened led is OFF");
    Serial.println(first_long_press_happened);
} 
else if (!first_long_press_happened)
{
   digitalWrite(fog_light_low_beam, LEDon); //my edit or add
   Serial.println("first_long_press_happened led is ON");
   Serial.println(first_long_press_happened);
}

} // longPress1





// This function will be called once, when the button1 is released after beeing pressed for a long time.
void longPressStop1() 
{
  

 Serial.println("Button 1 longPress stop");
}  // longPressStop1

//end

Now millis() wont work in a onebutton library function longPressStart1 but delay(2500); works

Here is an example : t961293.ino - Wokwi Arduino and ESP32 Simulator

I see it now switch_turn_on_the_high_beam_fog_lights.setPressTicks(2500); // a long press will be detected after 2500 ms thank you but now another problem

Also after I do a long press my button switch_turn_on_the_high_beam_fog_lights stays at "LOW" ,sometimes, and I have to press it twice to turn on an led, then it is fine, how can I set it to high after a long press?

SUMMARY:
Ok this script works, switch_turn_on_the_high_beam_fog_lights.setPressTicks(2500); // a long press will be detected after 2500 ms, lets me avoid using millis, or delay(2500); Now the only issue is after a long press to turn off all the fog lights, then another long press to turn them back on, the button 5 stays low sometimes, and I have to press it twice to toggle on the high beams from off to on. I have to find out how to set a button to high in the "onebutton library" If the high beams are on and then I do a long-press to turn off all the fog lights, and then I do another long-press to turn the fog lights back on, I then have to do 2 clicks to turn on the high beam fog light because the button is set at low.

Thanks



//Declarations
////blh64 Faraday https://wokwi.com/arduino/projects/323950857541386835
#include "OneButton.h"

// Setup a new OneButton push buttons on pin 4 and 5
OneButton button_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
int buttonState = 0;

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

//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;
bool just_blink_the_low_beams = DISABLED;
bool blink_both_the_low_beams_and_the_high_beams = DISABLED;
bool foglightsareoff = DISABLED;
bool the_first_time_long_press_happened = 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;


//enum { LIGHTS_OFF, LIGHTS_LOW_BEAM, LIGHTS_HIGH_BEAM, LIGHTS_BOTH };//original
enum {LIGHTS_LOW_BEAM, LIGHTS_BOTH};//my add edit

//lightState works with enum
int lightState;

//END Declarations


//setup
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); //my edit or add
digitalWrite(fog_light_low_beam_indicator, LEDon);//my edit or add

//this lightstate fires off first
lightState = LIGHTS_BOTH;

// 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
}

//button_blink_the_fog_lights.attachClick(start_the_blink_short_click);//original
button_blink_the_fog_lights.attachDuringLongPress(start_the_blink_short_click);
button_blink_the_fog_lights.attachLongPressStop(stop_the_blinking);
//
switch_turn_on_the_high_beam_fog_lights.attachClick(turn_on_the_high_beam_fog_lights);
switch_turn_on_the_high_beam_fog_lights.attachLongPressStart(longPressStart1);
switch_turn_on_the_high_beam_fog_lights.attachDuringLongPress(longPress1);
switch_turn_on_the_high_beam_fog_lights.setPressTicks(2500); // a long press will be detected after 2500 ms

//switch_turn_on_the_high_beam_fog_lights.setPressTicks(2500);
//setClickTicks(int)
//bool isLongPressed()
//int getPressedTicks()


}//setup

//loop
void loop()
{


//don't forget to set the start time with millis()
currentMillis = millis();

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

if ( blinking ) {
blink_the_fog_lights();
}


}//loop


void turn_on_the_high_beam_fog_lights()
{

if (!All_The_Fog_Lights_Are_Off)
{


Serial.println("switch_turn_on_the_high_beam_fog_lights click.");
switch (lightState) 
{
case LIGHTS_LOW_BEAM: // low beam fog lights are on so switch to just high beam fog lights
   //digitalWrite(fog_light_high_beam, LEDon);// original
   //digitalWrite(fog_light_high_beam_indicator, LEDon);// original

   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_BOTH;
   just_blink_the_low_beams = ENABLED;
   blink_both_the_low_beams_and_the_high_beams = DISABLED;
   Serial.println("high ON");
   break;

case LIGHTS_BOTH: // both low and high beam fog lights are on so switch everthing off
   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_LOW_BEAM;
   just_blink_the_low_beams = DISABLED;
   blink_both_the_low_beams_and_the_high_beams = ENABLED;
   Serial.println("all OFF");
   break;
}// switch (lightState
}
}//turn_on_the_high_beam_fog_lights


void start_the_blink_short_click()
{
blinking = !blinking;

if ( blinking == DISABLED )
{
blinking = ENABLED;
}//if
}//start_the_blink_short_click


void blink_the_fog_lights()
{
//if ( lightState == LIGHTS_OFF ) {
// lights are off so nothing to do
//  return;
//}
if (!All_The_Fog_Lights_Are_Off)
{
if (currentMillis - previousMillis >= blinkInterval)
{
   // enough time passed yet?
  previousMillis = currentMillis; // sets the time we wait "from"
  //if the value of "lightState" equals the value of "LIGHTS_HIGH_BEAM" "OR" the value of "lightState" equals the value of "LIGHTS_BOTH"
    if (blink_both_the_low_beams_and_the_high_beams)//all the fog lights are on
    {
       // 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
      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//just the low beams 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 
    }//if else
}//if
}//All_The_Fog_Lights_Are_Off

}//blink_the_fog_lights



void stop_the_blinking()
{

if (!All_The_Fog_Lights_Are_Off)
{
blinking = DISABLED;//stop the blinking
//leave on or off the proper fog lights when the blinking stops
if (blink_both_the_low_beams_and_the_high_beams)
{
 digitalWrite(fog_light_low_beam, LEDon);
 digitalWrite(fog_light_low_beam_indicator, LEDon);
 digitalWrite(fog_light_high_beam, LEDon);
 digitalWrite(fog_light_high_beam_indicator, LEDon);
}
else
{
 digitalWrite(fog_light_low_beam, LEDon);
 digitalWrite(fog_light_low_beam_indicator, LEDon);  
}//if else
}  
}//stop_the_blinking


void longPressStart1()
{

// longPressStart1
if (!the_first_time_long_press_happened)//the LED is on by default so turn it OFF in Button 1 longPress1
{
the_first_time_long_press_happened = ENABLED;// now make it true so now the LED will be turned OFF
} 
else if (the_first_time_long_press_happened)//the LED is off now so turn it ON in Button 1 longPress1
{
the_first_time_long_press_happened = DISABLED; //now make it false so now the LED will be turned ON
}
}  

void longPress1()
{
Serial.println("Button 1 longPress...");
if (the_first_time_long_press_happened)//if true turn OFF the LED
{

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);
All_The_Fog_Lights_Are_Off = ENABLED;

Serial.println("the_first_time_long_press_happened the LED was ON Now turn off the LED");
Serial.println("the_first_time_long_press_happened is now TRUE - 1");
Serial.println(the_first_time_long_press_happened);
} 
else if (!the_first_time_long_press_happened)//if false turn ON the LED
{
digitalWrite(fog_light_low_beam, LEDon); 
digitalWrite(fog_light_low_beam_indicator, LEDon);
digitalWrite(fog_light_high_beam, LEDoff);
digitalWrite(fog_light_high_beam_indicator, LEDoff);
All_The_Fog_Lights_Are_Off = DISABLED;
blink_both_the_low_beams_and_the_high_beams = DISABLED;
Serial.println("the_first_time_long_press_happened the LED was OFF Now turn ON the LED");
Serial.println("the_first_time_long_press_happened is now FALSE - 0");
Serial.println(the_first_time_long_press_happened);
}  
}



This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.