Created
December 7, 2015 22:21
-
-
Save beriberikix/db669e29c92935c71a9a to your computer and use it in GitHub Desktop.
Demo using Pololu's APA102 library with an ATTiny85 @ 16 MHz.
This file contains hidden or 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
/* This is a fork of the Raindbow sample from | |
* https://github.com/pololu/apa102-arduino/blob/master/examples/Rainbow/Rainbow.ino | |
* to work on an ATTiny85 @ 16MHz. While there is no license in the file, the overall | |
* library appears to be under MIT: | |
* https://github.com/pololu/apa102-arduino/blob/master/LICENSE.txt | |
*/ | |
#include <avr/power.h> | |
#include <APA102.h> | |
// Define which pins to use. | |
const uint8_t dataPin = 0; | |
const uint8_t clockPin = 1; | |
// Create an object for writing to the LED strip. | |
APA102<dataPin, clockPin> ledStrip; | |
// Set the number of LEDs to control. | |
const uint16_t ledCount = 11; | |
// Create a buffer for holding the colors (3 bytes per color). | |
rgb_color colors[ledCount]; | |
// Set the brightness to use (the maximum is 31). | |
const uint8_t brightness = 31; | |
void setup() | |
{ | |
if (F_CPU == 16000000) clock_prescale_set(clock_div_1); | |
} | |
// Converts a color from HSV to RGB. | |
// h is hue, as a number between 0 and 360. | |
// s is the saturation, as a number between 0 and 255. | |
// v is the value, as a number between 0 and 255. | |
rgb_color hsvToRgb(uint16_t h, uint8_t s, uint8_t v) | |
{ | |
uint8_t f = (h % 60) * 255 / 60; | |
uint8_t p = (255 - s) * (uint16_t)v / 255; | |
uint8_t q = (255 - f * (uint16_t)s / 255) * (uint16_t)v / 255; | |
uint8_t t = (255 - (255 - f) * (uint16_t)s / 255) * (uint16_t)v / 255; | |
uint8_t r = 0, g = 0, b = 0; | |
switch((h / 60) % 6){ | |
case 0: r = v; g = t; b = p; break; | |
case 1: r = q; g = v; b = p; break; | |
case 2: r = p; g = v; b = t; break; | |
case 3: r = p; g = q; b = v; break; | |
case 4: r = t; g = p; b = v; break; | |
case 5: r = v; g = p; b = q; break; | |
} | |
return (rgb_color){r, g, b}; | |
} | |
void loop() | |
{ | |
uint8_t time = millis() >> 4; | |
for(uint16_t i = 0; i < ledCount; i++) | |
{ | |
byte x = time - i * 8; | |
colors[i] = hsvToRgb((uint32_t)x * 359 / 256, 255, 255); | |
} | |
ledStrip.write(colors, ledCount, brightness); | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Used with the recommended ATTiny bootloader and the 16MHz clock configuration.