Friday, January 2, 2015

Steampunk Light Dress Part Three: The Easy Part

Hammered and QA'd the code for keeping the lights steady.  Simple, really; just a for loop that iterates over the all the passed lights (which I established in their own custom class and are passed in the function via an array) and sets them all to on.

Once one of these two things happen:

  •  Given number of seconds (passed to the function as an argument of type long) have passed.
  • The user changes the setting on the main potentiometer (effectively changing the setting.)
The "turnOffAll" function turns off all the lamps and return routes back to the function that called it.

Code follows.  A few values pulled from outside the scope of the function:
  • potPin: the pin of the main potentiometer.  This passes a value from 0 to 1024, and determines which setting the Arduino kicks off.
  • High and Low: the floor and ceiling value range for the current function.  Believe this one runs when the potentiometer is set in the 400-500 range... so if the user sets it below 399 or above 501, they would like the lights to do something different.
 To do:
  • Rename a few of the functions to something more logical
  • More comments

void solid(Led ledList[],int arraySize, long seconds)
{
  // The count increments each time the lights fire.  Once the count exceeds the number of seconds passed, it returns to the main/random loop. 
 long count=0;
// Stores the initial value of the Main Pot.
  int initVal=analogRead(potPin);
// This is our loop.  It runs while both the 
  do {
    for (int thisPin = 0; thisPin < arraySize; thisPin++)  {
        if (ledList[thisPin].IsRGB())
        {
        fireLed(ledList[thisPin],true);
        }
        else {
        digitalWrite(ledList[thisPin].RPin(), HIGH); 
        }
        // Reads the Main Pot.  If the user has set the potentiometer outside the range of the current function (represented by high and low,) set the count to seconds, breaking to do/while loop.
       if (analogRead(potPin)<low||analogRead(potPin)>high)
       {
         // Set the count to seconds.  Once count is iterated, the do/while loop stops. 
         count=seconds;
       }
       count=count+50;
 }
delay(50);
while (count<seconds);
  turnOffAll(ledList,arraySize);
  return;
}

void turnOffAll(Led ledList[], int arraySize)
{
    for (int i=0; i<arraySize;i++)
  {
    turnOffLed(ledList[i]);
  }

}

// Note: the random parameter determines whether or not the LED's color is randomized (false leaves it set at the current color.)
void fireLed(Led led, bool rand)
{
  // Randomize the color
  if (rand&&(led.IsRGB()))
  {
    led.SetRandomColor();
    led.SetRed(255);
    Serial.println("setting random color");


  }

      // Fire red, green, then blue
      analogWrite(led.RPin(),led.Red());
      analogWrite(led.GPin(),led.Green());
      analogWrite(led.BPin(),led.Blue());
      }
      else
      {
      digitalWrite(led.RPin(),HIGH);
      }
}

void turnOffLed(Led led)
{
      if (led.IsRGB())
      {
      // If it's an RGB, turn off red, green, then blue
      analogWrite(led.RPin(),0);
      analogWrite(led.GPin(),0);
      analogWrite(led.BPin(),0);
      }
      else
      {
        digitalWrite(led.RPin(),LOW);
      }

}

No comments:

Post a Comment