Pages: 1 2 [3] 4   Go Down
Author Topic: (Resolved) Continue running one part of code while still checking other inputs?  (Read 5012 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 3
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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

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.
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: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Looking back at page 1, two people suggested using interrupts before the original poster.
Logged

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

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.
Logged

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

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.
Logged

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

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:
// 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);
  }
}
« Last Edit: April 27, 2011, 09:54:37 pm by loudboy » Logged

0
Offline Offline
Sr. Member
****
Karma: 2
Posts: 360
I'm 15. I like making things. I like breaking things better.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 525
Posts: 26523
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

need  to read the switch:

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

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

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:
// 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
Logged

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

need  to read the switch:

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


Oh, duh. Thanks  smiley-red
Logged

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

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:
// 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.
Logged

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

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.
Logged

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

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:
    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.
Logged

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

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.
Logged

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