William's big Infinity Mirror Code

William's Infinity Mirror Code

     Hey Makers! William here! You may have seen my short video show-and-telling about my infinity mirror! During the video I offered to show my code for it, so here it is! 
If you wanted to use this at home you'll need some form of Arduino and you'll need the FastLED library which can be found here
You'll probably want to look into changing the value of "NUM_LEDS" to fit the number of leds you have in your project.
Or perhaps you just wanna read for yourself what some silly Arduino noob programmed?
Observe away!
I have some limited commenting that may explain some of what is going on. Code comments are in green.
Spelling errors included for posterity. 

Make yourself a great day, 
William


#include<FastLED.h>
//Pin Number Defines
#define PIN_LED 5
#define PIN_POT A0
#define PIN_BUT 2
//delay value for slowet painterdot movement
#define ANIM_SPEED_SLOWEST 75


//Strip Characteristic Defines
#define NUM_LEDS 67



//Global Variables
//storage hole for potetiometer value: maybe unneededed
int POTVAL = 0;
//state variable for button interrupt 
volatile byte is_accel = false;
//default delay val is ANIM_SPEED_SLOWEST
int delayVal = ANIM_SPEED_SLOWEST;
//holding counter for loops at top speed
int holding = 0;
int max_held = 6;

//Instantiate LED array
CRGB lstrip[NUM_LEDS];


void setup() {
  //Flag array as NEOPIXEL ARRAY
  FastLED.addLeds<NEOPIXEL, PIN_LED>(lstrip, NUM_LEDS);
  //button init
  pinMode(PIN_BUT, INPUT);
  attachInterrupt(digitalPinToInterrupt(PIN_BUT), switchswitch, CHANGE);
  //Serial.begin(9600);
}

void loop() {
  //The following loop steps through the strip of the led. One completion of this loop is one full lap
  // the loop does the following:
  // Reads the value of the potentiometer puts it in a variable -> remaps the value of the potentiometer into a RBG friendly value ->
  // Sets the current position to white -> sets the color value of the led behind the painter to a color based on potentiometer value ->
  // updates the strip -> Delays (animation speed) -> checks for acceleration flag and accelerates if set
  for(int i = 0; i <= (NUM_LEDS-1); i++){
    POTVAL = analogRead(PIN_POT);
    POTVAL = map(POTVAL, 15, 1023, 0, 255);

    //Serial Dignostic Info    
    //Serial.print("potval is ");
    //Serial.println(POTVAL);
    //Serial.print("i is ");
    //Serial.println(i);
    lstrip[i] = CRGB::White;
    
    if(i){
      lstrip[(i-1)].setHue(POTVAL);
    }
    if(i==(NUM_LEDS-1)){
      lstrip[i].setHue(POTVAL);
    }
    FastLED.show();
    
    //delay controls the speed of the animation: nooby i know
    delay(delayVal);
    

    //Horriblyineffecient array of checks for accel
    //Flag Check, accelerate
    if(is_accel & delayVal > 0){
      delayVal -= 15;
      //check for negative, cannot have negative delay I take it.
      if(delayVal < 0){
        delayVal = 0; 
        }
      }
    
  }
  //This segment only checks after a full lap. This segment decelerates the dot from its top speed to its slowests speed.
  //speed lowering only happens each full lap
  //will take ~8 or so laps to get back to slowest speed
  //Top SPeed, flag check holding increment
  if(is_accel & delayVal == 0) {
      holding++;
    }
// flag check and top speed loops check -> disable flag
  if(is_accel & holding == max_held) {
      is_accel = !is_accel;
     }
//if flag off and not at lowest speed, decelerate
//decelerate in stages this waybecause i am not good at math yet
  if(is_accel == false & delayVal < ANIM_SPEED_SLOWEST){
        if(holding != 0){
          holding = 0;
        }
      if(delayVal <= 2){
        delayVal++;
      }
      else if(delayVal <= 12){
        delayVal += 3;
      }
      
      else if(delayVal < ANIM_SPEED_SLOWEST){
        delayVal += 15;
      }
     }
     //Serial.print("Delayval is ");
     //Serial.println(delayVal);
     //Serial.print("Flagstate = ");
     //Serial.println(is_accel);

}

//function checks if at lowest speed to allow flag change and otherwise flips the acceleration flag
void switchswitch() {
  if(delayVal == ANIM_SPEED_SLOWEST){
  is_accel = true;
  }
}


Comments

Popular posts from this blog

Montana Mask - Makerspace Live!

MakerCraft LIVE! Chocolate Eclairs Recipe

Matcha Sugar Cookies Transcript/Recipe