Skip to content

Instantly share code, notes, and snippets.

@fvdbosch
Created May 16, 2016 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fvdbosch/657e77b24ccba5fcc27567d60813da30 to your computer and use it in GitHub Desktop.
Save fvdbosch/657e77b24ccba5fcc27567d60813da30 to your computer and use it in GitHub Desktop.
button to trigger kaku switch
#include "Arduino.h"
#include "Wire.h"
#include "Qduino.h" // https://www.sparkfun.com/products/13614
#include "RemoteSwitch.h" // https://github.com/jccprj/RemoteSwitch-arduino-library
fuelGauge battery; // initialize the library
qduino q;
KaKuSwitch kaKuSwitch(5);
const int led = 9;
const int button = 0;
bool lightState = 0;
long lastTrigger = 0;
void setup(){
Wire.begin();
battery.setup();
q.setup();
pinMode(led, OUTPUT);
pinMode(button, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(button), changeLight, CHANGE);
analogWrite(led, 0);
}
void loop(){
pulseLed(10);
checkBattery();
turnLightOff();
}
void checkBattery() {
int charge = battery.chargePercentage();
battery.reset();
if(charge >= 75) {
q.setRGB(GREEN);
}
else if(charge >= 50 && charge < 75) {
q.setRGB(YELLOW);
}
else if(charge >= 25 && charge < 50) {
q.setRGB(ORANGE);
}
else {
q.setRGB(RED);
}
}
void pulseLed(int waitms) {
for(int i=0; i<255; i++) {
analogWrite(led, i);
delay(waitms);
}
for(int i=255; i>0; i--) {
analogWrite(led, i);
delay(waitms);
}
}
void changeLight() {
if((millis() - lastTrigger) > 1000) {
lastTrigger = millis();
lightState = !lightState;
kaKuSwitch.sendSignal('A',1,lightState);
}
}
void turnLightOff() {
if(lightState && (millis() - lastTrigger) > (60*1000)) {
changeLight();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment