Go Down

Topic: (Resolved) Continue running one part of code while still checking other inputs? (Read 5 times) previous topic - next topic

loudboy


need  to read the switch:

if ( (LeftBlinkState == LOW) &&  (digitalRead (LeftBlinkSwitch) == HIGH) )



Oh, duh. Thanks  :smiley-red:

loudboy


This works with two lights and two switches. If the switch is grounded (ie. pressed) then it flashes the appropriate light. One light flashes twice as fast as the other:

Code: [Select]
// pin numbers

// switches
const int LeftSwitch = 2;   
const int RightSwitch = 3;

// lights
const int LeftBlink =  11;     
const int RightBlink = 10;

long interval = 500;

void setup()
{
  // set the digital pin as output:
  pinMode(LeftBlink, OUTPUT);
  pinMode(RightBlink, OUTPUT);
 
  // enable pull-ups
  digitalWrite (LeftSwitch, HIGH);
  digitalWrite (RightSwitch, HIGH);
}  // end of setup

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

void toggleSlow ()
{
  if (!digitalRead (RightSwitch))
    digitalWrite (RightBlink, !digitalRead (RightBlink));
  else
    digitalWrite (RightBlink, LOW);
}  // end of toggleSlow

void loop()
{
  delay (interval);
  toggleFast ();

  delay (interval);
  toggleFast ();
  toggleSlow ();
 
} // end of loop



Thanks! That code actually flashes the lights UNLESS the switches are pressed/grounded (at least on my setup; my buttons are wired as per example "Push Button Control"). The lights flash, one twice as fast an the other, when the buttons aren't pressed. They turn off when their respective button is pressed.

Is toggleFast() a standard term? I've looked around for an Arduino glossary and have found some useful info, but haven't seen that phrase before. A Google search of the phrase is not helpful...

I also don't understand your use of (!)"not". I understand it when used as a comparator, but not here.

And now that the two lights can blink independent of each other, how can I set the blink rate to be the same? (In my more recently posted sketch I had two different speeds just for distinctions sake) Being blinkers for a vehicle, it makes sense to have them blink at the same speed.  I also need to figure out a brake light with its own behaviors.

Thanks a ton for your help, Nick Gammon! I will keep marching on (somewhat blindly) and see what I can do.

loudboy


Theres nothing super complicate about it. Essentially all you need is the blink without delay sketch, with some extra button checking and blinking.


Understood. That little "extra" part has been kicking my ass though.

Nick Gammon


That code actually flashes the lights UNLESS the switches are pressed/grounded (at least on my setup; my buttons are wired as per example "Push Button Control"). The lights flash, one twice as fast an the other, when the buttons aren't pressed.


How did you wire it up? The way I had it, the wires are not connected unless you "press" them which is grounding them. Thus with the pull-ups enabled, the default (off) position is HIGH, and when you press the switch the are LOW.

Hence this:

Code: [Select]
    digitalWrite (LeftBlink, !digitalRead (LeftBlink));

That reads the switch and the "!" is a "not" which means "reverse the logic", true becomes false, false becomes true.


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.

loudboy

I have my buttons wired as seen in the first breadboard picture here: http://www.ladyada.net/learn/arduino/lesson5.html

But I'm betting you have this: http://arduino.cc/en/Tutorial/Button
I will change my board accordingly, since I didn't have any luck trying to modify the code by deleting the logic reverses.

Go Up