Need help with bike turn signal

Hello all

I am working on a bike turn signal/brake signal/horn project. I currently have my code running so that I have to hold a button for the turn signal to keep flashing, however would like it to work so that one click turns on the signal and another turns it off.

I have no idea how to even begin coding this. Any help would be appreciated.

Thanks in advance

(If you want to see my current code, i'll put it right here:

int L=2; //Varible used to store the digitalRead result for the left turn
int R = 2; // Varible used to store the digitalRead result for the right turn
void setup()
{pinMode(2,INPUT); // Pin that the left button is connected to
pinMode(3,INPUT); // Right button
pinMode(13,OUTPUT); // Left LED Pin
pinMode(12,OUTPUT);
}
void loop()
{
L=digitalRead(2);
R=digitalRead(3);
if(L==0)
{digitalWrite(13,HIGH);// Blinking Sequence
delay(400);
digitalWrite(13,LOW);
delay(400);}
else if (R==0)
{digitalWrite(12,HIGH); // Blinking Sequence
delay(400);
digitalWrite(12,LOW);
delay(400);}
}
}

I won't attempt to write the code for you but you'll need another variable (or two), plus an "and" statement (&&).

That variable needs to "remember" if the signal is on or not. Then the logic is: If the button is pushed AND the signal is on, turn it off. If the button is pushed AND the signal is off, turn it on.

Note that you can't read a momentary button during the delay() so you'll probably want to to re-write your code using [u]millis()[/u].

If you take-out the existing delay() statements, you'll need to [u]debounce[/u]. If you don't debounce one button-push will be read as several button pushes an you'll get "random" results.

DVDdoug:
I won't attempt to write the code for you but you'll need another variable (or two), plus an "and" statement (&&).

That variable needs to "remember" if the signal is on or not. Then the logic is: If the button is pushed AND the signal is on, turn it off. If the button is pushed AND the signal is off, turn it on.

Note that you can't read a momentary button during the delay() so you'll probably want to to re-write your code using [u]millis()[/u].

If you take-out the existing delay() statements, you'll need to [u]debounce[/u]. If you don't debounce one button-push will be read as several button pushes an you'll get "random" results.

Ok I have 3 questions:

  1. How would one go about de-bouncing a button?
  2. The delays are there for the light to flash. How would I use the millis() command to achieve the same result
  3. Can this variable be a Boolean? And if so, how would one go about toggling the Boolean with a command?

I'll try to implement the changes and see if it works, but would still like to have those questions answered

TechMaster04:

  1. How would one go about de-bouncing a button?
  2. The delays are there for the light to flash. How would I use the millis() command to achieve the same result
  3. Can this variable be a Boolean? And if so, how would one go about toggling the Boolean with a command?

The answers can be found in the first five sketches in IDE -> file/examples/digital. A grasp of the concepts/techniques presented there will open up many possibilities.

Thank you all for your help. I ended up using a library called "Button.h" by Michael Adams (https://github.com/madleech/ButtonL) and used the code from one of the digital examples (Blink without delay). It worked.

I appreciate all of your help.

If you want to see my working code, here it is:

#include <Button.h>
int ledButtonLeft = LOW;
int ledStateLeft = LOW;
int ledStateRight = LOW;
int ledButtonRight = LOW;
unsigned long previousMillis = 0; 
const long interval = 400;
Button LeftTrn(2);
Button RightTrn(3);
void setup()
{LeftTrn.begin();
RightTrn.begin();
pinMode(13,OUTPUT); // Left LED Pin
pinMode(12,OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();

if (LeftTrn.pressed())
{ if (ledButtonLeft == LOW) {
      ledButtonLeft = HIGH;
    } else 
      ledButtonLeft = LOW;
 }
if (RightTrn.pressed())
{ if (ledButtonRight == LOW) {
      ledButtonRight = HIGH;
    } else 
      ledButtonRight = LOW;
}
if (ledButtonLeft == HIGH){
if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledStateLeft == LOW) {
      ledStateLeft = HIGH;
    } else {
      ledStateLeft = LOW;
    }
 digitalWrite(13, ledStateLeft);
}
}
else{
digitalWrite(13,LOW);
}
if (ledButtonRight == HIGH){
if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledStateRight == LOW) {
      ledStateRight = HIGH;
    } else {
      ledStateRight = LOW;
    }
 digitalWrite(12, ledStateRight);
}
}
else
digitalWrite(12,LOW);

}

 
/*else if (R==0)
{digitalWrite(12,HIGH); // Blinking Sequence
delay(400);
digitalWrite(12,LOW);
delay(400);}
*/

Thank you once again