Pages: 1 2 3 [4]   Go Down
Author Topic: (Resolved) Continue running one part of code while still checking other inputs?  (Read 5582 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Quote
Is toggleFast() a standard term?

I just made those functions as you can see in the code. You can make your own functions to make the code more modular and easier to read.

I see where you added those functions into the code, but not how you defined them. How does the program know what that means and that it should be linked to the long interval? I see where they are mentioned again in "void loop", but that still doesn't define the term. I'm expecting to see something similar to "const int LeftSwitch = 2;" that links items, but I don't see where that is happening.

I'm still looking for a good reference manual for this stuff, can you suggest a book or website that teaches this? The Arduino Learning site is good, but I see nothing there that relates to your toggleFast trick or my problem here.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19118
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I see where you added those functions into the code, but not how you defined them.

I defined it thus:

Code:
void toggleFast ()
{
  if (!digitalRead (LeftSwitch))
    digitalWrite (LeftBlink, !digitalRead (LeftBlink));
  else
    digitalWrite (LeftBlink, LOW);
}  // end of toggleFast

To be honest, I'm not sure what you are asking me. If I had written:

Code:
int a = 42;

And you replied "I don't see how you defined a", I would be equally perplexed. I did it right there.

Quote
... can you suggest a book or website that teaches this?

Google "C tutorial" or "C++ tutorial".

There are many, many sites devoted to teaching you C. Find one whose style appeals to you.
Logged


0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, so I decided to come back to this after giving it a break to clear my head. Now I am attacking it again without all the ruts that I seemed to have been stuck in before. I read up more and learned a bit and tried my hand at implementing timed events; posted below is what I came up with. It is close, it is so very close (I think). The problem that I am running into is that my sketch isn't behaving the way I think I have it written.

What I want to happen: When the brake button (momentary switch) is pushed (goes HIGH), the brake light flashes rapidly for one second, then stays on until the brake button is released (goes LOW).

What happens: I press the brake button and it flashes rapidly for as long as the button is held down, up to ~32 seconds after the sketch started running/arduino was turned on. After ~32 seconds has elapsed since the sketch started, the light stays on steady when the switch is pushed without flashing first. It makes no difference if I hold down the button for the whole 32 seconds or not - my interactions with the button have no effect on the 32second phenomena.

Where is this 32 seconds coming from? I've trying looking for other people who have noticed a similar event, but without luck.
Why doesn't the light transition from rapid flash to steady on after one second(1000 millis)?

I've tried removing variables and lines of codes selectively to hunt down the culprit, but haven't had any luck tracing down the culprit there either. Any ideas? Thanks guys, and I hope you are all enjoying your Memorial Day activities!

Code:
/*A sketch to control the brake lights of a motorcycle,
including an inital attention grabbing rapid-blink sequence lasting one second before staying on steady.
*/

const int BrakeSwitch = 6;
const int BrakeLight = 9;

int BrakeSwitchMillis = 0; //Time stamp for when the switch last went to 1(HIGH). Initially set at 0(LOW)
int BrakeSwitchVal = 0; //Monitors the value of the Brake switch, whether 1 or 0 (HIGH or LOW). Initially set at 0(LOW)
int previousBrakeSwitchVal = 0; //Monitors the value of the Brake switch from last time it was checked. Initally set at 0(LOW)
int BrakeLightState = LOW; //Initial state of the Brake Light. Initally set to LOW(Off)
long previousMillis9 = 0; //Time stamp for when light last went to 1(LOW), initially set at O(LOW)

long interval = 1000; // interval at which to blink (milliseconds)

void setup()
{
  pinMode(BrakeLight, OUTPUT);
  pinMode(BrakeSwitch, INPUT); 
}

void loop()
{
  unsigned long currentMillis = millis(); //initiallizes the clock

  BrakeSwitchVal = digitalRead(BrakeSwitch); //read and store brake switch status
  if ((BrakeSwitchVal == HIGH) && (previousBrakeSwitchVal == LOW)) //compares current switch status to previous status
    previousBrakeSwitchVal = BrakeSwitchVal; //sets the current status as the old status in preparation for the next check
    BrakeSwitchMillis = currentMillis; //sets a time stamp at the current time
   
       if ((BrakeSwitchVal == HIGH) && (currentMillis - BrakeSwitchMillis > interval)) //checks if the brake switch is pressed and how long it has been pressed for
         {
         digitalWrite (BrakeLight, HIGH); //if the switch is being pressed on and it has been pressed for longer than one second, the brake light goes on.
         }
         
         
       else if((BrakeSwitchVal == HIGH) && (currentMillis - previousMillis9 > interval/50)) //if the brake switch is HIGH but less than a second has elapsed, this checks if the brake switch is HIGH
                                                                                             //and more than one-fifteith of a second has elapsed since the last time previousMillis9 was reset.
         {
        previousMillis9 = currentMillis; // saves current time
         
         if (BrakeLightState == LOW)
            BrakeLightState = HIGH;
          else
            BrakeLightState = LOW; //if more than one-fiftieth of a second has elapsed since the time stamp was reset, reverse the state of the brake light.

          // set the LED with the ledState of the variable:
          digitalWrite(BrakeLight, BrakeLightState);
          }
         
         {
     if (BrakeSwitchVal == LOW)
       digitalWrite (BrakeLight, LOW); //Overall, if the brake switch is off, keep the brake light off.     
         }
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26527
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
up to ~32 seconds
Big clue right there.
Probably about 32.767 (or so) seconds.
That tells me you've got an "int" where you should have a "long".
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19118
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

He's right. And as to why it ignores the switch:

Code:
int BrakeSwitchMillis = 0; //Time stamp for when the switch last went to 1(HIGH). Initially set at 0(LOW)

...

  BrakeSwitchVal = digitalRead(BrakeSwitch); //read and store brake switch status
  if ((BrakeSwitchVal == HIGH) && (previousBrakeSwitchVal == LOW)) //compares current switch status to previous status
    previousBrakeSwitchVal = BrakeSwitchVal; //sets the current status as the old status in preparation for the next check
  BrakeSwitchMillis = currentMillis; //sets a time stamp at the current time

That last line is done unconditionally (despite your indentation).  If it is supposed to be when you press the switch that is not what you are achieving.

You need the squiggly brackets:

Code:
BrakeSwitchVal = digitalRead(BrakeSwitch); //read and store brake switch status
  if ((BrakeSwitchVal == HIGH) && (previousBrakeSwitchVal == LOW)) //compares current switch status to previous status
    {
    previousBrakeSwitchVal = BrakeSwitchVal; //sets the current status as the old status in preparation for the next check
    BrakeSwitchMillis = currentMillis; //sets a time stamp at the current time
    }

Logged


0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you both for the quick replies! I actually realized that I used the wrong time frame shortly after posting. Actually, I finally understood the difference between the two rather than just having made a simple typo.

As for the squiggles, thanks for solidifying what slowely becoming clear; your confirmation makes it stick now smiley

I will makes those changes to my sketch, and then see if I can apply what I learned to making my turn signals behave similarly, but with a PWM pulsing blink effect.

Edit: I also had to add a line just before the end to make sure that the BrakeSwitchVal was reset to 0 after the button was released, otherwise the clock wouldnt reset and the initial flash wouldn't occur:   
  if (BrakeSwitchVal == LOW)
         {
           previousBrakeSwitchVal = BrakeSwitchVal; //makes sure the previousBrakeSwitch value is reset of off when the switch is in fact off.
         }



Stay tuned for the next exciting episode of The Adventures of a Coding n00b!
« Last Edit: May 29, 2011, 07:38:12 pm by loudboy » Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
/*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
Code:
/*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 smiley
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19118
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This line here:

 
Code:
// set the LED with the ledState of the variable:
  digitalWrite(RightBlink, RightBlinkState);

will have a 50% chance of turning it on, once it has been on before.

You probably want:

 
Code:
  // set the LED with the ledState of the variable:
  if (RightSwitchVal == HIGH)
    digitalWrite(RightBlink, RightBlinkState);


And I don't know about this:

Code:
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.     
  }

The second set of squiggly brackets don't do anything, and why not combine them anyway?

Code:
if (RightSwitchVal == LOW)
  {
  previousRightSwitchVal = RightSwitchVal; //makes sure the previousBrakeSwitch value is reset of off when the switch is in fact off.
  digitalWrite (RightBlink, LOW); //Overall, if the Right Blinker switch is off, keep the Right Blinker off.     
  }
Logged


0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh, thank you, I see that now. I thought that part falls under the umbrella of the initial condition...

Code:
if ((RightSwitchVal == HIGH) && (currentMillis - RightSwitchMillis > interval/2) && (currentMillis - SlowFlashMillis > interval/10))

...but I see now how it is all separated. How would I include the secondary if's and operations under the control of that initial condition? I have it formatted with indentation the way I thought it would have work, but obviously that isn't happening. I'll read up on how to do that.

 Onward!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19118
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You probably want to nest your ifs. Try  some books on C, it might make it clearer.

For example:

Code:
if (RightSwitchVal == HIGH)
  {

  // nested condition
  if (previousRightSwitchVal == LOW)
    {

    }  // end of switch high but was low before

  }  // end of RightSwitchVal == HIGH
else

  // must be low then
  {

  // what to do if switch is low

  } // end of RightSwitchVal == LOW
Logged


0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Finished! Done! It works! Thank you so much Nick Gammon for all of your help and support, I really appreciate that.

Here is half the code (it is too long for one posting  smiley-razz) It ain't pretty but it works, and I look forward to learning how to make it purtyier  smiley-cool

Code:
/*A sketch to control the indicator lights of a motorcycle,
including an inital attention grabbing rapid-blink sequence lasting one half second followed by pulsing "flashes"
or the brake light holding full bright on.
This is my first "major", functional, purposful sketch. It can proably be slimmed down significantly,
and I look forward to learning the tricks that will let me do that.
I could not have done it without the patient guidance of Nick Gammon and the help AWOL, CrossRoads, and many others.
Thank you.

-Anthony (Loudboy)
*/


const int BrakeSwitch = 6;
const int BrakeLight = 9;

const int RightSwitch = 7;
const int RightBlink = 10;

const int LeftSwitch = 8;
const int LeftBlink = 11;

long BrakeSwitchMillis = 0; //Time stamp for when the switch last went to 1(HIGH). Initially set at 0(LOW)
int BrakeSwitchVal = 0; //Monitors the value of the Brake switch, whether 1 or 0 (HIGH or LOW). Initially set at 0(LOW)
int previousBrakeSwitchVal = 0; //Monitors the value of the Brake switch from last time it was checked. Initally set at 0(LOW)
int BrakeLightState = LOW; //State of the Brake Light. Initally set to LOW(Off)
long previousMillis9 = 0; //Time stamp for when light last went to 1(LOW), initially set at O(LOW)

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 RightFlashMillis = 0; //Time stamp for when rapid flash last went to 1(LOW), initially set at O(LOW)
long RightPulseMillis = 0; //Time stamp for when the Right Blinker value i was last adjusted
int R = 0; //variable used to count up and down for the Right Blinker pulses.
int RightPulse = LOW; //variable used to determine Fade In or Fade Out. HIGH means i=225 and going down, LOW means i=0 and going up.

long LeftSwitchMillis = 0;
int LeftSwitchVal = 0;
int previousLeftSwitchVal = 0;
int LeftBlinkState = LOW;
long LeftFlashMillis = 0;
long LeftPulseMillis = 0;
int L = 0;
int LeftPulse = LOW;


long interval = 1000; // interval at which to blink (milliseconds)

void setup()
{
  pinMode(BrakeLight, OUTPUT);
  pinMode(BrakeSwitch, INPUT);
  
  pinMode(RightBlink, OUTPUT);
  pinMode(RightSwitch, INPUT);
  
  pinMode(LeftBlink, OUTPUT);
  pinMode(LeftSwitch, INPUT);
}

void loop()
{
  unsigned long currentMillis = millis(); //initiallizes the clock
  
//Brake Light Code////////////////////////////////////////////////////////////////////////////////////////
BrakeSwitchVal = digitalRead(BrakeSwitch); //read and store brake switch status
  if ((BrakeSwitchVal == HIGH) && (previousBrakeSwitchVal == LOW)) //compares current switch status to previous status
   {
    previousBrakeSwitchVal = BrakeSwitchVal; //sets the current status as the old status in preparation for the next check
    BrakeSwitchMillis = currentMillis; //sets a time stamp at the current time
   }
       if ((BrakeSwitchVal == HIGH) && (currentMillis - BrakeSwitchMillis > interval/2)) //checks if the brake switch is pressed and how long it has been pressed for
         {
         digitalWrite (BrakeLight, HIGH); //if the switch is being pressed on and it has been pressed for longer than one half second, the brake light goes on full and steady.
         }
        
        
       else if((BrakeSwitchVal == HIGH) && (currentMillis - previousMillis9 > interval/50)) //if the above conditions are true, this checks if the brake switch is HIGH
                                                                                             //and more than one-fifteith of a second has elapsed since the last time previousMillis9 was reset.
         {
        previousMillis9 = currentMillis; // saves current time
        
         if (BrakeLightState == LOW)
            BrakeLightState = HIGH;
          else
            BrakeLightState = LOW; //if more than one-fiftieth of a second has elapsed since the time stamp was reset, reverse the state of the brake light.

          
          digitalWrite(BrakeLight, BrakeLightState); // set the LED with the ledState of the variable:
          }
     if (BrakeSwitchVal == LOW)
         {
           previousBrakeSwitchVal = BrakeSwitchVal; //makes sure the previousBrakeSwitch value is reset of off when the switch is in fact off.
           digitalWrite (BrakeLight, LOW); //Overall, if the brake switch is off, keep the brake light off.  
         }
        
Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And here is the second half of the above code. This gets pasted directly under the last part.

Code:
//Right Blinker Code/////////////////////////////////////////////////////////////////////////////////////////////////////////
  RightSwitchVal = digitalRead(RightSwitch); //read and store Right Blinker 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) && (RightPulse == LOW) && (currentMillis - RightPulseMillis > interval/10000)) //checks if the Right Blinker switch is pressed and how long it has been pressed for,
        {                                                                                                                                                        // if the Pulse was last set LOW, and if it is time for another step in brightness.
         RightPulseMillis = currentMillis; //Time stamp used to check how long since the brightness, i, was last changed
         R++; //Adds 1 to R value. The value of R dictates light brightness
         RightBlinkState = R; //Sets the light brightness to the value of i.
         analogWrite(RightBlink, RightBlinkState); //Sets the blinker brightness to the dictates level. Why I cannot go directly use (RightBlink, R), I do not know...
         }

 
 
   if (R == 255) //checks value of R
    {
     RightPulse = HIGH; //if R is maxed out at 255, the RightPulse goes HIGH. This lets R decrease.
    }


   
   if ((RightSwitchVal == HIGH) && (currentMillis - RightSwitchMillis > interval/2) && (RightPulse == HIGH) && (currentMillis - RightPulseMillis > interval/10000)) //checks if the Right Blinker switch is pressed and how long it has been pressed for,
        {                                                                                                                                                           // if the Pulse was last set HIGH, and if it is time for another step in brightness.
         RightPulseMillis = currentMillis; //if the Right Blinker switch is pressed on and has been on for more than half a second, set a time stamp for the fade in.
         R--; //Subtracts 1 from R value
         RightBlinkState = R; //Sets the value of light brightness to the value of R
         analogWrite(RightBlink, RightBlinkState); //set the blinker brightness to the value of RightBlinkState
          }
         
   if ((R == 0) && (currentMillis - RightSwitchMillis > interval/2)) // Checks if R is at 0 so that the brightening cycle can repeate. Also checks if the initial rapid flash sequence is over.
    {
      RightPulse = LOW; //if the above conditions are met, reset RightPulse to LOW.
    }


    if((RightSwitchVal == HIGH) && (currentMillis - RightFlashMillis > 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
        RightFlashMillis = currentMillis; // saves current time                                                                             //of a second has elapsed since the last time previousMillis10 was reset.
         
         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.
           
          digitalWrite(RightBlink, RightBlinkState); // set the Right Blinker with the state of the variable:
          }
     
         
    if (RightSwitchVal == LOW) //checks if the Blinker switch is still on or not. If it is off, the following occurs...
         {
           previousRightSwitchVal = RightSwitchVal; //makes sure the previousBrakeSwitch value is reset to off when the switch is in fact off.
           RightBlinkState = LOW; //resets the blinker
           digitalWrite(RightBlink, RightBlinkState); //Overall, if the Right Blinker switch is off, keep the Right Blinker off.
           R = 0; //resets R in preparation for the next usage.
         }
         
 //Left Blinker Code. Identical to Right Blinker Code.//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  LeftSwitchVal = digitalRead(LeftSwitch);
 
  if ((LeftSwitchVal == HIGH) && (previousLeftSwitchVal == LOW))
    {
     previousLeftSwitchVal = LeftSwitchVal;
     LeftSwitchMillis = currentMillis;
    }

   if ((LeftSwitchVal == HIGH) && (currentMillis - LeftSwitchMillis > interval/2) && (LeftPulse == LOW) && (currentMillis - LeftPulseMillis > interval/10000))
        {
         LeftPulseMillis = currentMillis;
         L++;
         LeftBlinkState = L;
         analogWrite(LeftBlink, LeftBlinkState);
         }

 
 
   if (L == 255)
    {
     LeftPulse = HIGH;
    }


   
   if ((LeftSwitchVal == HIGH) && (currentMillis - LeftSwitchMillis > interval/2) && (LeftPulse == HIGH) && (currentMillis - LeftPulseMillis > interval/10000))
        {
         LeftPulseMillis = currentMillis;
         L--;
         LeftBlinkState = L;
         analogWrite(LeftBlink, LeftBlinkState);
          }
         
   if ((L == 0) && (currentMillis - LeftSwitchMillis > interval/2))
    {
      LeftPulse = LOW;
    }


    if((LeftSwitchVal == HIGH) && (currentMillis - LeftFlashMillis > interval/50) && (currentMillis - LeftSwitchMillis < interval/2))                                                                                                                                 
         {
        LeftFlashMillis = currentMillis;
         
         if (LeftBlinkState == LOW)
            LeftBlinkState = HIGH;
          else
            LeftBlinkState = LOW;
           
          digitalWrite(LeftBlink, LeftBlinkState);
          }
     
         
    if (LeftSwitchVal == LOW)
         {
           previousLeftSwitchVal = LeftSwitchVal;
           LeftBlinkState = LOW;
           digitalWrite(LeftBlink, LeftBlinkState);
           L = 0;
         }
         
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there!
Just found this post, I really like your sketch, I hope that I can use it as a core for what I am trying to do:

please go to www.roopod.com to see what I would use this for!

I would have to modify it to use momentary switches to toggle on the turn signals, and toggle them off.  Does anyone have ideas how to do this...I am very new to coding...so glad I found someone's solution and don't have to suffer through all that work!

-roo trimble
Logged

Pages: 1 2 3 [4]   Go Up
Jump to: