Skip to content

Instantly share code, notes, and snippets.

@Adirockzz95
Created July 26, 2017 18:22
Show Gist options
  • Save Adirockzz95/a9c3e1f837f9f8cbfbec639f57d911e4 to your computer and use it in GitHub Desktop.
Save Adirockzz95/a9c3e1f837f9f8cbfbec639f57d911e4 to your computer and use it in GitHub Desktop.
Neopixel rainbow effect
/**
* 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 different color patterns 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
#define RUN_FOR_N_MILLISECONDS(N) for(uint32_t start = millis(); (millis()- start) < N; )
CRGB leds[NUM_LEDS];
CRGBPalette16 currentPalette;
CRGBPalette16 targetPalette;
TBlendType currentBlending;
volatile bool int_flag = false;
uint8_t max_bright = 128;
void deep_sleep() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
sleep_cpu();
}
void animate() {
RUN_FOR_N_MILLISECONDS(4600) {
beatwave();
EVERY_N_MILLISECONDS(100) {
uint8_t maxChanges = 24;
nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // AWESOME palette blending capability.
}
EVERY_N_MILLISECONDS(800) { // Change the target palette to a random one every 5 seconds.
targetPalette = CRGBPalette16(CHSV(random8(), 255, random8(128, 255)), CHSV(random8(), 255,
random8(128, 255)), CHSV(random8(), 192, random8(128, 255)), CHSV(random8(), 255, random8(128, 255)));
}
FastLED.show();
}
FastLED.clear();
FastLED.show();
}
void beatwave() {
uint8_t wave1 = beatsin8(9, 0, 255); // That's the same as beatsin8(9);
uint8_t wave2 = beatsin8(8, 0, 255);
uint8_t wave3 = beatsin8(7, 0, 255);
uint8_t wave4 = beatsin8(6, 0, 255);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette( currentPalette, i + wave1 + wave2 + wave3 + wave4, 255, currentBlending);
}
} // beatwave()
void animation_ISR() {
sleep_disable();
int_flag = true;
}
void setup() {
pinMode(PEN_PIN, INPUT_PULLUP);
power_usart0_disable();
power_twi_disable();
power_spi_disable();
ADCSRA = 0;
power_adc_disable();
delay(800);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
set_max_power_in_volts_and_milliamps(5, 500);
currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND;
FastLED.clear();
FastLED.setBrightness(max_bright);
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