Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #30 on: April 22, 2011, 04:36:06 pm » |
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
God Member
Karma: 2
Posts: 825
|
 |
« Reply #31 on: April 22, 2011, 04:41:02 pm » |
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
Brattain Member
Karma: 137
Posts: 19007
I don't think you connected the grounds, Dave.
|
 |
« Reply #32 on: April 22, 2011, 04:42:16 pm » |
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.
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #33 on: April 22, 2011, 05:53:10 pm » |
Looking back at page 1, two people suggested using interrupts before the original poster.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 1
Posts: 62
Arduino rocks
|
 |
« Reply #34 on: April 27, 2011, 09:07:30 pm » |
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
Jr. Member
Karma: 1
Posts: 62
Arduino rocks
|
 |
« Reply #35 on: April 27, 2011, 09:25:40 pm » |
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
Jr. Member
Karma: 1
Posts: 62
Arduino rocks
|
 |
« Reply #36 on: April 27, 2011, 09:53:06 pm » |
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? // 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
Sr. Member
Karma: 1
Posts: 360
I'm 15. I like making things. I like breaking things better.
|
 |
« Reply #37 on: April 27, 2011, 10:59:46 pm » |
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," Alice answered. "Then," said the cat," it really doesn't matter, does it?"
-Lewis Carrol
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 241
Posts: 16465
Available for Design & Build services
|
 |
« Reply #38 on: April 27, 2011, 11:16:00 pm » |
need to read the switch:
if ( (LeftBlinkState == LOW) && (digitalRead (LeftBlinkSwitch) == HIGH) )
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #39 on: April 27, 2011, 11:18:17 pm » |
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: // 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
Jr. Member
Karma: 1
Posts: 62
Arduino rocks
|
 |
« Reply #40 on: April 27, 2011, 11:19:26 pm » |
need to read the switch:
if ( (LeftBlinkState == LOW) && (digitalRead (LeftBlinkSwitch) == HIGH) )
Oh, duh. Thanks 
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 1
Posts: 62
Arduino rocks
|
 |
« Reply #41 on: April 27, 2011, 11:48:05 pm » |
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: // 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
Jr. Member
Karma: 1
Posts: 62
Arduino rocks
|
 |
« Reply #42 on: April 27, 2011, 11:49:49 pm » |
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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #43 on: April 27, 2011, 11:53:48 pm » |
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: digitalWrite (LeftBlink, !digitalRead (LeftBlink)); That reads the switch and the "!" is a "not" which means "reverse the logic", true becomes false, false becomes true. 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
|
|
|
|
|
|
|
|