Created
July 26, 2017 18:21
-
-
Save Adirockzz95/11872e599207560c68da53644d018047 to your computer and use it in GitHub Desktop.
LED Neopixel program
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
/** | |
* Program name: This program is part of the project Pen Stand: LED Enabled | |
* Author: Aditya K | |
* Date: 7/1/2017 | |
* LICENSE: MIT | |
* | |
* Discription: This program animates easing effect every time user removes pen from stand. | |
* | |
* | |
* Copyright (c) 2017 Aditya K. | |
* 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 <FastLED.h> | |
#include <avr/interrupt.h> | |
#include <avr/power.h> | |
#include <avr/sleep.h> | |
#define PEN_PIN 2 | |
#define NUM_LEDS 40 | |
#define DATA_PIN 6 | |
struct CRGB leds[NUM_LEDS]; | |
volatile bool int_flag = false; | |
void deep_sleep() { | |
set_sleep_mode(SLEEP_MODE_PWR_DOWN); | |
sleep_enable(); | |
sleep_mode(); | |
sleep_cpu(); | |
} | |
void ease(){ | |
/* code ref: https://github.com/atuline/FastLED-Demos/blob/master/easing/easing.ino */ | |
uint8_t bpm = 30; | |
uint8_t inner = beatsin8(bpm, NUM_LEDS/4, NUM_LEDS/4*3); // Move 1/4 to 3/4 | |
uint8_t outer = beatsin8(bpm, 0, NUM_LEDS-1); // Move entire length | |
uint8_t middle = beatsin8(bpm, NUM_LEDS/3, NUM_LEDS/3*2); // Move 1/3 to 2/3 | |
leds[middle] = CRGB::Purple; | |
leds[inner] = CRGB::Blue; | |
leds[outer] = CRGB::Aqua; | |
nscale8(leds,NUM_LEDS,224); | |
} | |
void animate() { | |
uint8_t cycle = 0; | |
for(;;){ | |
EVERY_N_MILLISECONDS(20){ | |
FastLED.show(); | |
ease(); | |
if(cycle++ == 120) break; | |
} | |
} | |
FastLED.clear(); | |
FastLED.show(); | |
} | |
void animation_ISR() { | |
sleep_disable(); | |
int_flag = true; | |
} | |
void setup() { | |
pinMode(PEN_PIN, INPUT_PULLUP); | |
power_twi_disable(); | |
power_spi_disable(); | |
ADCSRA = 0; | |
power_adc_disable(); | |
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); | |
set_max_power_in_volts_and_milliamps(5, 500); | |
FastLED.clear(); | |
FastLED.setBrightness(100); | |
FastLED.show(); | |
attachInterrupt(digitalPinToInterrupt(PEN_PIN), animation_ISR, CHANGE); | |
} | |
void loop() { | |
deep_sleep(); | |
if(int_flag) { | |
bool pin_status = digitalRead(PEN_PIN); | |
delay(30); | |
pin_status = digitalRead(PEN_PIN); | |
// Pen is removed | |
if(pin_status == HIGH){ | |
animate(); | |
int_flag = false; | |
} | |
// Pen is inserted | |
else if(pin_status == LOW){ | |
int_flag = false; | |
} | |
} | |
attachInterrupt(digitalPinToInterrupt(PEN_PIN), animation_ISR, CHANGE); | |
delay(50); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment