W
W
Wasya UK2017-11-28 11:45:06
C++ / C#
Wasya UK, 2017-11-28 11:45:06

Why do LEDs themselves change color?

When you press a button, the microcontroller starts the animation of the LEDs, but then they just change one another in a cycle? (I'm new to this issue, please don't scold me too much) It seems that they should be replaced after pressing the button, but after the first press they don't react to it anymore, and work automatically. What am I doing wrong?

void main(void)
{    
    int buttonStatus = 0;
                                     
    // PORT C
    LED_STRIP_PORT &= ~(1<<LED_STRIP_PIN);
    LED_STRIP_DDR |= (1<<LED_STRIP_PIN); 

    while (1)  
    {           
        // check ws2812 animation type by pressing the button
        if (PINB.0 == 1) {                 
            if (buttonStatus < 3) {
                buttonStatus++;
                PORTC.0 = 1;
            } else {
                buttonStatus = 0;
                PORTC.0 = 0; 
            }
        }
    
        switch (buttonStatus) {  
            // first ws2812 animation   
            case 1:
                startFistAnimation(); 
            break;   
            // second ws2812 animation
            case 2:
                startSecondAnimation();
            break;
            // third ws2812 animation
            case 3:
                startThirdAnimation();    
            break;
            // turn off ws2812
            default:
                PORTC.0 = 0; 
            break;
        }
    } 
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2017-11-28
@Smirrom

You don't have any delay in the while (1) loop, so when the button is clicked, the buttonStatus variable has time to increase to three at once. Perhaps this is the problem.

I
Ilia Zhitenev, 2017-11-30
@ilyazh

use interrupts. When there is a change in the state on the leg with the button, you change the color. For example, for each click. In the main loop, remove the color change.
Those. the interrupt is triggered only at the moment the button is pressed and the variable responsible for the color changes there. In the main loop, you only set the color of the LED, depending on the current value of the variable

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question