Monday, May 13, 2013

Really trying to figure this out.

The kids, completely on their own, booted up the laptop, hooked up the circuit from Saturday, and wanted to tinker with their code. They were struggling a bit, wanting to do a bunch of fading in and out, so rather than continuing to do the overly complicated, nested, loops I helped them out with a "fade in" and "fade out" functions. They only accept 1 argument and that is the pin number. It assumes that the cathode is already set to LOW. Here's the functions:

void fadein(int ipin){
  for(int strength=1;strength<=255;strength++){
    analogWrite(ipin,strength);
    delay(10);
  }
}
void fadeout(int ipin){
  for(int strength=255;strength>=0;strength--){
    analogWrite(ipin,strength);
    delay(10);
  }
}      

After the kids went to bed, I rewired the LED Matrix. I think I figured out a few things. The first thing a figured out was why I couldn't lower the value of my LEDs with an analogWrite(). I didn't realize that only 14 of the 54 pins on my Arduino board can be used for PWM (Pulse Width Modulation). LEDs simply require a certain a minimum voltage and they light up. Less voltage does not make it light up less, just as more voltage will not make it brighter. The only way to make an LED brighter or dimmer is to flicker them off and on. That's what PWM does. I'll let the Arduino site give the technical explanation. My Arduino Mega  can only do PWM on pins 2-13, 45, and 46.

When I rewired my LED matrix, I made sure that the rows were attached to PWM pins. Now, when I set a red column to LOW. If I do analogWrite() with different values, I can make it different levels of brightness. Next, I want to try blending the colors. This is when I realized what was meant by the term "Common Anode," which is the type LED matrix I have. This term simply means that all of the colors in a given row are powered at the same time. By enabling the ground on the column of the desired color whatever PWM signal is sent through that row will apply to all grounded colors in that row. So how can I blend the colors?

The only thing that I can figure out is to:

  1. ground red "digitalWrite(LOW)" 
  2. power the row with the appropriate PWM pulse "analogWrite(pin,value). 
  3. Remove the ground from red "digitalWrite(HIGH)" 
  4. set the row back to ground "analogWrite(pin,0) 
Then do that for green and blue. Whew!

It kinda works for 1 LED, but it is not very bright. Adding a 1 millisecond delay between the colors helped brighten them. Here's the real problem, more than one LED makes it flicker, beyond belief. I think I'm going to have to figure out how shift registers work. I had assumed that the reason that all of the examples that I have found used shift register because the Unos didn't have enough pins. I didn't think it'd be necessary since the Arduino Mega has so many pins. Maybe it's my code. Who knows?

No comments:

Post a Comment