Skip to content

Instantly share code, notes, and snippets.

@giantmolecules
Created June 1, 2017 20:06
Show Gist options
  • Save giantmolecules/bd7adf0308b33a36c60497957172bea1 to your computer and use it in GitHub Desktop.
Save giantmolecules/bd7adf0308b33a36c60497957172bea1 to your computer and use it in GitHub Desktop.
#define SWITCH_PIN 1
#define LED_PIN 2
Timer bounceTimer(200, bounceFunction, 1);
bool switchValue = 0;
bool switchState = 0;
bool direction = 0;
int fadeCounter = 0;
int fadeVal = 0;
void setup() {
pinMode(SWITCH_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
switchValue = digitalRead(SWITCH_PIN);
if(switchValue == 0){
bounceTimer.start();
while(bounceTimer.isActive()){
// do nothing
}
}
if(switchState == 1){
fade();
}
else{
digitalWrite(LED_PIN, LOW);
}
}
void bounceFunction(){
switchState = !switchState;
Particle.publish("Button", String(switchState));
}
void fade(){
if(direction == 1){
fadeCounter++;
}
else{
fadeCounter--;
}
if(fadeCounter >= 255){
direction = 0;
}
if(fadeCounter <= 0){
direction = 1;
}
analogWrite(LED_PIN, fadeCounter);
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment