Go Down

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

Nick Gammon

Huh? The interrupt could be used to detect the switch press, not to flash the LEDs. However if you go through the main loop fast enough a simple digitalRead should do it.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

skyjumper


Huh? The interrupt could be used to detect the switch press, not to flash the LEDs. However if you go through the main loop fast enough a simple digitalRead should do it.


His app seems very simple, why not try using the Bounce library befoe going to an ISR?

AWOL

Quote
His app seems very simple, why not try using the Bounce library befoe going to an ISR?

That's what we've been trying to persuade him/her to.
The message doesn't seem to be getting through.
"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.

Nick Gammon

Looking back at page 1, two people suggested using interrupts before the original poster.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

loudboy


I don't think you have to PWM brake lights. My own tests show that much faster than around 50Hz is invisible (that is, too fast to see). And the processor clock is 16 MHz.


I'm using PWM to create a pulsing effect rather than a plain old blinking blinker.

loudboy


Quote
His app seems very simple, why not try using the Bounce library befoe going to an ISR?

That's what we've been trying to persuade him/her to.
The message doesn't seem to be getting through.


That's the first mention of a bounce library right there (which is a huge pain in the ass to actually find, BTW. For all the mad coding skills here, this website's search feature sucks.) But now that I have it I'm trying to give it a shot. Probably because I just don't know of the possibilities, but I can't see how this helps me. Is there a Bounce Library Tutorial? Everyone references using the Bounce library, but I cannot for the life of me find anywhere that explains how or why Bounce works (other than keeping a button from chattering when pressed).  Thanks y'all for the help; maybe one of these weeks/months I can find my answers.

loudboy

#36
Apr 28, 2011, 04:53 am Last Edit: Apr 28, 2011, 04:54 am by loudboy Reason: 1
Okay, I've dropped back to basics and am trying to modify the BlinkWithoutDelay sketch to have a button. This seems like it should be easy enough: when the sketch checks the state of the LED to decide if it is already On or not, it has to verify that the blinker switch is as well. Not having much luck. I also tried putting the "&& (LeftBlinkSwitch == HIGH)" in with the section that checks previous Millis against current Millis. I also tried adding "int LeftBlinkSwitchState = LOW;" in with the Variables, but no luck.     Progress?



Code: [Select]

// constants won't change. Used here to
// set pin numbers:
const int LeftBlink =  11;      // I've changed names and pin numbers

const int LeftBlinkSwitch = 8; // added a switch

// Variables will change:
int LeftBlinkState = LOW;             // ledState used to set the LED

long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
 // set the digital pin as output:
 pinMode(LeftBlink, OUTPUT);
 pinMode(LeftBlinkSwitch, INPUT);
 
}

void loop()
{
 // here is where you'd put code that needs to be running all the time.

 // check to see if it's time to blink the LED; that is, if the
 // difference between the current time and last time you blinked
 // the LED is bigger than the interval at which you want to
 // blink the LED.
 unsigned long currentMillis = millis();

 if((currentMillis - previousMillis > interval)) {
   // save the last time you blinked the LED
   previousMillis = currentMillis;  

   // if the LED is off turn it on and vice-versa:
   if ((LeftBlinkState == LOW) && (LeftBlinkSwitch == HIGH)) //*Should* check the state of the LED and the switch
     LeftBlinkState = HIGH;
   else
     LeftBlinkState = LOW;

   // set the LED with the ledState of the variable:
   digitalWrite(LeftBlink, LeftBlinkState);
 }
}

bilbo

Theres nothing super complicate about it. Essentially all you need is the blink without delay sketch, with some extra button checking and blinking.
Alice asked the Chesire Cat, who was sitting in a tree, "What road do I take?"
The cat asked, "Where do you want to go?"
"I don't know," Ali

CrossRoads

need  to read the switch:

if ( (LeftBlinkState == LOW) &&  (digitalRead (LeftBlinkSwitch) == HIGH) )
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Nick Gammon

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
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

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