Hello all,
I have this script working however there is 1 problem
PROBLEM:
LED pin 10 & led pin 11 seem to be blinking faster than LED pin 6 & led pin 7.
I can't figure out why. Please read on.
Here is what I have:
I have 2 buttons and 4 LEDs
Here is what I am trying to do:
I want to simulate fog lights that I can make blink with a "temporary on button" for my Harley Davidson, with either the low beam blinking or both the high beam and the low beam blinking.
The low beam fog lights are "always-on" LED pin 6 & led pin 7
Button 8 "temporary on" will blink LED pin 6 & led pin 7, the "low beam fog lights", when held down and stop blinking LED pin 6 & led pin 7 when released.
Button 5 a "toggle switch" will turn on LED pin 10 & led pin 11 the "high beam fog lights" when pushed and keep them on until it is pushed again.
Then when LED pin 10 & led pin 11, are ON and you press button 8, the "temporary on" all 4 led's 6 -7 10 -11 will blink.
When LED pin 10 & led pin 11, are "OFF" and you press button 8 the temporary on just LED pin 6 & led pin 7 will blink.
LED pin 6 & led pin 7 are blinking at 100ms this is what I want 100ms on all 4 LEDs
Please see if you can help me.
Thanks
//============================================================================================
//BEGIN:Declarations
//============================================================================================
//
#define LEDon HIGH
#define LEDoff LOW
#define PUSHED LOW
bool blink_the_high_bem_fog_lights = false;
int current_buttonState;
int current_button_press_reading = 0;
int Val = 0;
int previous_button_state_reading = LOW; // the previous reading from the input pin
int ledState = LOW;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 25; // the debounce time; increase if the output flickers
//
const unsigned long led_blink_speed = 100;
const unsigned long led_blink_speed_2 = 100;
byte button_blink_all_fog_lights = 8; //+5V----[Internal Pullup Resistor]----Input Pin ----[switch]----GND
byte button_turn_on_high_beam_fog_lights = 5;
bool onAndOff = false;
byte fog_light_halo_low_beam_indicator = 6;
byte fog_light_halo_low_beam = 7; //Pin Number----[>|]----[220 ohm]----GND
////
byte fog_light_high_beam_indicator = 10;
byte fog_light_high_beam = 11; //Pin Number----[>|]----[220 ohm]----GND
byte lastButton;
boolean LEDflag = false;
unsigned long blinkMillis;
unsigned long switchMillis;
unsigned long currentMillis;
///////
byte lastButton_2;
boolean LEDflag_2 = false;
unsigned long blinkMillis_2;
unsigned long switchMillis_2;
unsigned long currentMillis_2;
int lastButtonState;
int currentButtonState; // the current state of button
int trythis = 0;
//============================================================================================
//END:Declarations
//============================================================================================
//============================================================================================
//BEGIN:setup ()
//============================================================================================
void setup ()
{
Serial.begin(9600);
digitalWrite(fog_light_halo_low_beam, LOW );
pinMode(fog_light_halo_low_beam_indicator , OUTPUT);
pinMode(fog_light_halo_low_beam, OUTPUT);
//
pinMode(fog_light_high_beam_indicator , OUTPUT);
pinMode(fog_light_high_beam, OUTPUT);
//
pinMode(button_blink_all_fog_lights, INPUT_PULLUP);
pinMode(button_turn_on_high_beam_fog_lights, INPUT_PULLUP);
lastButton = digitalRead (button_blink_all_fog_lights);
digitalWrite(fog_light_halo_low_beam, LEDon);
digitalWrite(fog_light_halo_low_beam_indicator, LEDon);
//
lastButton_2 = digitalRead (button_turn_on_high_beam_fog_lights);
digitalWrite(fog_light_high_beam, LEDoff);
digitalWrite(fog_light_high_beam_indicator, LEDoff);
Serial.begin(9600);
} //END of setup()
//===========================================================================================
//END:setup ()
//===========================================================================================
//============================================================================================
//BEGIN:void loop ()
//===========================================================================================
void loop ()
{
currentMillis = millis();
currentMillis_2 = millis();
//===========================================================================================
//BEGIN:Do the work here
//===========================================================================================
//this is where you do the work of the temporary on button press
//************************
checkSwitches_1();
//************************
if (LEDflag == true && currentMillis - blinkMillis >= led_blink_speed)
{
//start the TIMER
blinkMillis = currentMillis;
//put your code here
//toggle LED
//Serial.println(Val);
//Serial.println(current_button_press_reading);
Serial.println("this is trythis");
Serial.println(trythis);
Serial.println("this is onAndoff");
Serial.println(onAndOff);
//trythis = digitalRead(fog_light_high_beam_indicator);
if (onAndOff == 0)
//if (trythis == 0)
{
//Serial.println(onAndOff);
//Serial.println("button pressed");
//trythis = digitalRead(fog_light_high_beam_indicator);
//Serial.println(trythis);
//Serial.println("the High beam fog light indicator is OFF");
// turn LED on:
//digitalWrite(ledPin, HIGH);
digitalWrite (fog_light_halo_low_beam, ! digitalRead (fog_light_halo_low_beam));
digitalWrite (fog_light_halo_low_beam_indicator, ! digitalRead (fog_light_halo_low_beam_indicator));
Serial.println("I am just blinking the low beams now");
}
if (onAndOff == 1)
//if (trythis == 1)
{
//Serial.println(onAndOff);
//Serial.println("button pressed");
//trythis = digitalRead(fog_light_high_beam_indicator);
//Serial.println(trythis);
//Serial.println("the High beam fog light indicator is OFF");
// turn LED on:
//digitalWrite(ledPin, HIGH);
digitalWrite (fog_light_halo_low_beam, ! digitalRead (fog_light_halo_low_beam));
digitalWrite (fog_light_halo_low_beam_indicator, ! digitalRead (fog_light_halo_low_beam_indicator));
digitalWrite (fog_light_high_beam, ! digitalRead (fog_light_high_beam));
digitalWrite (fog_light_high_beam_indicator, ! digitalRead (fog_light_high_beam_indicator));
Serial.println("I am blinking the low beam and high beams too");
}
//===========================================================================================
//END:Do the work here
//===========================================================================================
} //end void loop
//============================================================================================
//END:void loop ()
//===========================================================================================
//this is where you do the work of the led stays on utill button pressed again button press
checkSwitches_2();
if (LEDflag_2 == true && currentMillis_2 - blinkMillis_2 >= led_blink_speed_2)
{
//start the TIMER
blinkMillis_2 = currentMillis_2;
//put your code here
//toggle LED
}
} //END of void loop()
//============================================================================================
//BEGIN:checkSwitches_1()
//============================================================================================
void checkSwitches_1()
{
if (currentMillis - switchMillis < 50) //debounce
{
//it is not time to check the switches
return;
}
//start the TIMER
switchMillis = currentMillis;
byte button = digitalRead (button_blink_all_fog_lights);
if (lastButton != button)
{
//save the new state of the switch
lastButton = button;
if (button == PUSHED) // button pressed
{
//enable flashing
LEDflag = true;
//digitalWrite(fog_light_high_beam, LEDon);
//digitalWrite(fog_light_high_beam_indicator, LEDon);
}
else //not pushed
{
//disable flashing
LEDflag = false;
digitalWrite(fog_light_halo_low_beam, LEDon);
digitalWrite(fog_light_halo_low_beam_indicator, LEDon);
}
}
} //END of checkSwitches_1()
//============================================================================================
//END:checkSwitches_1()
//============================================================================================
//============================================================================================
//BEGIN:checkSwitches_2()
//============================================================================================
void checkSwitches_2()
{
// put your main code here, to run repeatedly:
//get the value of current_button_press_reading
current_button_press_reading = digitalRead(button_turn_on_high_beam_fog_lights);
// If the switch changed, due to noise or pressing:
if (current_button_press_reading != previous_button_state_reading) {
// reset the debouncing timer
//NOTE the "+=" in - "lastDebounceTime += millis();"
//The reason for this is that millis() may accumulate "jitter", and the clock then drifts.
//Fine if you use only timer, not so great if you have other timers running
//and you want them all to remain in lock step.
lastDebounceTime += millis();
}//end Debounce
if ((millis() - lastDebounceTime) > debounceDelay)
{
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(button_turn_on_high_beam_fog_lights); // read new state
if (lastButtonState == HIGH && currentButtonState == LOW)
{
if (onAndOff == true)
onAndOff = false;
//Serial.println("The button is pressed");
//Serial.println(ledState );
onAndOff = ledState;
LEDflag = false;
//Serial.println(onAndOff);
// toggle state of LED
ledState = !ledState;
}
else
{ //button not pushed
onAndOff = false;
//LEDflag = true;
//Serial.println(lastButtonState);
//Serial.println(ledState );
onAndOff = ledState;
//Serial.println(onAndOff);
//delay(2000);
// control LED arccoding to the toggled state
digitalWrite(fog_light_high_beam, ledState);
digitalWrite(fog_light_high_beam_indicator, ledState);
}
}//end if millis
} //END of checkSwitches_2()
//============================================================================================
//END:checkSwitches_2()
//============================================================================================