NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Help Getting Started on Project

December 27, 2011
by wadaltmon
wadaltmon's Avatar

Hello Comunity,

I have been working on a small project using the ATMEGA168. I am using the pushbutton and an LED, and have the pushbutton 5AC pin hooked up to PD2, the 125NO pin hooked up to the ground rail, and the LED hooked up to PB2 and the ground rail.

The point of the program is that if you press the button for less than 0.5, let off of the button for less than 0.5 seconds, then press it again, the LED will stay on as long as the second button press is held. Once you release the second button press after all the conditions are met, the LED turns off and the cycle begins again.

My problem is that I haven't the slightest idea how to go about this. I have only just made a program that will just turn the light on and off when I press the button, but that doesn't seem to be working either. Here is the code to said program:

#define PINMQ  (1<<PD2)   
#include <avr/io.h>

int main()
{
    PORTC |= PINMQ;
    DDRB |= 0xFF;
    while (1)
    {
        if (PINC & PINMQ)
        {
            PORTB |= (1<<PB2);
        }
        else
        {
            PORTB &= ~(1<<PB2);
        }

    return 0;
    }
}

So at this point, I don't know where to go. Please help.

Regards,

Dalton

December 27, 2011
by pcbolt
pcbolt's Avatar

Dalton -

Unless I'm missing something, you're button is hooked up to PD2 but you're checking PINC for input. I think you want PIND. Also, you may have to set PD2 as an input by using "DDRD &= ~(PINMQ)and not setting it as output at line 6 above.

December 28, 2011
by wadaltmon
wadaltmon's Avatar

Pcbolt -

You're not missing anything! In fact, I never noticed that, and probably never would have. Thank you.

And yeah, I was a little shaky on the setting as input vs. output. Thank you.

-Dalton

December 28, 2011
by wadaltmon
wadaltmon's Avatar

OK, here is my new code.

    #define MYPIN  (1<<PD2)   
    #include <avr/io.h>

        int main(void)
        {
            DDRD &= ~(MYPIN) 
            DDRB |= 0xFF;
            while (1)
            {
                if (PIND & MYPIN)
                {
                    PORTB |= (1<<PB2);
                }
                else
                {
                    PORTB |= ~(1<<PB2);
            }

        return 0;
        }
    }

Yet, I am still getting an error: line 7, called object '4' is not a function. Can anyone shed any light on this?

December 28, 2011
by pcbolt
pcbolt's Avatar

I would first try to change "int main(void)" to just "int main()". Also, (and this should not be the source of a compile error) on line 16, I think you want PORTB &= ~(1<<B2) not |= ,. The last thing (and this is really just for future reference) line 7 should be DDRB |= (1<<PB2); Your code will work, it just sets all the other pins on Port B. Good luck.

December 28, 2011
by wadaltmon
wadaltmon's Avatar

Pcbolt-

I changed all the things you told me, and still got an error. Finally figured out that I was missing a semicolon in line 6.

Thank you for all the help!

-Dalton

December 29, 2011
by pcbolt
pcbolt's Avatar

Dalton -

Ain't that the way it always goes... the smallest things. Glad to see things are progressing. This forum is pretty good for getting advice. I'm still wrestling with some of the non-programming aspects of dealing with MCU's and reading the forum is a huge help. Some contributors are wizards in one aspect and wanting in another. I'm no different so I might need advice from you in the future.

December 29, 2011
by wadaltmon
wadaltmon's Avatar

Hey, I may be able to help. This is my first time dealing with the programming MCUs, but I am incredibly good with overall digital electronics. You just tell me what you need help with, and I will see what I can do.

Post a Reply

Please log in to post a reply.

Did you know that reading a double floating point variable with scanf requires "%lf" for "long float"? Learn more...