Created
February 29, 2016 00:26
-
-
Save nebulous/752c8be205a6322fa2e2 to your computer and use it in GitHub Desktop.
Arduino source for Digistump Oak undercabinet lights
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
#include <ClickButton.h> | |
#include <pt.h> | |
#include "math.h" | |
//inputs | |
#define upPin 7 | |
#define dnPin 8 | |
//outputs | |
#define ledPin 1 | |
#define stripPin 5 | |
int brightness, targetbright, lastb; | |
static struct pt pt_led, pt_ramp; | |
ClickButton upBtn(upPin); | |
ClickButton dnBtn(dnPin); | |
int setBrightness(String opt) { | |
targetbright = opt.toInt(); | |
return 1; | |
} | |
void setup() { | |
pinMode(upPin, INPUT_PULLUP); | |
pinMode(dnPin, INPUT_PULLUP); | |
pinMode(stripPin, OUTPUT); | |
pinMode(ledPin, OUTPUT); | |
PT_INIT( &pt_led ); | |
PT_INIT( &pt_ramp ); | |
brightness=0; | |
targetbright=0; | |
lastb=1; | |
Particle.variable("brightness", brightness); | |
Particle.function("brightness", setBrightness); | |
} | |
void button_handler() { | |
upBtn.Update(); | |
switch (upBtn.clicks) { | |
case 1: targetbright=255; break; | |
case 2: if (targetbright==0) targetbright=1; targetbright<<=1; break; | |
case -1: targetbright=255; | |
} | |
dnBtn.Update(); | |
switch (dnBtn.clicks) { | |
case 1: targetbright=0; break; | |
case 2: targetbright>>=1; break; | |
case -1:targetbright=0; | |
} | |
} | |
static int led(struct pt *pt) { | |
PT_BEGIN(pt); | |
PT_WAIT_UNTIL(pt, lastb!=brightness); | |
if (brightness<0) brightness=0; | |
if (brightness>255) brightness=255; | |
analogWrite(stripPin, brightness); | |
analogWrite(ledPin, brightness); | |
lastb=brightness; | |
PT_END(pt); | |
} | |
static int ramp(struct pt *pt) { | |
PT_BEGIN(pt); | |
PT_WAIT_UNTIL(pt, targetbright!=brightness); | |
if (targetbright>brightness) { | |
brightness+=1; | |
} else if (targetbright<brightness) { | |
brightness-=1; | |
} | |
delay(4); | |
PT_END(pt); | |
} | |
void loop() { | |
button_handler(); | |
led(&pt_led); | |
ramp(&pt_ramp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment