SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By Nakor
#111932
Hello,

I recently received my 2x2 button pad ( http://www.sparkfun.com/commerce/produc ... ts_id=9277 ) but it does not work as I had expected. I'm really new to all of this so I may be just having a stupid day. I had expected the switch pin would return a number from which I could pull what button is being pressed. Obviously I need to do some more reading. Could someone please point me in the right direction here? I would love a tutorial or two that explains how these things work. I have seen and read the keypad Arduino stuff but that doesn't seem to be helping me just yet. I've tried searching this forum (and Google) but I seem to be missing the answer.

I am plugging the pad into an Arduino.

Thanks.

Edit: I forgot to mention that I don't yet have the diodes installed (SparkFun was out when I put my order through).
By Nakor
#111970
I've figured out how to control the LEDs (although not individually). I still haven't figured out how to tell what button is being pressed, or if any button is being pressed at all. I'm thinking that the grounds are somehow supposed to be used for data but I'm not sure how to do that and don't want to fry my board lol.
By Cory
#111978
Looks like we're in the same boat. I just bought my button pad and I can't figure out the buttons. My first thought was this: The Switch pin acted as a pull up resistor, and each button ground would go HIGH when the button was pressed. However, I've tried everything and I can't get them to behave correctly.

The lights, on the other hand, I was able to figure out. I created a Library for them to simplify my base program, but to turn on each light, I did the following:
Code: Select all
void ledColor(int led, int r, int g, int b)
{
  analogWrite(red,r);
  analogWrite(green,g);
  analogWrite(blue,b);
  delay(2);  //Accounts for flickering
  digitalWrite(led,LOW);
  delayMicroseconds(2100);  //Accounts for flickering
  digitalWrite(led,HIGH);
}
Basically, the Arduino loops this code for a period of time to light the LEDs. It sets the RGB pins, grounds the LED, and then sets it HIGH again. The delays are to reduce flicker. Without them (or at different settings) you'll notice the lights behave strangely.

There's surprisingly little documentation available for this kind of thing. I searched everywhere, but I eventually realized I'd have to come up with everything myself. If anyone else has suggestions, I'd be happy to hear them also.
By Nakor
#111995
Hey I'm still working on this but I just wanted to say thanks for posting. Let me know if you figure anything out and I'll do the same :)
By Cory
#112181
I've got the buttons working for the most part, although there is a .5-4 second delay I have yet to solve.

I ran 4 digital pins to the buttons, and grounded each with a 10k Ohm pulldown resistor to keep them from floating. The common SWITCH pin is connected to 5 volts. When a button is pressed, the corresponding input pin should read a HIGH.

