Skip to content

Instantly share code, notes, and snippets.

@Tho85

Tho85/USBChristmasTree.ino

Last active Nov 24, 2017
Embed
What would you like to do?
/*
The MIT License (MIT)
Copyright (c) 2013 thomasfredericks
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <Bounce2.h>
#define BUTTON_PIN PIN_B2
#define BUTTON_PIN_PULLDOWN PIN_B1
#define LED_PIN PIN_B0
// Instantiate a Bounce object
Bounce debouncer = Bounce();
typedef void (*num_func) ();
extern num_func effects[];
void effectSolid();
void effectBlinkSlow();
void effectBlinkFast();
void effectFadeSlow();
void effectFadeFast();
void effectFire();
void effectOff();
unsigned long effectFireNextRandomTime = 0;
num_func effects[] = {
effectFire,
effectSolid,
effectBlinkSlow,
effectBlinkFast,
effectFadeSlow,
effectFadeFast,
effectOff
};
int currentEffect = 0;
void setup() {
// Setup the button with an internal pull-up :
pinMode(BUTTON_PIN,INPUT_PULLUP);
pinMode(BUTTON_PIN_PULLDOWN, OUTPUT);
digitalWrite(BUTTON_PIN_PULLDOWN, LOW);
// After setting up the button, setup the Bounce instance :
debouncer.attach(BUTTON_PIN);
debouncer.interval(5); // interval in ms
//Setup the LED :
pinMode(LED_PIN,OUTPUT);
}
void loop() {
// Update the Bounce instance :
debouncer.update();
if(debouncer.fell()) {
currentEffect++;
currentEffect %= 7;
}
effects[currentEffect]();
}
void effectSolid() {
digitalWrite(LED_PIN, HIGH);
}
void effectBlinkSlow() {
bool powerOn = (millis()/1000)%2 == 1;
digitalWrite(LED_PIN, powerOn? HIGH : LOW);
}
void effectBlinkFast() {
bool powerOn = (millis()/200)%2 == 1;
digitalWrite(LED_PIN, powerOn? HIGH : LOW);
}
void effectFadeSlow() {
int val = sin(2*M_PI*millis()/5000)*127+128;
analogWrite(LED_PIN, val);
}
void effectFadeFast() {
int val = sin(2*M_PI*millis()/2000)*127+128;
analogWrite(LED_PIN, val);
}
void effectFire() {
if(millis() > effectFireNextRandomTime) {
effectFireNextRandomTime = millis() + random(300);
analogWrite(LED_PIN, random(120)+135);
}
}
void effectOff() {
digitalWrite(LED_PIN, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment