Download‎ > ‎

Chapter 08. POV



Simply copy the code from one of the boxes below and paste it into a new Arduino project window. 

See Chapter 8 for further details on installing and setting up Arduino.



Chapter 8. Persistence of Vision

long period = 400;

int ledPins[6][3] = {{4, 3, 2}, {7, 6, 5}, {10, 9, 8}, {13, 12, 11}, {16, 15, 14}, {19, 18, 17}};

int n = 128;
char *message[6] = {
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000770000000000000",
"11111110022000002200033330004400000000000000000055555500666666600110001100022220003300033000444440000000000000000770000000000000",
"11000000002200022000003300004400000007777000000550000000660000000111001100002200003300033004400000000000000000000770000000000000",
"11111000000220220000003300004400000007777000000555555000666660000111101100002200003300033000444440000000000000000770000000000000",
"11000000000022200000003300004400000000000000000550005500660000000110011100002200003300033000000044000000000000000770000000000000",
"11111110000002000000033330004444400000000000000055555000666666600110001100022220000333330004444440000000000000000770000000000000"
};

void setup()
{
 for (int led = 0; led < 6; led ++)
 {
   for (int color = 0; color < 3; color++)
   {
     pinMode(ledPins[led][color], OUTPUT);
   }
 }
}

void loop()
{
  for (int col = 0; col < n; col++)
  {
    for (int row = 0; row < 6; row++)
    {
      int color = (int)(message[5-row][col] - '0');
      setLed(row, color); 
    }
    delayMicroseconds(period);
    allOff();
    delayMicroseconds(period / 16);
  }
}

void setLed(int led, int color)
{
 digitalWrite(ledPins[led][0], !(color & 1));
 digitalWrite(ledPins[led][1], !(color & 2));
 digitalWrite(ledPins[led][2], !(color & 4)); 
}

void allOff()
{
   for (int led = 0; led < 6; led ++)
 {
   for (int color = 0; color < 3; color++)
   {
     digitalWrite(ledPins[led][color], HIGH);
   }
 }
}




Chapter 8. Test Application

int ledPins[6][3] = {{4, 3, 2}, {7, 6, 5}, {10, 9, 8}, {13, 12, 11}, {16, 15, 14}, {19, 18, 17}};

// 1 - red
// 2 - green
// 3 - yellow
// 4 - blue
// 5 - mauve
// 6 - cyan
// 7 - white


void setup()
{
 for (int led = 0; led < 6; led ++)
 {
   for (int color = 0; color < 3; color++)
   {
     pinMode(ledPins[led][color], OUTPUT);
   }
 }
}

void loop()
{
    for (int row = 0; row < 6; row++)
    {
      for (int color = 0; color < 7; color++)
      {
        allOff();
        setLed(row, color); 
        delay(500);
      }
    }
}

void setLed(int led, int color)
{
 digitalWrite(ledPins[led][0], !(color & 1));
 digitalWrite(ledPins[led][1], !(color & 2));
 digitalWrite(ledPins[led][2], !(color & 4)); 
}

void allOff()
{
   for (int led = 0; led < 6; led ++)
 {
   for (int color = 0; color < 3; color++)
   {
     digitalWrite(ledPins[led][color], HIGH);
   }
 }
}

Comments