Ok, let's make things interesting.  The following sketch is designed to display a variety of patterns on the P.O.V.'s 7 LED stick, play some noise based on the displayed pattern, and use the microswitch to toggle sound on and off.  You can see a video of the sketch running on the P.O.V. at youtube here .  It pulls together all of the concepts described in the previous pages.  I deliberately left out comments in order to give you a bit of a challenge.  Try to figure out what the code does on your own.  This sketch is not intended as an example of "good programming practices" (you can see that on the Arduino site).    There are still a few patterns remaining that you can add to the sketch yourself.

---------------------------------------------------------------------

//  Flashy POV sketch

//  May 30, 2010, by Curtis Hoffmann

// ThreeStepsOverJapan

#define LEDPIN   7       // Start LED numbering with pin D7
#define NUMLED   7       // LED count
#define SWPIN    6       // Switch on pin d6

#define PAT_1010 0b01010101
#define PAT_232  0b01100011
#define PAT_313  0b01110111
#define PAT_34   0b01110000

#define NORM     0
#define INVERT   1
#define FORWARD  0
#define BACKWARD 1

byte bitmap[] = {

  0b01000001,  // 0  X in
  0b00100010,  // 1
  0b00010100,  // 2
  0b00001000,  // 3
  0b00010100,  // 4
  0b00100010,  // 5
  0b01000001,  // 6
  0b00001000,  // 7  X out
  0b00010100,  // 8
  0b00100010,  // 9
  0b01000001,  // 10
  0b00100010,  // 11
  0b00010100,  // 12
  0b00001000,  // 13
  0b00000000,  // 14  Fill up lights from one end to the other
  0b00000001,  // 15
  0b00000010,  // 16
  0b00000100,  // 17
  0b00001000,  // 18
  0b00010000,  // 19
  0b00100000,  // 20
  0b01000000,  // 21
  0b01000001,  // 22
  0b01000010,  // 23
  0b01000100,  // 24
  0b01001000,  // 25
  0b01010000,  // 26
  0b01100000,  // 27
  0b01100001,  // 28
  0b01100010,  // 29
  0b01100100,  // 30
  0b01101000,  // 31
  0b01110000,  // 32
  0b01110001,  // 33
  0b01110010,  // 34
  0b01110100,  // 35
  0b01111000,  // 36
  0b01111001,  // 37
  0b01111010,  // 38
  0b01111100,  // 39
  0b01111101,  // 40
  0b01111110,  // 41
  0b01111111,  // 42
  0b00000000,  // 43   Diamond
  0b00001000,  // 44
  0b00011100,  // 45
  0b00111110,  // 46
  0b01111111,  // 47
  0b00111110,  // 48
  0b00011100,  // 49
  0b00001000,  // 50
};

boolean play_tone = 0;
long randNumber;

void setup() {
  randomSeed(analogRead(0));
 
  for(char pin = LEDPIN; pin < LEDPIN + NUMLED; pin++) {
    pinMode(pin, OUTPUT);
  }
  pinMode(SWPIN, INPUT);
  digitalWrite(SWPIN, HIGH);
}

void loop() {
   rightleft(5, NORM);
   right(10, FORWARD,  NORM);   // right(count, direction, invert_HIGHLOW)
   right(10, BACKWARD, NORM);   // direction: 0 = left, 1 = right

   rightleft(5, INVERT);   // invert_HIGHLOW: 1 => HIGH = 1, 0 => LOW = 1
   right(10, FORWARD,  INVERT);
   right(10, BACKWARD, INVERT);
      // xin(count, start_addr, stop_addr, direction, invert_HIGHLOW)
   play_pat( 5,  0,  6, FORWARD, NORM);  // "X"
   play_pat( 5,  7, 13, FORWARD, NORM);  // "Kiss"
   play_pat( 5,  0,  6, FORWARD, INVERT);     // Inverted "X"
   play_pat( 5,  7, 13, FORWARD, INVERT); // Inverted "Kiss"
   play_pat( 6, 43, 50, FORWARD, NORM);  // Diamond
   play_pat( 6, 43, 50, FORWARD, INVERT); // Inverted Diamond

   blink_pat(25, PAT_1010);   // blink_pat(count, value_to_display)
   blink_pat(25, PAT_232 );
   blink_pat(25, PAT_313 );
   blink_pat(25, PAT_34  );

   scroll_in(3, NORM);    // scroll_in(count, invert_HIGHLOW)
   scroll_in(3, INVERT);

   rand_pat(200);
}


void rand_pat(int cnt) {
   for(int j1=0; j1<cnt; j1++) {
     display(random(127), 0, 1);
   }
}
  
void play_pat(int cnt, int j_start, int j_stop, boolean dir, boolean inv) {
  for(int j2=0; j2<cnt; j2++) {
    if(j_start < j_stop) {
       for(int j1=j_start; j1<=j_stop; j1++) {
          display(bitmap[j1], dir, inv);
       }
     }
     if(j_stop > j_start) {
         for(int j1=j_stop; j1>=j_start; j1--) {
           display(bitmap[j1], dir, inv);
        }
     }
  }
}

void blink_pat(int cnt, int val) {
  for(int j1=0; j1< cnt; j1++) {
    display(val, FORWARD, NORM);
    display(val, FORWARD, INVERT);
  }
}

void scroll_in(int cnt, boolean inv) {
  for(int j3=0; j3<cnt; j3++) {
      play_pat(1, 14, 42, FORWARD, inv);
      play_pat(1, 42, 14, FORWARD, inv);
      play_pat(1, 14, 42, BACKWARD, inv);
      play_pat(1, 42, 14, BACKWARD, inv);
   }
}

void rightleft(int cnt, boolean inv) {
  for(int j3=0; j3<cnt; j3++) {
     right(1, FORWARD,  inv);
     right(1, BACKWARD, inv);
   }
}

void right(int cnt, boolean dir, boolean inv) {
  for(int j1=0; j1<cnt; j1++) {
     for (int j2=0; j2<NUMLED; j2++) {
        display(0b01 << j2, dir, inv);
     }
  }
}

void display(byte dval, boolean disp_back, boolean invert) {
   byte temp  = dval;
   byte temp2 = 0;
   int  i;
   boolean tripped = 0;
   unsigned long time = millis();
  
   if(invert) {
      temp = ~temp;  // Invert all 1's to 0's and 0's to 1's.
   }
  
   if(disp_back) {  // Flip the number to display from left to right
      for(i = 0; i < NUMLED; i++) {
        temp2 += ((temp >> i) & 0b00000001) * (64/( 0b00000001 << i));
      }
      temp = temp2;
   }
  
   for(i = 0; i < NUMLED; i++) {
      digitalWrite(LEDPIN + NUMLED - 1 - i, (temp >> i) & 0b00000001);
   }

   if(play_tone) {
      tone(14, temp*25 + 100, 100);
   }
  
   while(millis() - time < 70) {
      if(digitalRead(SWPIN) == LOW && ! tripped) {  // If switch D6 closed, toggle SOUND ON
         play_tone =! play_tone;
         tripped =! tripped;
      }
   }
}

---------------------------------------------------------

 

Return to the main Index
An arithmetic sketch