Thursday, January 1, 2015

Steampunk Light Dress Part Two: Let there be Sparkles

Hammered out (and tested) a function to make the lights sparkle.  The general idea is:

  • Accept an array of the custom Led class, the size of the array, and the number of seconds the function is to be executed.
  • Select a random bulb
  • If it's an LED bulb, set it to a random color.
  • Light the bulb for a random period of time modified by the static sensitivity pot, which is mapped from x1 to x12.
  •  Verify that the wearer has not changed the settings potentiometer.  If they have, return to the main loop to run another function.
  • Turn off the bulb for a random period of time modified per above on the sensitivity pot.
  • Track the total number of seconds paused.  If more than the number of seconds passed, return to the main loop.
The idea is that the "bursts" should be brief in comparison to the pauses, and both should randomize between long and short, giving it a very random, "sparkling" appearance.

The code follows.  Note that it relies upon the following global variables set outside the function scope:
  •  potPin, Sensepin, Staticsensepin: constants referring to the pin #'s of the setting potentiometer(the dial that tells the Arduino what function to run,) the sensitivity potentiometer (the dial that, in our music application, tells it how much gain to add to the sound sensor's analog signal,) and the static sensitivity potentiometer (the dial that tells the music program how sensitive the static (non RGB) LED's should be.)
  • fireLed, turnOffLed: functions to fire and turn off the LED.
  • High, Low: The floor and ceiling values for the setting potentiometer.  If the potentiometer changes beyond these bounds it means that the user wants to go to another function.


  void sparkle(Led ledList[], int arraySize, long seconds)
{
  // the intial setting of the Settings potentiometer.
  int initVal=analogRead(potPin);
  
// Set the count variable (this will track the MS invested in the run and tell the program when to stop.)
 long count=0;
 do
 {
 // Select a random bulb
 int bulb=random(arraySize);  
 // Turn it on (if it's RGB, randomize the color)
 fireLed(ledList[bulb], true);
 // The on sensitivity obtained from the static sense pin
 byte onSensitivity=map(analogRead(staticSensePin),0,1023,1,12); 
 // Select a random pause time
 int inPause=random(20*onSensitivity,50*onSensitivity);
 // Add this to the total count time
 count+=inPause;
 // Pause
 delay(inPause);
 // Turn off the selected bulb
 turnOffLed(ledList[bulb]);
 // break if we've gone beyond the time
 if (analogRead(potPin)>high||analogRead(potPin)<low)
 {
   return;
 }
 // Adds a value calculated from the sensitivity pot
 byte sensitivity=map(analogRead(sensePin),0,1023,1,12); 
 // Select a random pause time

 int outPause=random(50*sensitivity,500*sensitivity);
 // Add this to the total count time.
 count+=outPause;
 // Pause
 delay(outPause);
Serial.println(count);
 }
 while ((seconds)>count);
 // Stop once the alloted number of minutes are done.
}

No comments:

Post a Comment