Using a Toggle Switch Turn pin On and off (USING ARDUINO UNO)

(I am Using ARDUINO UNO)

I am using a toggle Switch on Pin 2 and one on Pin 3 to Trigger pin 10 and 12

I want Pin 2 to control pin 10

SO when the Toggle Switch is Open on Pin 2 Pin 10 Will be off
then when the Toggle Switch in closed I want pin 10 to turn on.

and the Same for Pin 3 and 12.

This is a simple Turn signal circuit for a UTV which I am making street legal.

The problem is Pin 10 stay on continually.

I am using the following code where an I going wrong?

const int lftSW = 2; // Left Switch is on pin 2 of Arduino

const int rtSW = 3; // Right Switch is on pin 3 of Arduino

const int lftLED = 10; // Left LED Circuit is on pin 10 of Arduino

const int rtLED = 12; // Right LED Circuit is on Pin 12 of Arduino

void setup ()

{
pinMode (lftSW, INPUT);
pinMode (rtSW, INPUT);
pinMode (lftLED, OUTPUT);
pinMode (rtLED, OUTPUT);

}

void loop()

{
if (digitalRead(lftSW) == LOW)
{
TurnOn(lftLED);
}

if (digitalRead(rtSW) == LOW)
{
TurnOn(rtLED);
}
}
void TurnOn(int led)
{
for (int i=0;i<3;i++) // Flash Lights 3 times then delay
{
digitalWrite(led, HIGH);
delay(100);

digitalWrite(led, LOW);
delay(100);

}
delay(100);
}

ZachBike_V2.ino (723 Bytes)

The logic of your code with outputs set with
  if (digitalRead(lftSW) == LOW)looks like you have ground on one side of the toggle switch and the other side is connected to the input pins. Unless you are using an external pull up resistor to 5v, those pins are floating when the toggle switch is open and they can read anything.

I would recommend that you take advantage of the internal pullup resistors in the AT328 and use

pinMode (lftSW, INPUT_PULLUP);
pinMode (rtSW, INPUT_PULLUP);

Connect one side of the toggle switch to the input pin, and the other side to ground.