Programming the glasses

In order to make the Trinket board so small, the serial to USB conversion is left out from the design. On a regular Arduino board, this conversion is handled by another Atmega chip, and on older versions this was done by an FTDI (Future Technology Devices International) chip. The FTDI chips are still around and you can buy these as standalone breakout boards as shown in Figure 3.8 to the left of the Trinket board:

Programming the glasses

Figure 3.8: The FTDI serial to USB converter and the Trinket board

Normally, you solder male pins to the end of the Trinket board that connects to the FTDI converter, but in this case we want to keep the Trinket board as flat as possible and we don't want sharp pins on the inside of the glasses that might hurt your eyes. So the trick is to just attach the male pin headers to the FTDI converter and hold it in place while programming the Trinket board. Once in a while there will be glitches in the connection and the upload will fail. This is probably due to the FTDI not connecting properly to the Trinket board. However, this is not a big problem since you can just start the upload over again while making sure the pins have a good connection.

Now let's make a sketch that checks that all of the LEDs light up. In order to do so, we will loop through the LEDs one by one to see that everything works as it is supposed to. The Trinket is programmed as a normal Arduino. Uno board, so make sure you select this type in the board menu Upload the following code and check the LEDs in front of the glasses:

/*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[]={
  3,4,5,6,8,9,14,16,17,18,19};
/*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[]={
  10,11,12};

void setup(){
  /*In order for the matrix to work we need to be able to control our gnd lines in the matrix. The trick is to use all pins as output. When we turn the gnd pins HIGH we will be able to block the ground connection*/
  for(int i=0; i<20;i++){
    pinMode(i,OUTPUT);
  }
  //Turn all the gnd pins HIGH in order to keep all LEDs off
  for(int j=0;j<3;j++){
    digitalWrite(gndPins[j],HIGH);
  }
}
void loop(){
  //Run the function
  looper();
}

void looper(){
  /*In this function we run through all the LEDs using two for loops starting by opening a gnd connection*/
  for(int i=0; i<11;i++){
    digitalWrite(powerPin[i],HIGH);
    //Once a gnd pin is accessible we turn on one of the LEDs
    for(int j=0;j<3;j++){
      digitalWrite(gndPins[j],LOW);
      delay(50);
      digitalWrite(gndPins[j],HIGH);
      delay(50);
    }
    digitalWrite(powerPin[i],LOW);
  }
 //Loop everything backwards
    for(int i=10; i>=0;i--){
    digitalWrite(powerPin[i],HIGH);
    for(int j=3;j>=0;j--){
      digitalWrite(gndPins[j],LOW);
      delay(50);
      digitalWrite(gndPins[j],HIGH);
      delay(50);
    }
    digitalWrite(powerPin[i],LOW);
  }
}

In this example sketch, we are implementing a trick in order to be able to control the ground connections. If we connected the negative rows of the matrix straight to GND we would not be able to control the separate LEDs. The trick is to use normal pins as outputs. When a pin is LOW, it connects to ground, which we can use to light up our LEDs in the matrix. But once it turns to HIGH, we block the connection to the ground. So now we can control each LED individually by turning one of the positive columns HIGH and one of the negatives rows LOW. You will need to make sure that your pin declarations line up with the actual physical layout in your glasses or else looping through them could get very hard. As you can see in the schematic in Figure 3.3, the columns are connected after one another to the digital pins.

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

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