As for the delay, I've heard multiple solutions. One is to add foil to the buttons, which provides a faster read but risks shorting the LEDs. The other (and probably the better solution) is to increase the resistance of the pulldown resistor. I can't confirm that this works, but once my new PCB gets here (I messed up the first one A LOT :() I'll test it out.
By Nakor
#112192
Thanks for that post! Because of that I figured out how to get my buttons to work (although I'm still not sure how to selectively light them). I have one button that isn't working but I'm sure I'll figure out what I did wrong (which might have included frying the crap out of it.)

I didn't have the diodes and I wasn't sure if they were necessary so I used regular red LEDs in their stead. It seems to work.
By Cory
#112193
I have one button that isn't working but I'm sure I'll figure out what I did wrong (which might have included frying the crap out of it.)
I was so close to being finished when I fried all my RGB LEDs.
I didn't have the diodes and I wasn't sure if they were necessary so I used regular red LEDs in their stead. It seems to work.
Using LEDs as diodes was a great idea (as long as you put them in the right way, of course :wink:).

To selectively light the LEDs, you need to connect each LED cathode (LED-GNDx) to a digital pin. To turn on an LED, you would set the pin LOW. So to selectively light an LED, you would use analogWrite() to set the values for the red, green, and blue, then use digitalWrite() to ground the LED you want to turn on.

Turning on multiple LEDs with different colors is a bit more complicated. As you can see from one of my previous posts, you have to set the RGB values, delay (to give the LEDs time to adjust, I assume), ground the LED that should be that color, delay again (so it has time to illuminate), and then set the LED pin HIGH again. If you create a loop that does this for each LED, you'll get the illusion of them all being on at the same time!
By Nakor
#112195
For the LED individual control you are configuring the ground pins as input or output? I am guessing they are configured as input and you are turning the internal resistor off to ground it. This in my mind means that you are directing it to the path of least resistance, so how do you have your board set up? I have been trying to do this but haven't managed to get it to work just yet.
By Cory
#112196
You're correct in that it would take the path of least resistance, but you don't have to make it that complex. Just configure the ground pins as outputs and configure them high or low to ground them.

The pullup/pulldown resistors only apply to the buttons. On my circuit the buttons go through the resistors to ground when they're unpressed, and to 5v when pressed (since there's less resistance).
By Nakor
#112199
Ah ok. I was under the impression that I might damage my Arduino if I configure a pin as output and then send power into it.
By Cory
#112204
Output pins can be damaged, but only if you configure them to output, set them to LOW, and then send a 5v flow in (taken from http://www.arduino.cc/cgi-bin/yabb2/YaB ... 1277404149).

That's actually a great question--one that I didn't really consider when I wired my circuit the first time. However, all my LEDs light up fine, so I must have avoided the mistake.

I'm not an expert on all the theory, so you probably shouldn't just take my word for it. The link I posted above seemed to explain the gist of the topic.

EDIT: I almost forgot; since LEDs are diodes, there's no way for the voltage to come back into the pins. So you're safe.
By Nakor
#112207
Thanks. I was doing some reading on short circuits (since I didn't really know what they were lol). Apparently if you connect ground to +5v with no resistance, that is a short. One thing that is protecting me is the fact that an LED is a diode like you said, but also that all of my grounds are going through resistors. I think it is still possible to have a short with the resistors there but it's not likely. If I'm wrong let me know :)
By Nakor
#112208
Your code works well but I'm not happy with the way the LEDs look when I'm lighting all 4 at once. I think tomorrow I will try to use a 555 to improve it. I have one just sitting around. I may be wrong but I'm thinking that would be better because it would shift out simultaneously.

I had been doing output to the serial window as well and I noticed that it was causing a lot of extra flicker (read: strobe). Hopefully a shift register can fix that as well.
By Cory
#112209
You are correct that a short is a direct connection between ground and 5v (or 9v, etc.). However, components such as LEDs resist the flow, preventing a short circuit. There's also a whole other concept called impedance, which is similar to resistance in that it opposes electrical flow.

You generally don't have to be worried about creating a short circuit. Unless you're connecting the VCC line to GND or something similar, you'll be fine.
Your code works well but I'm not happy with the way the LEDs look when I'm lighting all 4 at once.
I'm glad it at least gave you a starting point. I'm using a very primitive (trial and error, basically) way to reduce flicker, but there are better solutions. For example, you might be able to smooth out the flicker with capacitors. Again, that's not something I'm completely familiar with, but ask anybody and they're likely to know. Or, just experiment with the delays! I got 2 LEDs to work great with 3 and 4 one time, and then 2 and 3 with 3 LEDs. It varies.
I think tomorrow I will try to use a 555 to improve it.
There's no need for an analog device when you have a programmable microcontroller. You could use one if you wanted, but I'm not sure it would help solve your problem (except maybe for the capacitor).
Hopefully a shift register can fix that as well.
How many pins will you need for your project? Shift registers are most often used when I/O pins are limited. Even if you use one, you can only use it for the cathodes (on/off), and that will only save you one pin. With that said, it would be good practice if you want to become more familiar with them.
By Nakor
#112210
Yeah I want to practice with the shift register just to get the hang of it. I understand it fairly well I think but I still need to try using one.

I was thinking it would help reduce flicker because everything is set high or low at the same time. Using your code I am doing the first one, then the second one, then the third one, and finally the fourth one. With a shift register I would be doing all four, then all four, then all four, then all four, and so on.

I think you are correct about the delays though. I was thinking about that as well. I could have a different delay set depending on how many LEDs are being lit. That would probably work. I may play with that as well.