zenlogic

musings of a self-proclaimed inventor named Travis

Starduino Source Code Version 1

Posted on June 3rd, 2010 by Travis ("ottobonn")

Below is the current source code for the Starduino project! I hope someone out there will find it as fun as I do. It was tested and deployed with Arduino 0018, though will likely work on most (if not all) previous releases. Enjoy!

Creative Commons License
Starduino by Travis Geis is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.
Based on a work at www.zenlogic.org.

/*
Starduino by Travis Geis

for Emily Rolen
with love...

Version 1
Wednesday 2 June 2010
*/

const int ONEPIN = 3, TWOPIN = 5, THREEPIN = 6, FOURPIN = 9, FIVEPIN = 10, POTPIN = 3;
int currentOne, currentTwo, currentThree, currentFour, currentFive = 0; //On a scale of 256 (direct output values, NOT percentages!)
int maxBrightness, minBrightness, brightnessOffset;
double spacing, potValue, crossover, lowerBuffer, upperBuffer;
const int sampleNumber = 100;
int potSamples[sampleNumber];
int currentIndex = 0;
unsigned long total = 0;

void setup(){
  //init pot sample array
  for(int i = 0; i < sampleNumber; i++){
    potSamples[i] = 0;
  }
  spacing = 0.5; //radians on a sinusoid
  maxBrightness = 100; //percent
  minBrightness = 0; //percent
  brightnessOffset = 100; //percent
  crossover = 50; //percent
  lowerBuffer = 0; //use these buffers if the lower percentage of each half-pot is flaky
  upperBuffer = 0; //measures in percent
  pinMode(ONEPIN, OUTPUT);
  pinMode(TWOPIN, OUTPUT);
  pinMode(THREEPIN, OUTPUT);
  pinMode(FOURPIN, OUTPUT);
  pinMode(FIVEPIN, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  static double time; //looping counter for wave function
  if(time > 2*PI){
    time = 0;
  }
  potValue = readPotPercent(); //Percent
  //Serial.println(potValue);
  if(potValue >= 0 + lowerBuffer && potValue < crossover){
    double brightness = (double) potValue / (double) crossover * 100;
    showLightPercentage(brightness, brightness, brightness, brightness, brightness);
  }else if(potValue >= crossover + upperBuffer && potValue <= 100){
    maxBrightness = (double) (potValue - crossover) / (double) (100 - crossover) * 100.0;
    //CONSTANT FOR NOW //minBrightness = max(0, maxBrightness - brightnessOffset); //whichever's bigger: 0 or the subtraction
    showLightPercentage(wave(time + (2 * spacing)), wave(time + spacing), wave(time), wave(time - spacing), wave(time - (2 * spacing)));
  }else{
    showLightPercentage(100, 100, 100, 100, 100);
  }
  time += 0.005;
  delay(1);
}
double wave(double time){ //returns a percentage
  return ( (double) (maxBrightness - minBrightness) / 2.0 ) * (cos(time) + 1.0) + minBrightness;
}
void showLight(int one, int two, int three, int four, int five){
  analogWrite(ONEPIN, one);
  analogWrite(TWOPIN, two);
  analogWrite(THREEPIN, three);
  analogWrite(FOURPIN, four);
  analogWrite(FIVEPIN, five);
}
void showLightPercentage(double one, double two, double three, double four, double five){
  showLight(one * 2.55, two * 2.55, three * 2.55, four * 2.55, five * 2.55);
}
int readPot(){
  //this method is smoothed and so experiences a slight lag behind actual values
  //but is much more stable
  total -= potSamples[currentIndex]; //subract the oldest entry
  potSamples[currentIndex] = analogRead(POTPIN); //read the pot and log it in the array
  total += potSamples[currentIndex]; //add the newest
  currentIndex++; //move the index forward one
  if(currentIndex >= sampleNumber) //wrap around if needed
    currentIndex = 0;
  return total / sampleNumber; //preserve decimals until after division
}
double readPotPercent(){
  return readPot() / 10.23; //(dividing by 1023 quanta of resolution and multiplying by 100 percent)
}