chebe: (Default)
[personal profile] chebe
I can't help it, I think LEDs are pretty. So what could be better than a tshirt with 49 of them? Well, there's a lot to figure out before I get that far. Let's start with a more reasonable number, say 9, that's a 3x3 matrix. Yes, that's doable. But let's not make it too easy, let's try using the sewing machine, and user input. Okay, ready?

I could make each of the LEDs individually addressable, that would mean at least 9 ports/slots/petals of the Lilypad would be needed. But this doesn't scale well, for the 49 LED project I'd be in trouble. Instead making use of the matrix/grid technique will be more useful. The 9 LEDs will be laid out in a 3x3 grid, meaning I'll need 6 petals. 49 will be 7x7, needing only 14. Just about doable.

The idea of a matrix can be hard to comprehend, but once you get it it's worth the effort. Each LED has a positive and negative lead, both of which must be connected (with electricity flowing in the correct direction) for the circuit to complete and the LED to light up. We take the LEDs, lay them out in a 3x3 grid, and this fits very naturally. I've set the rows to be the positive connections, and the columns to be the negative. Each LED must be attached to one row, and one column, only. So, if I turn on the power to the positive, and off the power at the negative, the current can flow, completing the circuit and turning on the LED. Each LED is individually addressable in this way, but turning on multiple LEDs at once can result in unwanted LEDs turning on, so I have to be careful. (This is because when you turn a row on, you turn all of it on. More later.)

Construction:
Okay, we have the theory, now let's construct the circuit. Physically making this 3x3 circuit took me about four hours, once I got it right. My first attempt didn't work out too well. I decided to try using the sewing machine to speed up the process. I'm using 4-ply silver conductive thread (which has a lower resistance than the 2-ply, but is thicker, more likely to fray, and more difficult to use in a machine) so my only option was to wind it on a bobbin, and use regular thread as the top thread. (This turns out to be the same as the way I was doing it by hand, funny coincidence.) You have to loosen the tension of the top thread so it doesn't fight with the stronger conductive thread and keep breaking on you.



The rows will be laid on the front of the fabric, and the columns on the back. (Remember, the conductive thread is in your bobbin, so will be on the underside of the fabric as you're sewing it.)

Turns out that your choice of fabric is very important. I used just regular cotton. Which is 'weaker' than the conductive thread. What I mean by that is that the top thread seemed to pull the conductive thread through the fabric at intervals, leaving that section open to connecting with the other trace and causing a short. There are two options; use a thicker fabric, or at each junction stop the machine, lift the presser foot, and pull the fabric through to just past the junction, lower the foot and continue sewing. This ensures the conductive thread stays on it's own side when it's most important. I chose the second option.

We have to make sure we leave enough conductive thread at the ends of the traces to allow us to connect it up to the Lilypad microcontroller. This last bit is done by hand. As you go along check with a multimeter that you haven't any crossed lines. No two traces should conduct yet.

Next, take the LEDs, and with a pliers curl the legs. Make sure to leave some way of identifying which is the positive lead. I left a little piece of leg sticking out straight. Then place where two traces cross, hand stitching the positive leg to the positive trace, and the negative to the negative through the fabric. Be really careful not to cross paths; don't stitch too close to the other one, remove any loose hairs, and use something to stop the ends fraying (I used clear nail polish because it dries quickly). Check with the multimeter that the connections are good and only the intended LED lights up.

Now we have something that looks like this:

Front.


Back.

I haven't added a power source because I only ever intend to use this while the Lilypad is connected directly to the computer. Speaking of which, now we're at the programming stage.

Programming:
My positives are connected to a0, a1, and a2. For now I'm only using them as digital ports, so their numbers are 14, 15, and 16. These are my row0, row1, and row2 respectively. My negatives are 7, 9, and 11, and are column2, column1, and column0 respectively. In setup() I set all to OUTPUT.
void setup()
{
  Serial.begin(9600);
  pinMode(row0, OUTPUT); 
  pinMode(row1, OUTPUT); 
  pinMode(row2, OUTPUT); 
  pinMode(column0, OUTPUT); 
  pinMode(column1, OUTPUT); 
  pinMode(column2, OUTPUT); 
  
  allOff();
}


I created a function called allOff(), that does what it says on the tin. This works based on the fact that the LEDs will only light up if the power is flowing in the correct direction. That means the positive/row must be +/HIGH, and negative/column must be -/LOW. To turn off, reverse.
void allOff()
{
  digitalWrite(row0, LOW);
  digitalWrite(row1, LOW);
  digitalWrite(row2, LOW);
  digitalWrite(column0, HIGH);
  digitalWrite(column1, HIGH);
  digitalWrite(column2, HIGH); 
}


Now play around with the LEDs, turn them on, make patterns, have fun. To turn on one LED:
void on00()
{
  digitalWrite(column0, LOW);
  digitalWrite(row0, HIGH);
}

Depending on what I want I may want to call allOff() just before calling on00() (i.e. if I only want that one LED at row0, column0 on).
void rowStripe()
{
  on00();
  on01();
  on02();
  delay(100);
  allOff();
  on10();
  on11();
  on12();
  delay(100);
  allOff();
  on20();
  on21();
  on22();
  delay(100);
  allOff();
  delay(500);
}


When I got around to trying to draw an X I found that I could turn on the four corners at the same time no problem. But when I tried to turn on the centre one as well, all nine lit up. That is because when I turned it on all three rows were now HIGH, and all three columns were LOW. To manage this effect we need to trick the human eye. The LEDs are capable of flicking on and off much faster than we can see, so we exploit that. But, because of the downtime this results in the LEDs appearing less bright. Have a look at the video to see what I mean.
void X()
{
  allOff();
  on00();
  allOff();
  on11();
  allOff();
  on22();
  allOff();
  on20();
  allOff();
  on11();
  allOff();
  on02();
}


And finally, now that I have all these different patterns I want to be able to choose between them. So I set up the program to take user input, and behave differently based on it. Perfect use of the switch statement.
void loop()
{
  if(Serial.available() > 0)
  {
    inChar = Serial.read(); //read in from text box in Serial Monitor

    switch(inChar)
    {
      case '1':
        chaseSequence();
        allOff();
        break;
      case '2':
        spin();
        spin();
        allOff();
        break;
      default:
        int x=0;
        //keep on for about 5 seconds
        for(x=0; x<10000; x++){
          X();
        }
        allOff();
        break;
    }
  }
}


And that's all folks!



Things I Learned:
1. That to use 4-ply conductive thread in a sewing machine it needs to be in the bobbin.

2. That your choice of fabric is very important. If it is thin/light you need to skip a couple of stitches at the point where two traces cross, to prevent possible shorting.

3. That I can use user input through the computer to affect Arduinos through Serial.read().
Page generated 2014-Oct-20, Monday 09:38 am
Powered by Dreamwidth Studios