Skip to content

Instantly share code, notes, and snippets.

@suhajdab
Last active February 17, 2016 23:43
Show Gist options
  • Save suhajdab/5a6650b282c0569446c7 to your computer and use it in GitHub Desktop.
Save suhajdab/5a6650b282c0569446c7 to your computer and use it in GitHub Desktop.
RFduino controller for power LED used inside a upcycled NIFE lamp
int ledPin = 6;
int btnPin = 2;
int state = 0;
int power = 0;
float diff = 0;
int steps = 30;
int statesArray[4] = {0, 85, 170, 255};
void setup() {
pinMode(btnPin, INPUT);
pinMode(ledPin, OUTPUT);
// prepare wake on btn press
RFduino_pinWake(btnPin, HIGH);
}
void btnReleased() {
// cycle through 4 states on btn release
state ++;
if (state > 3) state = 0;
// calculate step size, so transition between states always has same duratiob
diff = (statesArray[state] - power) / steps;
}
void updatePower() {
if (abs(statesArray[state] - power) > abs(diff)) {
power += diff;
} else if (abs(statesArray[state] - power) > 0) {
power = statesArray[state];
}
analogWrite(ledPin, power);
}
void loop() {
int isPressed = digitalRead(btnPin);
if (!isPressed && statesArray[state] == power) {
// good night
RFduino_ULPDelay(INFINITE);
// point of resume
RFduino_resetPinWake(btnPin);
}
if (isPressed) {
// wait for btn to be released
while (digitalRead(btnPin)) {}
btnReleased();
}
updatePower();
delay(20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment