Advertisement
ErnestoGrimes

NumbSin

Jan 31st, 2016
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "FastLED.h"
  2.  
  3. #define NUM_LEDS 60
  4. #define DATA_PIN 6
  5. #define POT_PIN A1
  6. #define buttonPin 10
  7. int sinVal = 0; // global for numbSin()
  8. int buttonState = 0; // I bet you can guess what this is for
  9.  
  10.  
  11. int palette[]={109,132}; // an array to hold colro values
  12. int paletteCt = (sizeof(palette) / sizeof(palette[0]));  // count palette values
  13.  
  14.  
  15. CRGB leds[NUM_LEDS]; // Define the array of leds
  16.  
  17. void setup() {
  18.       FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  19.       FastLED.clear();
  20.       FastLED.show();
  21.       Serial.begin(9600);
  22.       pinMode(buttonPin, INPUT);
  23.      
  24. }
  25.  
  26. void loop() {
  27.   buttonState = digitalRead(buttonPin); //read the state of the button
  28.   palette[paletteCt-1] = potmap(); //assign the mapped malue from the pot to the last position in the array
  29.   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
  30.   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
  31.   Serial.println(numbSin(NUM_LEDS,500,0,0)); // for some reason calling this a second time fixes everything
  32.   FastLED.show(); // display what is currently in the led array
  33. }
  34.  
  35. int potmap(){ // map a reading from to pot to 0-255
  36.   int sensorValue = analogRead(POT_PIN);
  37.   sensorValue = map(sensorValue,0,1023,0,255);
  38.   return sensorValue;
  39. }
  40.  
  41. int numbSin( int amp, int waveL,int offset,int delayval){ // offset -right +left , returns the current position on a sin wave
  42.   int i = sinVal;
  43.   i = i + offset;
  44.   int half = amp/2;
  45.   i++;
  46.   int mysin = (half*sin(i*((PI*2)/waveL))+half);
  47.   sinVal = i;
  48.   delay(delayval);
  49.   if (sinVal > waveL) sinVal = 0;
  50.   return mysin;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement