#include <FastLED.h>
#define LED_PIN 3
#define COLOR_ORDER GRB
#define CHIPSET WS2811
#define BRIGHTNESS 254
const uint8_t kMatrixWidth = 16;
const uint8_t kMatrixHeight = 16;
uint8_t hue;
const bool kMatrixSerpentineLayout = true;
int potpin1 = 0; // analog pin used to connect the potentiometer
int potpin2 = 1;
int potpin3 = 2;
int val1; // variable to read the value from the analog pin
int val2;
int val3;
uint16_t XY( uint8_t x, uint8_t y)
{
uint16_t i;
if( kMatrixSerpentineLayout == false) {
i = (y * kMatrixWidth) + x;
}
if( kMatrixSerpentineLayout == true) {
if( y & 0x01) {
// Odd rows run backwards
uint8_t reverseX = (kMatrixWidth - 1) - x;
i = (y * kMatrixWidth) + reverseX;
} else {
// Even rows run forwards
i = (y * kMatrixWidth) + x;
}
}
return i;
}
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
CRGB* const leds( leds_plus_safety_pixel + 1);
uint16_t XYsafe( uint8_t x, uint8_t y)
{
if( x >= kMatrixWidth) return -1;
if( y >= kMatrixHeight) return -1;
return XY(x,y);
}
void loop()
{
val1 = map(analogRead(potpin1), 0, 1023, 0, 10);
val2 = map(analogRead(potpin2), 0, 1023, 0, 10);
val3 = map(analogRead(potpin3), 0, 1023, 0, 10);
spirograph(val1,val2,val3);
}
void setup() {
Serial.begin(115200);
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
FastLED.setBrightness( BRIGHTNESS );
}
float Sp = 0;
void spirograph(int fixed, //outer ring radius
int moving, //inner, moving ring radius
int offSet //pen offset from inner ring (like the holes in the toy)
){
//int Sp = millis()*.03;
Sp = Sp+.03;
int cX = kMatrixWidth/2;
int cY = kMatrixHeight/2;
int thisX = (fixed+moving) * cos(Sp) - (moving+offSet)*cos(((fixed+moving)/moving)*Sp);
int thisY = (fixed+moving) * sin(Sp) - (moving+offSet)*sin(((fixed+moving)/moving)*Sp);
leds[XYsafe(thisX+cX,thisY+cY)] = CHSV( 0, 0, 255);
Serial.print(val1);
Serial.print(", ");
Serial.print(val2);
Serial.print(", ");
Serial.println(val3);
//Serial.print(thisX+cX);
//Serial.print(", ");
//Serial.println(thisY+cY);
fadeToBlackBy(leds, NUM_LEDS, 1);
FastLED.show();
leds[XYsafe(thisX+cX,thisY+cY)] = CHSV( 16, 255, 255);
}