1) You can attach a button / switch to an arduino without an external resistor. You connect one side of the button to ground, the other to the digital input. Set the pinMode to input, then turn on the internal pullup resistor using the digitalWrite function. I like using defines for my pin numbers at the top of my file.
#define SWITCH1 5
pinMode(SWITCH1, INPUT);
digitalWrite(SWITCH1, HIGH); //turn on pullup
Later, when you need to read the switch, just use digitalRead and test for LOW.
if (digitalRead(SWITCH1==LOW))
{
//do something
}
This method simplifies your circuit AND keeps you from running power to all your switches reducing the possibility of shorting your power supply.
2) You can use the analog input pins A0-A5 as digital inputs! Just number them 14-19 and use them for standard digital I/O.
Using the above code, you would simply define SWITCH1 as 14 and connect the switch to pin A0 on the arduino...
#define SWITCH1 14 //A0
pinMode(SWITCH1, INPUT);
digitalWrite(SWITCH1, HIGH); //turn on pullup
Little tips go a long way to maximizing productivity and hardware capability...enjoy!
No comments:
Post a Comment