Making a pattern

In the next code example, we will implement some pattern designs. These patterns are stored in arrays that correspond to the layout of the LEDs in the glasses. We can draw our patterns in code and later loop through the array and activate the LEDs. When the code is formatted as it is in the next sketch, we get a visual repetition of the pattern. A 0 in the array represents a turned off LED in the same position in the matrix and a 1 represent an LED that is turned HIGH:

/*Collect all the positive columns pins in one array. You need to make sure that these pins correspond to the direction you have placed the columns in the glasses*/
int powerPin[]={
  19,18,17,16,14,9,8,6,5,4,3};
/*Collect all the negative row pins in one array. Again make sure they are added in the same order corresponding to the glasses*/
int gndPins[]={
  12,11,10};
//This is a two dimensional array that holds the pattern
int pattern[3][11] = {
  {1,1,1,1,0,0,0,1,1,1,1  },
  {0,1,1,0,0,0,0,1,0,0,1  },
  {0,1,1,0,0,0,0,1,1,1,1  }
  ,
};
//Variable to store the refresh rate on the led display
int refreshRate=200;

void setup(){
  //Declare all pins as outputs
  Serial.begin(9600);
  for(int i=0; i<20;i++){
    pinMode(i,OUTPUT);
  }
  //turn all the gnd ports High to keep them blocked
  for(int j=0;j<3;j++){
    digitalWrite(gndPins[j],HIGH);
  }
}
void loop(){
  //Run the pattern function
  displayPattern();

}
/*Function that runs through all the positions in the pattern array*/
void displayPattern()
{
  for (byte x=0; x<3; x++) {
    for (byte y=0; y<11; y++) {
      int data =pattern[x][y];
    //If the data stored in the array is 1 turn on the led
      if (data==1) {
        digitalWrite(powerPin[y],HIGH);
        digitalWrite(gndPins[x],LOW);
        delayMicroseconds(refreshRate);
        digitalWrite(powerPin[y],LOW);
        digitalWrite(gndPins[x],HIGH);
      }
      //If it is something else turn the led off
      else {
        digitalWrite(powerPin[y],LOW);
        digitalWrite(gndPins[x],HIGH);
      }
    }

  }
}

This sketch implements a two-dimensional array, which is the same as placing an array into an array. As you can see, we have three arrays, and inside each of those arrays we have 11 positions in the first two and eight in the last one, which corresponds to the layout of the matrix. Using the two-dimensional array, we can now fetch the positions of the LEDs similar to x and y coordinates, which is much easier than storing everything in a normal array. If the values are stored in a normal array, we would need to define where each row ends on our own. This could be done using if sentences to check where the row begins, but using a two-dimensional array makes things much easier and makes for better-looking code.

Then the arrays run through the display pattern function, which loops through all the positions in the array. Every time it finds a 1 in the array it turns on the LED corresponding to the position in the actual glasses. It only turns it on for a brief time based on the refresh rate before it turns it off, since we can only have one LED on at a time in the LED matrix. Again, this is where we use the POV phenomenon, looping through all the LEDs very fast so that when we look at the glasses it looks like multiple LEDs are on, though in fact there is only one LED on at a time.

In order to get a better understanding of the code, I would suggest you modify the pattern array by changing which LEDs light up. If you look closely at the array, you might make out that I have tried to spell my initials TO with 1s in the code, which corresponds to LEDs turned on. Try switching the letters for your own initials and upload the code to your glasses.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.38.125