Pages: [1]   Go Down
Author Topic: Simple LED Blink code, Kinda....  (Read 764 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all, great forum you have here. I am super impressed with the knowledge.. smiley-mr-green

So all I need is an LED(or series of) to blink upon receiving a high on one of the pins. Ok easy, now the part I can't figure out. The way I have the code written is similar to the button example, and I have the LED blinking however with that input staying high the arduino with continue to blink it. I would like the LED to blink, then go low while that input stays high. The input at some point will go low, and I would like the cycle to start again upon going high.. smiley-roll-sweat

FYI I am using an UNO.

Thank you all for the help and at least looking over my post, much appreciated... smiley
« Last Edit: February 03, 2013, 06:42:22 am by medictrode » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Keep track of the previous state of the input pin and only start a blink when the current state is HIGH and the previous state is LOW.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what is blink in your definition?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49092
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Posting in this forum requires that you post your code, no matter how poor you think it is. We can be a better judge of that.  smiley-cool

Logged

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

Hello again, and thank you for the replies, much appreciated. Sorry for the vague blink description, I was trying to keep it light(no pun intended)  smiley-grin.. Anyway, what I mean by blink is an LED being high--delay--low..

Here is the code I am slowly modifying. It is the button example from the libraries. Once the button is engaged I want the LED to blink once, then stay low while that button is engaged. After the button is disengaged, then engaged again the LED will blink once, then back to where we were...

Thank you again.... smiley-cool


Code:


const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);    
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
    // turn LED on:    
    digitalWrite(ledPin, HIGH);
    delay(200);
    digitalWrite(ledPin, LOW);
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49092
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You want the code to do something when the switch state changes from not pressed to pressed, only. Right?

Notice that that is changes, not is. You need to keep track of the previous state of the switch.

Code:
int prevState = LOW;
int currState;

void loop()
{
   currState = digitalRead(buttonPin);
   if(currState != prevState)
   {
      // A transition occurred - pressed to released OR released to pressed
   }
   prevState = currState;
}

In the block where the transition occurred, the current state of the switch defines which transition (the switch is now pressed or the switch is now released).

Can you see how to use this in your code?

By the way, switches generally work better with the Arduino than buttons. Buttons work better for shirts.
Logged

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

Blinks, Buttons..LOL. I will get the proper terminology down .. smiley-mr-green.. But yes for what I am doing a switch is the way to go, and how I am actually doing it.

I can see where you are going with this, I will give it a try and see how it goes.. Again, much appreciated.. smiley
Logged

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

Hello again all, sorry for the delay as I have been busy with life...  smiley

So here is what I have so far. You guys have me on the right track for sure, but there are a few issues I can't figure out. When the switch is held High, the LED will blink(High then Low). When the switch is set to Low the Led will blink again, which in all honesty I kinda prefer however I would like to figure out how to just make it blink when the switch is set to High, then not blink again until the switch is cycled low then back to High again.

Any further assistance or hints into the right direction would be super appreciated... smiley-grin

Code:

int switch1 = 12;     // the number of the switch pin
int led1 =  4;      // the number of the LED pin

int prevState = LOW;
int currState;


void setup()
 {
  // initialize the LED pin as an output:
  pinMode(led1, OUTPUT);
 
                             
                             // initialize the switch pin as an input:
  pinMode(switch1, INPUT);     
 }
 
void loop()
 {
 
  currState = digitalRead(switch1);
 
 
  if (currState != prevState)
   {     
   digitalWrite(led1, HIGH);
   delay(100);
   digitalWrite(led1, LOW);
   delay(100);
 
   
   }
  prevState = currState;

 
 
 
 }
 
   
 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49092
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a hard time





reading code that





has so many blank lines.

It's like reading a book full of blank pages. By the time you find the next bit of code, you've forgotten what you were looking for.

Anyway, it looks like your problem is a floating pin. You are not using the internal pullup resistors, so the switches nee to have external pullup or pulldown resistors.

How are your switches wired?

Using the internal pullup resistor is easy. Just add digitalWrite(switch1, HIGH); after the pinMode() call. Then, connect one leg of the switch to pin 12 and the other leg to ground. Then, LOW is pressed and HIGH is released (just like the top of the switch).
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3379
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello again all, sorry for the delay as I have been busy with life...  smiley

So here is what I have so far. You guys have me on the right track for sure, but there are a few issues I can't figure out. When the switch is held High, the LED will blink(High then Low). When the switch is set to Low the Led will blink again, which in all honesty I kinda prefer however I would like to figure out how to just make it blink when the switch is set to High, then not blink again until the switch is cycled low then back to High again.

Right now, you're just detecting signal edges. You're not looking at where it's a falling edge (HIGH to LOW) or a rising edge (LOW to HIGH). Checking for what the current state is will tell you which edge it is.
Logged

Pages: [1]   Go Up
Jump to: