Next are the blinkers. I am building the sketch to handle one of them right now, and will simply copy+paste it and change the names to create the other side.
What I want to happen: When the turn signal toggle switch is turned on (HIGH), the associated turn signal should flash rapidly for half a second, then settle into a slower blink rate normally seen on vehicles. When the toggle switch is turned off (LOW), the turn signal light should go off and stay off.
What is happening: When the toggle switch goes HIGH, the blinker flashes rapidly for one half second, then settles into a normal blink rate. However, when the toggle switch goes LOW, the light either goes off completely or it stays half lit at half brightness. In operation there is no obvious pattern to determine whether the light will go off or stay half bright.
What I'm thinking: The half-brightness indicates to me that for some reason the sketch gets caught in a loop of turning the light on and off as fast as it can. I could be wrong though. The final instruction in the sketch dictates that if the state of the toggle switch is LOW, then the light must also be LOW. For some reason, that is not always happening.
Here is the sketch with the rapid-flash portion of the code removed. By process of elimination I found that that part of the code is not the culprit.
/*A sketch to control the turn indicator lights of a motorcycle,
including an inital attention grabbing rapid-blink sequence lasting one second followed by pulsing "flashes".
*/
const int RightSwitch = 7;
const int RightBlink = 10;
long RightSwitchMillis = 0; //Time stamp for when the switch last went to 1(HIGH). Initially set at 0(LOW)
int RightSwitchVal = 0; //Monitors the value of the Right Blinker switch, whether 1 or 0 (HIGH or LOW). Initially set at 0(LOW)
int previousRightSwitchVal = 0; //Monitors the value of the Right Blinker switch from last time it was checked. Initally set at 0(LOW)
int RightBlinkState = LOW; //State of the Right Blinker Light. Initally set to LOW(Off)
long FastFlashMillis = 0; //Time stamp for when rapid flash last went to 1(LOW), initially set at O(LOW)
long SlowFlashMillis = 0; //Time stamp for when slow flash last went to 1(LOW), initially set at O(LOW)
int i = 0; //variable used to count up and down for the blinker pulses.
long interval = 1000; // interval at which to blink (milliseconds)
void setup()
{
pinMode(RightBlink, OUTPUT);
pinMode(RightSwitch, INPUT);
}
void loop()
{
unsigned long currentMillis = millis(); //initiallizes the clock
RightSwitchVal = digitalRead(RightSwitch); //read and store brake switch status
if ((RightSwitchVal == HIGH) && (previousRightSwitchVal == LOW)) //compares current switch status to previous status
{
previousRightSwitchVal = RightSwitchVal; //sets the current status as the old status in preparation for the next check
RightSwitchMillis = currentMillis; //sets a time stamp at the current time
}
if ((RightSwitchVal == HIGH) && (currentMillis - RightSwitchMillis > interval/2) && (currentMillis - SlowFlashMillis > interval/10)) //checks if the Right Blinker switch is pressed and how long it has been pressed for
{
SlowFlashMillis = currentMillis; //if the Right Blinker switch is pressed on and has been on for more than half a second, set FastFlashMillis to the current time.
if (RightBlinkState == LOW)
RightBlinkState = HIGH;
else
RightBlinkState = LOW; // if the LED is off turn it on and vice-versa
}
digitalWrite(RightBlink, RightBlinkState); // set the Right Blinker to match Right Blink State
if (RightSwitchVal == LOW)
{
previousRightSwitchVal = RightSwitchVal; //makes sure the previousBrakeSwitch value is reset to off when the switch is in fact off.
}
{
if (RightSwitchVal == LOW)
digitalWrite (RightBlink, LOW); //Overall, if the Right Blinker switch is off, keep the Right Blinker off.
}
}
And just to be sure, here is the full sketch
/*A sketch to control the turn indicator lights of a motorcycle,
including an inital attention grabbing rapid-blink sequence lasting one second followed by pulsing "flashes".
*/
const int RightSwitch = 7;
const int RightBlink = 10;
long RightSwitchMillis = 0; //Time stamp for when the switch last went to 1(HIGH). Initially set at 0(LOW)
int RightSwitchVal = 0; //Monitors the value of the Right Blinker switch, whether 1 or 0 (HIGH or LOW). Initially set at 0(LOW)
int previousRightSwitchVal = 0; //Monitors the value of the Right Blinker switch from last time it was checked. Initally set at 0(LOW)
int RightBlinkState = LOW; //State of the Right Blinker Light. Initally set to LOW(Off)
long FastFlashMillis = 0; //Time stamp for when rapid flash last went to 1(LOW), initially set at O(LOW)
long SlowFlashMillis = 0; //Time stamp for when slow flash last went to 1(LOW), initially set at O(LOW)
int i = 0; //variable used to count up and down for the blinker pulses.
long interval = 1000; // interval at which to blink (milliseconds)
void setup()
{
pinMode(RightBlink, OUTPUT);
pinMode(RightSwitch, INPUT);
}
void loop()
{
unsigned long currentMillis = millis(); //initiallizes the clock
RightSwitchVal = digitalRead(RightSwitch); //read and store brake switch status
if ((RightSwitchVal == HIGH) && (previousRightSwitchVal == LOW)) //compares current switch status to previous status
{
previousRightSwitchVal = RightSwitchVal; //sets the current status as the old status in preparation for the next check
RightSwitchMillis = currentMillis; //sets a time stamp at the current time
}
if ((RightSwitchVal == HIGH) && (currentMillis - RightSwitchMillis > interval/2) && (currentMillis - SlowFlashMillis > interval/10)) //checks if the Right Blinker switch is pressed and how long it has been pressed for
{
SlowFlashMillis = currentMillis; //if the Right Blinker switch is pressed on and has been on for more than half a second, set FastFlashMillis to the current time.
if (RightBlinkState == LOW)
RightBlinkState = HIGH;
else
RightBlinkState = LOW; // if the LED is off turn it on and vice-versa
}
// set the LED with the ledState of the variable:
digitalWrite(RightBlink, RightBlinkState);
/*
for (i = 0; i < 255;i++){ //loop from 0 to 254(fade in)
analogWrite(RightBlink, i); //set the LED brightness
delay(10); //Wait 10ms because analogWrite is instantaneous and we would
//not see a change
}
delay(1000); //stay full bright for 1 sec
for (i = 255; i > 0; i--) { //loop from 255 to 1 (fade out)
analogWrite(RightBlink, i); //set the LED brightness
delay(10);
}
delay(1000); //stay full off for 1 sec
if((RightSwitchVal == HIGH) && (currentMillis - FastFlashMillis > interval/50) && (currentMillis - RightSwitchMillis < interval/2)) //if the Right Blink switch is HIGH but less than half a second has elapsed,
// this checks if the Right Blink switch is HIGH and more than one-fifteith
//of a second has elapsed since the last time previousMillis10 was reset.
{
FastFlashMillis = currentMillis; // saves current time
if (RightBlinkState == LOW)
RightBlinkState = HIGH;
else
RightBlinkState = LOW; //if more than one-fiftieth of a second has elapsed since the time stamp was reset, reverse the state of the Right Blinker.
// set the Right Blinker with the state of the variable:
digitalWrite(RightBlink, RightBlinkState);
}
*/
if (RightSwitchVal == LOW)
{
previousRightSwitchVal = RightSwitchVal; //makes sure the previousBrakeSwitch value is reset of off when the switch is in fact off.
}
{
if (RightSwitchVal == LOW)
digitalWrite (RightBlink, LOW); //Overall, if the Right Blinker switch is off, keep the Right Blinker off.
}
}
I do think that I am understanding and getting the hang of the timed events better now though