ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
BlinkLED.cpp
1 /*
2  * Copyright (C) 2012 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "BlinkLED.h"
24 #if defined(ARDUINO) && ARDUINO >= 100
25 #include <Arduino.h>
26 #else
27 #include <WProgram.h>
28 #endif
29 
64 BlinkLED::BlinkLED(uint8_t pin, unsigned long onTime, unsigned long offTime, bool initialState)
65  : _pin(pin)
66  , _state(initialState)
67  , _paused(false)
68  , _onTime(onTime)
69  , _offTime(offTime)
70 {
71  pinMode(pin, OUTPUT);
72  digitalWrite(pin, initialState ? HIGH : LOW);
73  _lastChange = millis();
74 }
75 
80 {
81  if (_paused)
82  return;
83  unsigned long currentTime = millis();
84  if (_state) {
85  if ((currentTime - _lastChange) >= _onTime) {
86  digitalWrite(_pin, LOW);
87  _lastChange += _onTime;
88  _state = false;
89  }
90  } else {
91  if ((currentTime - _lastChange) >= _offTime) {
92  digitalWrite(_pin, HIGH);
93  _lastChange += _offTime;
94  _state = true;
95  }
96  }
97 }
98 
122 void BlinkLED::setBlinkRate(unsigned long onTime, unsigned long offTime)
123 {
124  _onTime = onTime;
125  _offTime = offTime;
126 }
127 
145 void BlinkLED::setState(bool state)
146 {
147  if (_state != state) {
148  digitalWrite(_pin, state ? HIGH : LOW);
149  _state = state;
150  _lastChange = millis();
151  }
152 }
153 
171 {
172  if (_paused) {
173  _paused = false;
174  unsigned long currentTime = millis();
175  if (_state) {
176  if ((currentTime - _lastChange) >= _onTime) {
177  digitalWrite(_pin, LOW);
178  _lastChange = currentTime;
179  _state = false;
180  }
181  } else {
182  if ((currentTime - _lastChange) >= _offTime) {
183  digitalWrite(_pin, HIGH);
184  _lastChange = currentTime;
185  _state = true;
186  }
187  }
188  }
189 }
190 
void resume()
Resumes the LED blink cycle after a pause().
Definition: BlinkLED.cpp:170
void loop()
Definition: BlinkLED.cpp:79
unsigned long offTime() const
Returns the number of milliseconds the LED will be off.
Definition: BlinkLED.h:36
void setState(bool state)
Sets the current state of the LED, where true is on, false is off.
Definition: BlinkLED.cpp:145
void setBlinkRate(unsigned long onTime, unsigned long offTime)
Sets the onTime and offTime (in milliseconds).
Definition: BlinkLED.cpp:122
bool state() const
Returns the current state of the LED; true is on, false is off.
Definition: BlinkLED.h:39
unsigned long onTime() const
Returns the number of milliseconds the LED will be on.
Definition: BlinkLED.h:35
BlinkLED(uint8_t pin, unsigned long onTime, unsigned long offTime, bool initialState=false)
Initialize a blinking LED on the specified pin.
Definition: BlinkLED.cpp:64