I was trying to study AVR programming using an Arduino. I just wanted to blink some LEDs. At first I succeeded then I tried using a push button for it. It didn't work out. I tried using a switch debouncing circuit. It didn't work out.
Please help me find out why is it not working.
The LED is not glowing when I connect the connection to the push button and press it. It works when it is connected directly to 5V. Please help me out here. What am I doing wrong?
#include <avr/io.h>
#include <util/delay.h>
int main (void) {
DDRD &= (0<<DDD5);
DDRD |= (1<<DDD2) | (1<<DDD3);
if (PIND & (1<<PIND5) )
{ _delay_ms(500);
PORTD |= (1 << PORTD2);
PORTD |= (1 << PORTD3);
}
else{
_delay_ms(500);
PORTD &= (0<<PORTD2);
PORTD &= (0<<PORTD3);
}
}
This was without the debounce circuitry:
this was without the debounce circuitry
It doesn't work in either case:
1 Answer 1
Maybe you need to put this part of the
if (PIND & (1<<PIND5) )
{ _delay_ms(500);
PORTD |= (1 << PORTD2);
PORTD |= (1 << PORTD3);
}
else{
_delay_ms(500);
PORTD &= (0<<PORTD2);
PORTD &= (0<<PORTD3);
}
program inside a while(1){..} loop.
At least on my compiler the processor freezes in an infinite loop when it "returns" from main() so it would only execute once after reset. Also you probably should set the F_CPU to the right number for the Uno if you want the delays to be accurate.
+
... press button \$\endgroup\$