0

I have problem, microcontroller simply doesnt register when i press button. As it didnt happened. Here is my code.

    #ifndef F_CPU   
    #define F_CPU 1000000UL         
    #endif

    #define bit_is_clear(byte, bit) (!(byte & (1 << bit)))

    #include <avr/io.h>
    #include <util/delay.h>
    #include <avr/interrupt.h>

    typedef enum { false, true } boolean;
    boolean clicked = false;

    int sigurno_pritisnut = 0;

    void pressed();
    void unpressed();

    int main(void)
{
    //LED OUTPUT
    DDRB |= (1 << PINB0) | (1 << PINB1) | (1 << PINB2);

    //LED LOW
    PORTB &= ~((1 << PINB0) | (1 << PINB1) | (1 << PINB2));

    //BUTTON INPUT
    DDRC &= ~(1 << PINC5);

    //BUTTON HIGH
    PORTC |= (1 << PINC5);

    /* Replace with your application code */
    while (1) 
    {
        if(bit_is_clear(PINC, PINC5)){
            sigurno_pritisnut++;
            if(sigurno_pritisnut > 400){
                clicked = !clicked;
                sigurno_pritisnut = 0;
            }
        }

        if(clicked){
            pressed();
        }else{
            unpressed();
        }

    }
}

void pressed(){
    PORTB ^= (1 << PINB0);
    _delay_ms(500);
    PORTB ^= (1 << PINB1);
    _delay_ms(500);
    PORTB ^= (1 << PINB2);
    _delay_ms(500);
    sigurno_pritisnut = 0;
}

void unpressed(){
    PORTB ^= (1 << PINB0);
    _delay_ms(500);
    PORTB ^= (1 << PINB0);
    PORTB ^= (1 << PINB1);
    _delay_ms(500);
    PORTB ^= (1 << PINB1);
    PORTB ^= (1 << PINB2);
    _delay_ms(500);
    PORTB ^= (1 << PINB2);

}

................................................................... I have tried with other buttons, tried with other pins but nothing. ...................................................................

user7585238
  • 63
  • 1
  • 9

1 Answers1

1

The problem seem to be your delay statements. In both pressed and unpressed you wait 3 x 500ms.

In the loop you want to see bit_is_clear 400 times before changing clicked.

So it seems that bit_is_clear must be true in 600 seconds before you change clicked.

Notice that this

    if(clicked){
        pressed();
    }else if(!clicked){
        unpressed();
    }

is the same as

    if(clicked){
        pressed();
    }else
        unpressed();
    }

so in every loop you'll enter one of them.

Further it seems strange that you don't an else here:

    if(bit_is_clear(PINC, PINC5)){
        sigurno_pritisnut++;
        if(sigurno_pritisnut > 500){
            clicked = !clicked;
            sigurno_pritisnut = 0;
        }
    }
    else
    {
        // ... don't you need some code here?
    }

So button works like this

bit_is_clear == true means pressed

bit_is_clear == false means released

you'll need something like:

while (1) 
{
    while (1)  // Loop until a state is changed
    {
        if(bit_is_clear(PINC, PINC5))
        {
             // pressed
             if (clicked)
             {
                 // Same state so just continue...
                 sigurno_pritisnut = 0;
                 continue;
             }
             sigurno_pritisnut++;
             if(sigurno_pritisnut > 400){
                 clicked = 1;
                 sigurno_pritisnut = 0;

                 // State change - break out
                 break;
             }
         }
         else
         {
             // released
             if (!clicked)
             {
                 sigurno_pritisnut = 0;
                 continue;
             }
             sigurno_pritisnut++;
             if(sigurno_pritisnut > 400){
                 clicked = 0;
                 sigurno_pritisnut = 0;
                 break;
             }
         }
    }

    if(clicked){
        pressed();
    }else{
        unpressed();
    }

}

In this way you only call pressed / unpressed when something new has happened.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63