TinyCircuits

TinyDuino Sparkler

TinyDuino Sparkler
« on: February 21, 2014, 08:00:12 PM »
Make the TinyShield 16 Edge LEDS sparkle. Through each loop a LED is chosen at random and its on-time delay also chosen at random. http://www.youtube.com/watch?v=rwu3iNypoQ0.

The code below is slightly modified code from the TinyDuino Darkness Guage by @tamberg (thank you).

Code: [Select]
// Sparkler (TinyDuino plus TinyShield 16 Edge LEDS) Feb 2014 robrlstn
// One LED at a time choosen at random and on-time rnadom delay
// Majority of code is from TinyDuino Darkness Guage by @tamberg, 14.04.2013
// on TinyCircuits TinyCircuits Products User Projects / Code Examples


int wait[5] = {60,40,20,10,5};
void setLedRaw (int px, int wx, int py, int wy,
  int m5, int m6, int m7, int  m8, int m9)
{
  digitalWrite(px, wx);
  digitalWrite(py, wy);
  pinMode(5, m5);
  pinMode(6, m6);
  pinMode(7, m7);
  pinMode(8, m8);
  pinMode(9, m9);     
}

void setLed (int i) {
  switch (i) {
    case 1: setLedRaw(5, HIGH, 6, LOW, OUTPUT, OUTPUT, INPUT, INPUT, INPUT); break;
    case 2: setLedRaw(5, LOW, 6, HIGH, OUTPUT, OUTPUT, INPUT, INPUT, INPUT); break;
    case 3: setLedRaw(5, HIGH, 7, LOW, OUTPUT, INPUT, OUTPUT, INPUT, INPUT); break;
    case 4: setLedRaw(7, HIGH, 5, LOW, OUTPUT, INPUT, OUTPUT, INPUT, INPUT); break;
    case 5: setLedRaw(6, HIGH, 7, LOW, INPUT, OUTPUT, OUTPUT, INPUT, INPUT); break;
    case 6: setLedRaw(7, HIGH, 6, LOW, INPUT, OUTPUT, OUTPUT, INPUT, INPUT); break;
    case 7: setLedRaw(6, HIGH, 8, LOW, INPUT, OUTPUT, INPUT, OUTPUT, INPUT); break;
    case 8: setLedRaw(8, HIGH, 6, LOW, INPUT, OUTPUT, INPUT, OUTPUT, INPUT); break;
    case 9: setLedRaw(5, HIGH, 8, LOW, OUTPUT, INPUT, INPUT, OUTPUT, INPUT); break;
    case 10: setLedRaw(8, HIGH, 5, LOW, OUTPUT, INPUT, INPUT, OUTPUT, INPUT); break;
    case 11: setLedRaw(8, HIGH, 7, LOW, INPUT, INPUT, OUTPUT, OUTPUT, INPUT); break;
    case 12: setLedRaw(7, HIGH, 8, LOW, INPUT, INPUT, OUTPUT, OUTPUT, INPUT); break;
    case 13: setLedRaw(9, HIGH, 7, LOW, INPUT, INPUT, OUTPUT, INPUT, OUTPUT); break;
    case 14: setLedRaw(7, HIGH, 9, LOW, INPUT, INPUT, OUTPUT, INPUT, OUTPUT); break;
    case 15: setLedRaw(9, HIGH, 8, LOW, INPUT, INPUT, INPUT, OUTPUT, OUTPUT); break;
    case 16: setLedRaw(8, HIGH, 9, LOW, INPUT, INPUT, INPUT, OUTPUT, OUTPUT); break;
    default: setLedRaw(5, LOW, 6, LOW, OUTPUT, OUTPUT, INPUT, INPUT, INPUT); // off
  }
}

void setup () {
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(9, INPUT);
}

void loop () {
  int i = random(0,17);
  int RandNum2 = random(0,5);
  setLed(i);
  delay(wait[RandNum2]);
}