I'm trying to get the hang of adding new sensors and actuators to my $50 bot, and I'm having trouble getting digital pins to work as input. To my understanding, all you have to do is set the pin to input and then call PORT_IS_ON. If the port is high, PORT_IS_ON will return true, and if the port is low it will return false.
So to set pins 0-3 as output and 4-7 as input you would go into SoR_Utils.h and set
DDRD = 0x0F
Then to check if the port is high or low you would just say
if(PORT_IS_ON(PORTD, 4))
{
printf("Port D pin 4 is on\n");
}
else
{
printf("Port D pin 4 is off\n");
}
Now I'm not sure what the problem is, but its just not working. It will never read anything besides "Port D pin 4 is on". I have a button that pulls the pin low, and my multimeter confirms that when I press the button the pin really does go from close to 5V to around 0.02V. I set the internal pull-up resistors on port D I have been able to successfully connect the button to an analog pin and use a2dConvert8bit() to tell me whether the button is pressed or not. If I output the result of a2dConvert8bit() to uart, it shows 255 when the button is not pressed, and close to 0 when the button is pressed so I know the button is working fine and is successfully pulling the pin low. The problem has to be in the code somewhere. But I don't know if the problem is in setting the pins to input/output correctly, if I'm not reading the pin correctly, or if I'm just not getting the pin close enough to 0V to read as "low". I don't know how sensitive that sort of thing is. I imagined that anything below half of 5V would read as low and anything above half of 5V would read as high but I don't have any experience backing this up.