is the baby orangutan 168 internally pulled high or low? Would I be better off pulling the inputs high or low?
The reason I ask is because I have a switch connected to ground. Most of the time when the switch is off my program reads the switch as off. On occasion (every 30 seconds to few minutes) the program will misread the input and do the activities for when the switch is on. Should I just read the port 4 times and figure out what reading occurs most often or should I connect the switch to +5 volts?
The mega168 digital inputs are high-impedance (floating) by default, but they can be configured so that they are weakly pulled to 5V internally. You should definitely use these internal pull-ups (or make your own external pull-up or pull-down) when you connect this line to something that alternates between a voltage and disconnected, since the pin can read anything during the latter state if it’s not being pulled to a default voltage. My suggestion is to enable the internal pull-up on your switch input and have the switch connect the pin to ground. You can enable the internal pull-up with code such as the following:
DDRD &= ~(1 << PD0); // make pin PD0 an input
PORTD |= (1 << PD0); // enable internal pull-up on PD0
You should clear the pin’s DDR bit to make it an input. If you then set the pin’s PORT bit, the internal pull-up is enabled. If it’s port bit is zero, the input is high-impedance and you cannot reliably predict what it will read if you aren’t putting an external voltage on it.