Thread: Need help with making LED blink then button is pressed

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    3

    Need help with making LED blink then button is pressed

    Hey, im super new to coding so sorry for this noob question. I have the arduino and im coding in C.
    I simply cant figure out why my code isnt working.

    Code:
    #define F_CPU 16000000UL
    #include <avr/io.h>
    #include <util/delay.h>
    
    
    
    
    void setup(void);
    void blink(void);
    
    
    
    
    int main(void)
    {
    
    
        setup();
        
        while (1) 
        {
            if(PIND0 == 1)
            {
                blink();
            }
        }
    }
    void setup(void)
    {
        DDRB = 0x3f;
        DDRD = 0x00;
    }
    void blink(void)
    {
        PORTB ^=0x3f;
        _delay_ms(250);
    }
    Hopefully you guys can help
    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You do realise you need to send PIND0 to logic 1 to make something happen.

    Then there is the question of which pin your LED is attached to on port B.
    You seem to be blasting away at 6 pins all at the same time in a hit-or-miss approach.

    Not to mention, have you got the LED the right way round? If you get them the wrong way round, nothing happens.

    Have you tried a much simpler program of say
    Code:
    setup();
    PORTB =0x3f;
    You need to make this work before moving onto making it blink.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    3
    Thanks for the input!
    Let me clear a few things up.
    My LED is actually multiply LEDS, i have one in each port on PORTB.
    All the LEDS works with the code you describe above, my problem is that i want a press of a button to affect when the LEDs light up.
    If i need to explain further, please let me know :P

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Are you sure you have your switch wired to pin D0 ?

    Is the other end of the switch wired to +5v or ground?

    Do you need a pull-up, or pull-down resistor in series with the switch and pin D0?

    Have you tried if(PIND0 == 0) in case you wired up your switch to pull the logic level of D0 to ground?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2016
    Posts
    3
    My button is connected to +5V, i just tried with PIND == 0 and connected my button to ground, but all i achieved was constant blinking
    I might need a pull down resistor, so i have to look into that i guess.
    Thanks you for the input so far
    Last edited by muhaitio; 11-13-2016 at 02:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [PDcurses] Handle pressed button
    By netpumber in forum Windows Programming
    Replies: 1
    Last Post: 05-08-2012, 01:49 PM
  2. Drawing a bitmap when a button is pressed
    By h3ro in forum Windows Programming
    Replies: 5
    Last Post: 08-05-2008, 11:41 AM
  3. Replies: 2
    Last Post: 06-28-2008, 08:30 PM
  4. how do i check which button is pressed ?
    By intruder in forum Windows Programming
    Replies: 2
    Last Post: 04-24-2006, 03:24 PM
  5. How do i make out which button is pressed ?
    By intruder in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2006, 12:15 PM

Tags for this Thread