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