Last active
May 3, 2018 00:22
-
-
Save fbrnc/c3ea0b02adc78892f19a to your computer and use it in GitHub Desktop.
Hardware AJAX Loader using an Attiny85, 3 potentiometers and a 24 RGB LED ring
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
#include <FastLED.h> | |
#define NUM_LEDS 24 | |
#define NUM_DOTS 3 | |
#define DATA_PIN 0 | |
#define MAX_SPEED 60 | |
using namespace std; | |
CRGB leds[NUM_LEDS]; | |
class Dot { | |
int resolution = 2048; | |
int color; | |
byte maxbrightness = 50; | |
int current_speed = 0; | |
float mean; | |
float sigma; | |
int currentposition = 0; | |
int stepwidth; | |
public: | |
void setColor(byte); | |
void setCurrentSpeed(int); | |
void next(); | |
byte getBrightness(byte); | |
CHSV getColor(byte); | |
}; | |
void Dot::setColor (byte _color) { | |
color = _color; | |
stepwidth = resolution / NUM_LEDS; | |
mean = resolution / 2; | |
sigma = resolution / 18; | |
} | |
void Dot::setCurrentSpeed (int _speed) { | |
current_speed = _speed; | |
} | |
void Dot::next () { | |
currentposition = (currentposition + current_speed) % resolution; | |
} | |
byte Dot::getBrightness (byte led) { | |
int x = (currentposition + led * stepwidth); | |
if (x < 0) { x+=resolution; } | |
x %= resolution; | |
return maxbrightness * exp(-0.5 * pow((x - mean) / sigma, 2.)); | |
} | |
CHSV Dot::getColor(byte led) { | |
return CHSV(color, 255, getBrightness(led)); | |
} | |
Dot dots[NUM_DOTS]; | |
void setup() { | |
dots[0].setColor(HUE_RED); | |
dots[1].setColor(HUE_GREEN); | |
dots[2].setColor(HUE_BLUE); | |
dots[0].setCurrentSpeed(0); | |
dots[1].setCurrentSpeed(-5); | |
dots[2].setCurrentSpeed(5); | |
FastLED.addLeds<WS2812B, DATA_PIN>(leds, NUM_LEDS); | |
} | |
void loop() { | |
dots[0].setCurrentSpeed(map(analogRead(A1), 0, 1023, MAX_SPEED, -1 * MAX_SPEED)); | |
dots[1].setCurrentSpeed(map(analogRead(A2), 0, 1023, MAX_SPEED, -1 * MAX_SPEED)); | |
dots[2].setCurrentSpeed(map(analogRead(A3), 0, 1023, MAX_SPEED, -1 * MAX_SPEED)); | |
for (int i=0; i<NUM_DOTS; i++) { | |
dots[i].next(); | |
} | |
for (int led=0; led<NUM_LEDS; led++) { | |
for (int i=0; i<NUM_DOTS; i++) { | |
if (i==0) { | |
leds[led] = dots[i].getColor(led); | |
} else { | |
leds[led] += dots[i].getColor(led); | |
} | |
} | |
} | |
FastLED.show(); | |
delay(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment