#include "FastLED.h"
#define NUM_LEDS 60
#define DATA_PIN 6
#define POT_PIN A1
#define buttonPin 10
int sinVal = 0; // global for numbSin()
int buttonState = 0; // I bet you can guess what this is for
int palette[]={109,132}; // an array to hold colro values
int paletteCt = (sizeof(palette) / sizeof(palette[0])); // count palette values
CRGB leds[NUM_LEDS]; // Define the array of leds
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.clear();
FastLED.show();
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin); //read the state of the button
palette[paletteCt-1] = potmap(); //assign the mapped malue from the pot to the last position in the array
if(buttonState==HIGH) palette[paletteCt-2] = potmap(); // if the button is down, set the second to last position in the array to the current pot value
leds[numbSin(NUM_LEDS,500,0,0)]=CHSV(palette[random(paletteCt)],255,random(10,80)); // pick the current led using the numbSin funtion, set the color to a random palette position, fully saturated with clamped random brightness
Serial.println(numbSin(NUM_LEDS,500,0,0)); // for some reason calling this a second time fixes everything
FastLED.show(); // display what is currently in the led array
}
int potmap(){ // map a reading from to pot to 0-255
int sensorValue = analogRead(POT_PIN);
sensorValue = map(sensorValue,0,1023,0,255);
return sensorValue;
}
int numbSin( int amp, int waveL,int offset,int delayval){ // offset -right +left , returns the current position on a sin wave
int i = sinVal;
i = i + offset;
int half = amp/2;
i++;
int mysin = (half*sin(i*((PI*2)/waveL))+half);
sinVal = i;
delay(delayval);
if (sinVal > waveL) sinVal = 0;
return mysin;
}
Comments
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