I have my Arduino and code set up to have a left and right turn signals and a brake light.
What works:
- Hold the Brake button to activate, release to deactivate
- Press left turn signal once to activate, press again to deactivate
- Press right turn signal once to activate, press again to deactivate
- If one turn signal is activated pressing the opposite one cancels the first and activates the second
What doesn't work:
- When either of the turn signals are activated they only blink once and then stay activated (they need to keep blinking until turned off)
- When I turn on the Arduino and start the code the default state of both turn signals is on (they need to start "off")
Do you mind looking at what I'm doing wrong?
My Code: (beware, I don't really know what I'm doing and I'm just reverse engineering bits of other code )
int ledPinRed = 5; // choose the pin for the LED
int inPinRed = 2; // choose the input pin (for a pushbutton)
int valRed = 0; // variable for reading the pin status
int inPinLeft = 3; // Left Signal button
int outPinLeft = 6; // Left Signal LED
int inPinRight = 4; // Right Signal button
int outPinRight = 7; // Right Signal LED
int stateLeft = HIGH; // Left current state of the output pin
int readingLeft; // Left current reading from the input pin
int previousLeft = LOW; // Left previous reading from the input pin
int stateRight = HIGH; // Right current state of the output pin
int readingRight; // Right current reading from the input pin
int previousRight = LOW; // Right previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup()
{
pinMode(inPinLeft, INPUT);
pinMode(outPinLeft, OUTPUT);
pinMode(inPinRight, INPUT);
pinMode(outPinRight, OUTPUT);
pinMode(ledPinRed, OUTPUT); // declare LED as output
pinMode(inPinRed, INPUT); // declare pushbutton as input
}
void loop()
{
valRed = digitalRead(inPinRed); // read input value
if (valRed == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPinRed, LOW); // turn LED OFF
} else {
digitalWrite(ledPinRed, HIGH); // turn LED ON
}
readingLeft = digitalRead(inPinLeft);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (readingLeft == HIGH && previousLeft == LOW && millis() - time > debounce) {
if (stateLeft == HIGH)
stateLeft = LOW;
else
stateLeft = HIGH,
stateRight = LOW;
digitalWrite(6, HIGH); // turn the LED on (HIGH is the voltage level)
delay(400); // wait for a second
digitalWrite(6, LOW); // turn the LED off by making the voltage LOW
delay(400); // wait for a second
time = millis();
}
digitalWrite(outPinLeft, stateLeft);
digitalWrite(outPinRight, stateRight);
previousLeft = readingLeft;
readingRight = digitalRead(inPinRight);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (readingRight == HIGH && previousRight == LOW && millis() - time > debounce) {
if (stateRight == HIGH)
stateRight = LOW;
else
stateRight = HIGH,
stateLeft = LOW;
digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level)
delay(400); // wait for a second
digitalWrite(7, LOW); // turn the LED off by making the voltage LOW
delay(400); // wait for a second
time = millis();
}
digitalWrite(outPinRight, stateRight);
digitalWrite(outPinLeft, stateLeft);
previousRight = readingRight;
}