0
\$\begingroup\$

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:

both case its not working

JRE
75k10 gold badges115 silver badges197 bronze badges
asked Nov 12, 2022 at 17:21
\$\endgroup\$
2
  • \$\begingroup\$ How have you debugged if the pushbutton is the correct way around or correctly inserted and makes a good connection? The code has problems too, but for the purpose of doing what it is supposed to, it will work. \$\endgroup\$ Commented Nov 12, 2022 at 18:13
  • \$\begingroup\$ test the switch ... remove the yellow wire ... move the switch to the resistor where the yellow wire was ... connect the other side of the switch to + ... press button \$\endgroup\$ Commented Nov 12, 2022 at 20:27

1 Answer 1

1
\$\begingroup\$

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.

answered Nov 12, 2022 at 20:27
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.