Pages: [1]   Go Down
Author Topic: Do something once when switch closed, then blink LED until switch opened  (Read 1011 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've done a bit of searching and not come across a way to do this, so I'll go ahead and post:

I'd like to have my program beep once when a switch is closed, then blink an LED continuously until the switch is opened. The bit I'm having trouble with is getting the beep to execute just once before going into the LED blink loop.

I feel like this should be fairly simply... just haven't succeeded yet. Might as well ask the experts right?

Direct me to an example? Suggestions?

Much appreciated.
Logged

California
Offline Offline
God Member
*****
Karma: 14
Posts: 629
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you post you code, the "experts" will come   smiley

Code:
Don't forget to put the code in a "Code Block".
Logged


Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 533
Posts: 26956
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

Use a flag to indicate that beep has occurred,  clear the flag when the switch is opened to allow beep again.
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.

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

I will try having a variable change state once the beep has occurred:

pseudo-code:
Code:
int flag = 0;
if((switch == HIGH) && (flag == 0)) {
    beep buzzer once
    flag = flag + 1;
    ....now do I initiate another loop here (if or while statement) that blinks the green LED? }
else if (switch == LOW){
    blink red LED
    flag = flag - 1;}
Logged

Mid-Atlantic, USA
Offline Offline
God Member
*****
Karma: 30
Posts: 515
"Remember kids, the only difference between Science and screwing around is writing it down." - Adam Savage
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You almost have it with the pseudo-code, but I don't see a check for when the switch is high and the flag is 1. You might want the check for the flag high to be an additional if statement within the switch high if statement.

Also remember that blinking an LED is an active action (not passive like setting a pin state and going on). That means you need to do something clever to check for the switch changing state while the LED is flashing. This can be done several ways, two that I can think of off the top of my head are:
A) incorporate the trick of the Blink without delay example at http://arduino.cc/en/Tutorial/BlinkWithoutDelay
B) external to the Arduino, make the LEDs blink. This can be done with the output pin of the Arduino activating a 555 timer, or a simple UJT oscillator toggling a flip flop. This takes the blink action away from your code, but reduces flexibility because the LED will always blink.
Logged


Global Moderator
Online Online
Brattain Member
*****
Karma: 480
Posts: 18730
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you post you code, the "experts" will come   smiley

Definition of an expert:

X - unknown
spurt - a drip under pressure
Logged


North Wales
Offline Offline
Jr. Member
**
Karma: 1
Posts: 51
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you post you code, the "experts" will come   smiley

Definition of an expert:

X - unknown
spurt - a drip under pressure

Slightly different to the one I've heard here in the UK: "X" (as in "ex-") = has been. Same definition for spurt smiley
Logged

California
Offline Offline
God Member
*****
Karma: 14
Posts: 629
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you post you code, the "experts" will come   smiley

Definition of an expert:

X - unknown
spurt - a drip under pressure

Or...

"An expert is a person who has made all the mistakes that can be made in a very narrow field." - Niels Bohr

I keep on making new ones, so I must defer...
Logged


Offline Offline
Sr. Member
****
Karma: 0
Posts: 342
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I , in my non expert opinion would do the following,

use an if statement to react to the switch being pressed,
the first thing in the IF is the tone function to get a beep effect,

then within the IF statement, go into a WHILE, which will run the before mentioned blinkwithoutdelay code,

for example,

if (switch==HIGH){
tone (buzzerpin, tone frequency , 20)
          while (switch==HIGH){
           blinkwithoutdelay()
               }
}


im not sure but it could work out well
Logged

Global Moderator
Online Online
Brattain Member
*****
Karma: 480
Posts: 18730
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Stuff about switches:  http://www.gammon.com.au/switches

Stuff about doing things with delays: http://www.gammon.com.au/blink
Logged


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

Thanks ex/x - spurts. I will be taking another shot at this in my free time this week.

This microcontroller stuff is fun.
Logged

Pages: [1]   Go Up
Jump to: