Colorful Codes
Colorful Codes

Reputation: 597

How can I make this while loop faster on a microcontroller button?

I am working with a micro controller that has a button A. When I press this button and hold it for 2 seconds, its value becomes 0 and the color turns either blue or green, when I release, its value goes back to 1 but the color stays the same unless it was clicked again and the color changes. The problem is, it shouldn't take someone 2 whole seconds to have to change the led light. What can I do to make the value (either 0 or 1) be read faster?

Here is a snippet of the code in the while loop.


// here are the states for reference. Everything is either a 0 or a 1
const int BUTTON_PRESSED = 0;
const int BUTTON_UNPRESSED = 1;

const int GREEN_LED = 0;
const int BLUE_LED = 1;

    const struct timespec sleepTime = { 1, 0 };

    while (true) {
        Value_Type value;
        // this function get the input of button a when pressed
        GetValue(button_A_fd, &value);
        Log_Debug(
           "Button value (%d)\n", value);

        // Processing the button.

        //Turns LED ON; Button not pressed down
        if (value == BUTTON_UNPRESSED) {
            last_button_state = BUTTON_UNPRESSED;
        } else {

            // if last button state is 1 then now it is being pressed
            if (last_button_state == BUTTON_UNPRESSED) {
                // Flip LEDs
                if (active_led == BLUE_LED) {
                    active_led = GREEN_LED;
                }
                else if (active_led == GREEN_LED) {
                    active_led = BLUE_LED;
                }

                last_button_state = BUTTON_PRESSED;
                // sets the pointer to the 0 bit of the file to write
                lseek(fd_storage, 0, SEEK_SET);
                // write current active led to mutable storage and save
                write(fd_storage, &active_led, sizeof(active_led));
            }
        }
        // Blinking the active LED.
        // reading input only when pressed and turn off other led
        if (active_led == GREEN_LED) {
            // turn off blue led, then turn on green
            SetValue(blue_led_fd, Value_High);
            SetValue(green_led_fd, Value_Low);
            nanosleep(&sleepTime, NULL);
            SetValue(green_led_fd, Value_High);
            nanosleep(&sleepTime, NULL);
        }
        else if (active_led == BLUE_LED) {
            // turn off green led, then turn on blue
            SetValue(green_led_fd, Value_High);
            SetValue(blue_led_fd, Value_Low);
            nanosleep(&sleepTime, NULL);
            SetValue(blue_led_fd, Value_High);
            nanosleep(&sleepTime, NULL);
        }
    }
}



I tried to put GetValue() in several parts of the code to see if it could maybe get a value faster but it did not work. How can I move from here? I hope I shared enough code to understand the problem. Thank you.

Upvotes: 1

Views: 136

Answers (2)

Bill Morgan
Bill Morgan

Reputation: 570

Looks like your code reads the button which is fast, and then immediately sets outputs and sleeps 1 second, sets an output and sleeps another second before going up to check if the button is pressed again.

You should restructure the code to check the state of the button more frequently where you're going to sleep now.

Sleep for shorter periods and check to see if the button is pressed in a loop until your total sleep time has been met or the button state changes.

Upvotes: 1

Akib Rhast
Akib Rhast

Reputation: 671

Upon further inspection, I found these: enter image description here

The above picture is linked from here , it is the spec sheet for your microcontroller.

You want to look at the "Board Pin Map section" from this link to match it with the spec sheet of the chip itself

Upvotes: 1

Related Questions