-
-
Save harrisonhjones/de9de281add4a417802f to your computer and use it in GitHub Desktop.
SparkCore LED Effects Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This #include statement was automatically added by the Spark IDE. | |
#include "LEDEffect.h" | |
#include "clickButton.h" | |
//#include <LEDEffect.h> | |
// General Garage Controller Config | |
int internalState = 0; // Start up | |
// LED Config | |
const int ledPin = D0; | |
LEDEffect led1(ledPin); | |
int ledState = 0; | |
// Relay Config | |
const int rlyPin = D1; | |
char rlyState = 0; | |
unsigned long rlyLastTime = 0; | |
const int rlyLockoutTime = 3000; // Seconds | |
const int rlyTime = 2000; | |
// Button Config | |
const int buttonPin1 = D3; | |
ClickButton button1(buttonPin1, LOW, CLICKBTN_PULLUP); | |
void setup() | |
{ | |
// Pin Mode Config | |
pinMode(ledPin, OUTPUT); | |
pinMode(rlyPin, OUTPUT); | |
pinMode(buttonPin1, INPUT_PULLUP); | |
// Clickbutton Setup | |
button1.debounceTime = 20; // Debounce timer in ms | |
button1.multiclickTime = 250; // Time limit for multi clicks | |
button1.longClickTime = 1000; // time until "held-down clicks" register | |
led1.on(); | |
ledState = 1; | |
led1.breath(30); | |
digitalWrite(rlyPin, LOW); | |
} | |
void goRelay() | |
{ | |
if(rlyState == 0) // Relay is "normal" | |
{ | |
rlyState = 1; | |
rlyLastTime = millis(); | |
} | |
} | |
void relayUpdate() | |
{ | |
if(rlyState == 1) // Relay is "ON" | |
{ | |
led1.blink(200); | |
digitalWrite(rlyPin, HIGH); | |
if(millis() > (rlyLastTime + rlyTime)) | |
{ | |
rlyState = 2; // Lock the relay | |
} | |
} | |
else if(rlyState == 2) | |
{ | |
led1.dim(50); | |
digitalWrite(rlyPin, LOW); | |
if(millis() > (rlyLastTime + rlyLockoutTime)) | |
{ | |
rlyState = 0; // Lock the relay | |
} | |
} | |
else | |
{ | |
rlyState = 0; | |
digitalWrite(rlyPin, LOW); | |
} | |
} | |
void stateLEDUpdate() | |
{ | |
if(rlyState == 0) // Normal Relay Operation | |
{ | |
if(internalState == 0) // Normal Operation | |
{ | |
led1.breath(30); | |
} | |
} | |
} | |
void loop() | |
{ | |
// Update button state | |
stateLEDUpdate(); | |
button1.Update(); | |
led1.update(); | |
relayUpdate(); | |
if(button1.clicks == 1) | |
goRelay(); | |
/*if(ledState == 0) | |
{ | |
led1.on(); | |
ledState = 1; | |
} | |
else if(ledState == 1) | |
{ | |
led1.breath(30); | |
ledState = 2; | |
} | |
else if(ledState == 2) | |
{ | |
led1.fadeDown(30); | |
ledState = 3; | |
} | |
else if(ledState == 3) | |
{ | |
led1.fadeUp(10); | |
ledState = 4; | |
} | |
else if(ledState == 4) | |
{ | |
led1.blink(250); | |
ledState = 5; | |
} | |
else | |
{ | |
led1.off(); | |
ledState = 0; | |
}*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
LEDEffect.cpp - Library for LED Effecs. | |
Created by Harrison H. Jones, October 3, 2014. | |
*/ | |
//#include "Arduino.h" | |
#include "LEDEffect.h" | |
LEDEffect::LEDEffect(int pin) | |
{ | |
pinMode(pin, OUTPUT); | |
_pin = pin; | |
_time = millis(); | |
_brightness = 125; | |
_fadeAmount = 5; // how many points to fade the LED by | |
_fadeDirection = _fadeAmount; | |
_ledState = 0; // 0 = off, 1 = on, 2 = breath, 3 = fade down, 4 = fade up, 5 = blink | |
_ledDelay = 30; // in ms | |
} | |
void LEDEffect::update() | |
{ | |
if(millis() > _time + _ledDelay) | |
{ | |
_time = millis(); | |
if(_ledState == 0) | |
_brightness = 0; | |
else if(_ledState == 1) | |
_brightness = 255; | |
else if (_ledState == 2) | |
{ | |
// change the _brightness for next time through the loop: | |
_brightness = _brightness + _fadeDirection; | |
// reverse the direction of the fading at the ends of the fade: | |
if (_brightness == 0) | |
_fadeDirection = _fadeAmount; | |
else if (_brightness == 255) | |
_fadeDirection = -_fadeAmount; | |
} | |
else if (_ledState == 3) // Fade down | |
{ | |
// change the _brightness for next time through the loop: | |
_brightness = _brightness - _fadeAmount; | |
// reverse the direction of the fading at the ends of the fade: | |
if (_brightness == 0) | |
_brightness = 255; | |
} | |
else if (_ledState == 4) // Fade up | |
{ | |
// change the _brightness for next time through the loop: | |
_brightness = _brightness + _fadeAmount; | |
// reverse the direction of the fading at the ends of the fade: | |
if (_brightness == 255) | |
_brightness = 0; | |
} | |
else if(_ledState == 5) | |
{ | |
if(_brightness == 255) | |
_brightness = 0; | |
else | |
_brightness = 255; | |
} | |
else if(_ledState == 6) // Dim | |
{ | |
} | |
} | |
analogWrite(_pin, _brightness); | |
} | |
void LEDEffect::off() | |
{ | |
_ledState = 0; | |
_ledDelay = 10; | |
} | |
void LEDEffect::on() | |
{ | |
_ledState = 1; | |
_ledDelay = 10; | |
} | |
void LEDEffect::breath(int ledDelay) | |
{ | |
if(_brightness == 0) | |
_fadeDirection = _fadeAmount; | |
else if(_brightness == 255) | |
_fadeDirection = -_fadeAmount; | |
_ledState = 2; | |
_ledDelay = ledDelay; | |
} | |
void LEDEffect::fadeDown(int ledDelay) | |
{ | |
_fadeDirection = -_fadeAmount; | |
_ledState = 3; | |
_ledDelay = ledDelay; | |
} | |
void LEDEffect::fadeUp(int ledDelay) | |
{ | |
_fadeDirection = _fadeAmount; | |
_ledState = 4; | |
_ledDelay = ledDelay; | |
} | |
void LEDEffect::blink(int ledDelay) | |
{ | |
_ledState = 5; | |
_ledDelay = ledDelay; | |
} | |
void LEDEffect::dim(unsigned char brightness) | |
{ | |
_ledState = 6; | |
_brightness = brightness; | |
_ledDelay = 1000; // Not really required. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
LEDEffect.h - Library for LED Effecs. | |
Created by Harrison H. Jones, October 3, 2014. | |
*/ | |
#ifndef LEDEffect_h | |
#define LEDEffect_h | |
//#include "Arduino.h" | |
#include "application.h" | |
class LEDEffect | |
{ | |
public: | |
LEDEffect(int pin); | |
void update(); | |
void off(); | |
void on(); | |
void breath(int ledDelay); | |
void fadeDown(int ledDelay); | |
void fadeUp(int ledDelay); | |
void blink(int ledDelay); | |
void dim(unsigned char brightness); | |
private: | |
int _pin; | |
unsigned char _brightness; | |
unsigned char _fadeAmount; // how many points to fade the LED by | |
unsigned char _fadeDirection; | |
unsigned char _ledState; // 0 = off, 1 = on, 2 = breath, 3 = fade down, 4 = fade up, 5 = blink | |
int _ledDelay; // in ms | |
unsigned long _time; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment