ArduinoLibs
|
00001 /* 00002 * Copyright (C) 2012 Southern Storm Software, Pty Ltd. 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a 00005 * copy of this software and associated documentation files (the "Software"), 00006 * to deal in the Software without restriction, including without limitation 00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 * and/or sell copies of the Software, and to permit persons to whom the 00009 * Software is furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included 00012 * in all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00020 * DEALINGS IN THE SOFTWARE. 00021 */ 00022 00023 #include "ChaseLEDs.h" 00024 #if defined(ARDUINO) && ARDUINO >= 100 00025 #include <Arduino.h> 00026 #else 00027 #include <WProgram.h> 00028 #endif 00029 00071 ChaseLEDs::ChaseLEDs(const uint8_t *pins, int num, unsigned long advanceTime) 00072 : _pins(pins) 00073 , _numPins(num) 00074 , _currentIndex(-1) 00075 , _advanceTime(advanceTime) 00076 , _lastChange(millis()) 00077 { 00078 for (uint8_t index = 0; index < _numPins; ++index) { 00079 pinMode(_pins[index], OUTPUT); 00080 digitalWrite(_pins[index], LOW); 00081 } 00082 } 00083 00087 void ChaseLEDs::loop() 00088 { 00089 if (_currentIndex >= 0) { 00090 if ((millis() - _lastChange) >= _advanceTime) { 00091 // Advance to the next LED in sequence. 00092 _currentIndex = (_currentIndex + 1) % _numPins; 00093 _lastChange += _advanceTime; 00094 advance(previousPin(1), _pins[_currentIndex]); 00095 } 00096 } else { 00097 // First time - light the first LED. 00098 _currentIndex = 0; 00099 _lastChange = millis(); 00100 advance(previousPin(1), _pins[_currentIndex]); 00101 } 00102 } 00103 00136 void ChaseLEDs::advance(uint8_t prevPin, uint8_t nextPin) 00137 { 00138 digitalWrite(prevPin, LOW); 00139 digitalWrite(nextPin, HIGH); 00140 } 00141