After the blinker has been held on for while when it turns off if there is a bit of contact bounce it will latch the blinker for 3 seconds. its not a show stopper but Id like to rectify it. In reality it may rarely occur as the blinker switch contacts are a bit more robust than my dodgey piece of wire. Ive been playing around with some techniques to debounce it but nothing is really working out.
It may be that I rewrite without using interrupts and poll the input but ill leave it like it is at the moment if you want to have a play here is what I have so far.
/* Implementation of 3 second "auto" blinkers for SAAB9-5 by SAABAus Sept 2017
* uses pins 2 and 3 as inputs untill required than change to outputs to hold the blinker on.
*/
int Blink_left = 2; // Left Blinker switch on pin 2
int Blink_right = 3; // Right Blinker Switch on Pin 3
int debounceTime = 200; // debounce lock out time
bool Blink_left_status = 0; // Store for the current status of the Left blinker
bool Blink_right_status = 0; // Store for the Current Status of the Right Blinker
unsigned long debounce = 0; //
unsigned long trigger = 0;
unsigned long Time_start = 0;
unsigned long OnTime = 3000; // Time blinkers stay on for in ms
void Right_Blink ()
{
if (millis()- debounce > debounceTime)
{
if (Blink_left_status == 1) //check to see if the left blinker is on
{
Blink_left_status = 0; // if it is turn it off
digitalWrite(Blink_left, LOW);
pinMode (Blink_left, INPUT);
}
else
{
Blink_right_status = 1; // if the left blinker wasnt on turn on the right blinker
pinMode (Blink_right, OUTPUT); // set the right pin to an output
digitalWrite (Blink_right, HIGH); // turn on the right blinker
Time_start = millis(); // Starts the 3 second timer
}
debounce = millis();
}
}
void Left_Blink ()
{
if (millis() - debounce > debounceTime)
{
if (Blink_right_status == 1) //check to see if the right blinker is on
{
Blink_right_status = 0; // if it is turn it off
digitalWrite(Blink_right, LOW);
pinMode(Blink_right, INPUT);
}
else
{
Blink_left_status = 1; // if the right blinker wasnt on turn on the left blinker
pinMode (Blink_left, OUTPUT); // set the left pin to an output
digitalWrite (Blink_left, HIGH); // turn on the left blinker
Time_start = millis(); // Starts the 3 second timer
}
debounce = millis();
}
}
void Blinker_reset() // checks to see which blinker is on and turns it off
{
if (Blink_right_status != 0)
{
Blink_right_status = 0; // Sets status to off
digitalWrite (Blink_right, LOW); // Turns off blinker
pinMode (Blink_right, INPUT); // Returns to listening to blinker switch
}
if (Blink_left_status !=0)
{
Blink_left_status = 0; // Sets status to off
digitalWrite (Blink_left, LOW); // Turns off blinker
pinMode (Blink_left, INPUT); // Returns to listening to blinker switch
}
}
void setup() {
// put your setup code here, to run once:
pinMode (13, OUTPUT);
attachInterrupt (digitalPinToInterrupt (Blink_left), Left_Blink, RISING); //when the left blinker switch goes high call the left blinker ISR
attachInterrupt (digitalPinToInterrupt (Blink_right), Right_Blink, RISING); //when the right blinker switch goes high call the right blinker ISR
}
void loop()
{
if (Blink_left_status == 1) //this little section is for working out how to deal with the bouncing switch on release after the blinker has been on for a while
{
digitalWrite(13, HIGH); // built in LED dipsplays the what the arduino thinks is the status the left hand blinker
}
else
{
digitalWrite(13, LOW);
}
if (millis() > (Time_start + OnTime)) { // if more than the alloted time has elapsed check to to see which blinke ris on and turn it off
Blinker_reset();
}
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.