| 
View
 

micropolis-busbarn-code-v02.txt

File history uploaded by Zonker Harris 9 years, 7 months ago
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  BusBarn   Version 0.2  - 1 SEP 2014
  - named for the small theater in Los Altos.
  - meant for my Micropolis Bus Terminal module.
  - a switch will toggle the house lights on and off.
  - two PWM outputs will run random "Fireball" 'rotating light'.
  Paste this text into an Arduino IDE to then load into your Arduino
  
  PWM pins: 3, 5, 6, 9    Digital pins: 2, 4, 7, 8
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 
 
#define switchpin 2  //  Switch input to toggle lamps
#define lampspin 4   //  Control pin for LEDs in the terminal (transitor drive)
#define pos1pin 3    //  Fireball lamp at position 1 (LED Anode)
#define pos2pin 5    //  Fireball lamp at position 2 (LED Anode)

boolean lastSwitch = LOW;
boolean currentSwitch = LOW;
boolean ledsOn = false;

int fireballs = 2;
int fireballLevel[] = {0, 0};
int fireballDelay[] = {5, 5} ;  // initial delay countdown at power-up
int fireballLoop[] = {3, 3} ;  // Inital number of flashes at power-up
int fireballSequence[] = {0, 0} ; // Where in the flash cycle (0-95) is each lamp?
// the array below is the PWM intensity of the LEDs in the Fireball sequence...
int fireballCycle[] = {
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 20, 30, 
  40, 60, 80, 100, 100, 100, 100, 100, 100, 100, 120, 150, 190, 230, 250, 
  250, 230, 190, 150, 120, 100, 100, 100, 100, 100, 100, 100, 80, 60, 40, 
  30, 20, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ;
  
void setup() { 
  pinMode (switchpin, INPUT);  // toggle the terminal lamps
  pinMode (lampspin, OUTPUT);  // control for LEDs, 1 = on
  pinMode (pos1pin, OUTPUT);  // PWM for "Fireball" #1
  pinMode (pos2pin, OUTPUT);  // PWM for "Fireball" #2
}

void loop() {
  // Check the switch, toggle the LEDs in the terminal.
  currentSwitch = debounce(lastSwitch);
  if (lastSwitch == LOW && currentSwitch == HIGH)
    { ledsOn = !ledsOn; }
  lastSwitch = currentSwitch;
  digitalWrite (lampspin, ledsOn);
  
  //Run the Fireball!
  for (int thisFireball = 0; thisFireball < fireballs; thisFireball++)
  {
    if (fireballDelay == 0);  //  The delay for this Fireball has expired, GO!
    {
      fireball(thisFireball);
    }
  }
  analogWrite (pos1pin, fireballLevel[0]);
  analogWrite (pos2pin, fireballLevel[1]);
}

void fireball(int myFireball) 
{
  if ((fireballDelay[myFireball] == 0) && (fireballLoop[myFireball] > 0)) 
  {
    // fireball is active, loops remaining
    if (fireballSequence[myFireball] == 70) 
    {
      // current LED sequence is done, decrement the loop count, reset seq.
      fireballLoop[myFireball] = ( fireballLoop[myFireball] - 1 );
      fireballSequence[myFireball] = 0;
      if (fireballLoop[myFireball] == 0)
      {
        // Current secuence is done, on the last loop. Roll the dice!
        fireballDelay[myFireball] = (random(250, 950)) ;
        fireballLoop[myFireball] = (random(7, 18)) ;
      }
    }
    else  // we are incrementing the fireball sequence
    {
      fireballSequence[myFireball] = fireballSequence[myFireball]++ ;
    }
    fireballLevel[myFireball] = fireballCycle[fireballSequence[myFireball]];
  }
  else   // fireball is in the delay (off) state
  {
    fireballDelay[myFireball] = ( fireballDelay[myFireball] - 1 ) ;
  }
  delay(10);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead (switchpin);
  if (last != current)
  { 
    delay(50);
    current = digitalRead(switchpin);
  }
  return current;
}

Comments (0)

You don't have permission to comment on this page.