Need help with bike turn signal project

Hello all

I’m working on a bike turn and stop signal project, however need some help with a piece of the code. (Quick FYI: I have a dual-sided switch that is momentary that I will be using)

I want it to function so that when I flip the switch once, it begins the blinking for the turn signal, and when the switch is flipped again the blinking stops.

My initial idea for this was to have a Boolean that would toggle every time the switch is pressed, however can’t seem to get that to work.

My current (working) code is this:

int d=2; //Varible used to store the digitalRead result
void setup()
{pinMode(2,INPUT); // Pin that the switch is connected to
pinMode(13,OUTPUT); // LED Pin
}
void loop()
{
d=digitalRead(2);
if(d==0)
{digitalWrite(13,HIGH); // Blinking Sequence
delay(400);
digitalWrite(13,LOW);
delay(400);}
else
{digitalWrite(13,LOW);}
}

My non-working code is this:

int d=2; //Varible used to store the digitalRead result
bool Blink_S = false; //Boolean used to see when the LEDs are blinking
void setup()
{pinMode(2,INPUT); // Pin that the switch is connected to
pinMode(13,OUTPUT); // LED Pin
}
void loop()
{
d=digitalRead(2);
if(d==0)
{Blink_S = !Blink_S;}
if (Blink_S == true){
digitalWrite(13,HIGH); // Blinking Sequence
delay(400);//          //       |         
digitalWrite(13,LOW);  //       |    
delay(400);}           //       |
else
{digitalWrite(13,LOW);}
}

Right now, the light never stops blinking.

Any suggestions as to how to make this work?

Thanks in advance

(PS: Please excuse any rookie mistakes, I haven’t coded Arduino in a while and have forgotten a lot. Apologies in advance for this)

You CERTAINLY need to de-bounce the switch in order to get your logic to work properly.

Paul

TechMaster04:
Hello all

I'm working on a bike turn and stop signal project, however need some help with a piece of the code. (Quick FYI: I have a dual-sided switch that is momentary that I will be using)

I want it to function so that when I flip the switch once, it begins the blinking for the turn signal, and when the switch is flipped again the blinking stops.

My initial idea for this was to have a Boolean that would toggle every time the switch is pressed, however can't seem to get that to work.

My current (working) code is this:

int d=2; //Varible used to store the digitalRead result

void setup()
{pinMode(2,INPUT); // Pin that the switch is connected to
pinMode(13,OUTPUT); // LED Pin
}
void loop()
{
d=digitalRead(2);
if(d==0)
{digitalWrite(13,HIGH); // Blinking Sequence
delay(400);
digitalWrite(13,LOW);
delay(400);}
else
{digitalWrite(13,LOW);}
}




My non-working code is this: 



int d=2; //Varible used to store the digitalRead result
bool Blink_S = false; //Boolean used to see when the LEDs are blinking
void setup()
{pinMode(2,INPUT); // Pin that the switch is connected to
pinMode(13,OUTPUT); // LED Pin
}
void loop()
{
d=digitalRead(2);
if(d==0)
{Blink_S = !Blink_S;}
if (Blink_S == true){
digitalWrite(13,HIGH); // Blinking Sequence
delay(400);//          //      |       
digitalWrite(13,LOW);  //      |   
delay(400);}          //      |
else
{digitalWrite(13,LOW);}
}




Right now, the light never stops blinking.

Any suggestions as to how to make this work?

Thanks in advance

(PS: Please excuse any rookie mistakes, I haven't coded Arduino in a while and have forgotten a lot. Apologies in advance for this)

The only time your bool will change is if d==0; Print the value D and check that you are getting a 0 from the pin.

Ok so I did the Serial Monitor check as requested by Romonaga, and noticed that flipping the switch on the first time, everything went according to plan. However, once the "Turning off" sequence was triggered, by pressing the switch again, I got two 0's instead of the usual one zero.

TechMaster04:
Ok so I did the Serial Monitor check as requested by Romonaga, and noticed that flipping the switch on the first time, everything went according to plan. However, once the "Turning off" sequence was triggered, by pressing the switch again, I got two 0's instead of the usual one zero.

Perhaps that is why you need to debounce the switch contacts.

Paul