HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Pausing a loop with a Raspberry Pi 3

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withraspberrylooppausing

Problem

I am trying to perform a loop and then stop the loop when the last line of code in the if statement is printed to screen saving on CPU cycles.

I want to start a while loop and then kill the loop after it is complete, but not breaking out of the loop so I can re-initiate the loop once again by pressing a GPIO button "B1".

My end goal is to have the RGB LED cycle and not continue to loop once it is complete.

Setup: RPi 3, RGB LED, button and breadboard.

```
#include
#include
#include
#include

#define LED_RED RPI_GPIO_P1_12 //GPIO 18
#define LED_GRN RPI_GPIO_P1_16 //GPIO 23
#define LED_BLU RPI_GPIO_P1_18 //GPIO 24
#define B1 RPI_GPIO_P1_22 //GPIO 25

int main(int argc, char **argv)
{
if (!bcm2835_init()) return 1;
bcm2835_gpio_fsel(LED_RED, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(LED_GRN, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(LED_BLU, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(B1, BCM2835_GPIO_FSEL_INPT);
bcm2835_gpio_set_pud(B1, BCM2835_GPIO_PUD_UP);

bcm2835_gpio_len(B1);

unsigned int delay = 1000;
while (1){
printf("Light cycle machine\n");

if(bcm2835_gpio_eds(B1)!=0){

printf("Button Pressed!!!\n");
printf("\n");
bcm2835_gpio_set(LED_RED);
printf("Red LED on!!!\n");
bcm2835_delay(delay);
bcm2835_gpio_clr(LED_RED);
printf("Red LED off!!!\n");
bcm2835_gpio_set(LED_GRN);
printf("Green LED on!!!\n");
bcm2835_delay(delay);
bcm2835_gpio_clr(LED_GRN);
printf("Green LED off!!!\n");
bcm2835_gpio_set(LED_BLU);
printf("Blue LED on!!!\n");
bcm2835_delay(delay);
bcm2835_gpio_clr(LED_BLU);
printf("Blue LED off!!!\n");
delay(delay);
bcm2835_gpio_set_eds(B1);
printf("\n");
printf("System Idle\n");
}

else{

delay(delay);

Solution

Naming

Keep your #define constant names consistent. You specify that LED_RED, LED_GRN, and LED_BLU are LEDs, but you don't specify what B1 is. A better name would be BTN_B1.

Bracing

Keep your bracing consistent. Stick to one indentation style. For example in your code, you have both

if(...){
...
}


and also

if (...)
{
...
}


Delay Variable

Use the const keyword if you're not going to be changing delay.

Main Args

If you're not going to be using int argc, char **argv, then they don't need to be there.

Use Functions

The first 7 lines of main are setting up the GPIO pins, you should put that into a setup() function.

Make good use of whitespace

The long block of statements inside the if(bcm2835_gpio_eds(B1)!=0) would be better off separated by some spaces, to make it more readable.

Resulting Code

#include 
#include 
#include 
#include 

#define LED_RED RPI_GPIO_P1_12 //GPIO 18
#define LED_GRN RPI_GPIO_P1_16 //GPIO 23
#define LED_BLU RPI_GPIO_P1_18 //GPIO 24
#define BTN_B1 RPI_GPIO_P1_22 //GPIO 25

void setup_gpio(void);

int main()
{
    if (!bcm2835_init()) return 1;
    setup_gpio();

    unsigned int delay = 1000;
    while (1) {
        printf("Light cycle machine\n");

        if (bcm2835_gpio_eds(BTN_B1)!=0) {
            printf("Button Pressed!!!\n\n");

            bcm2835_gpio_set(LED_RED);
            printf("Red LED on!!!\n");
            bcm2835_delay(delay);
            bcm2835_gpio_clr(LED_RED);
            printf("Red LED off!!!\n");

            bcm2835_gpio_set(LED_GRN);
            printf("Green LED on!!!\n");
            bcm2835_delay(delay);
            bcm2835_gpio_clr(LED_GRN);
            printf("Green LED off!!!\n");

            bcm2835_gpio_set(LED_BLU);
            printf("Blue LED on!!!\n");
            bcm2835_delay(delay);
            bcm2835_gpio_clr(LED_BLU);
            printf("Blue LED off!!!\n");

            delay(delay);
            bcm2835_gpio_set_eds(BTN_B1);
            printf("\nSystem Idle\n");
            }
        else {
            delay(delay);
            //printf("Done!\n");
            }
        }

    return 0;
}

void setup_gpio()
{
    bcm2835_gpio_fsel(LED_RED, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(LED_GRN, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(LED_BLU, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(BTN_B1, BCM2835_GPIO_FSEL_INPT);

    bcm2835_gpio_set_pud(BTN_B1, BCM2835_GPIO_PUD_UP);
    bcm2835_gpio_len(BTN_B1);
}


As for your question, you say you want to kill the loop but not break out of it. I'm not exactly sure what you mean. But, perhaps you're looking for interrupts?

Code Snippets

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <bcm2835.h>

#define LED_RED RPI_GPIO_P1_12 //GPIO 18
#define LED_GRN RPI_GPIO_P1_16 //GPIO 23
#define LED_BLU RPI_GPIO_P1_18 //GPIO 24
#define BTN_B1 RPI_GPIO_P1_22 //GPIO 25

void setup_gpio(void);

int main()
{
    if (!bcm2835_init()) return 1;
    setup_gpio();


    unsigned int delay = 1000;
    while (1) {
        printf("Light cycle machine\n");

        if (bcm2835_gpio_eds(BTN_B1)!=0) {
            printf("Button Pressed!!!\n\n");

            bcm2835_gpio_set(LED_RED);
            printf("Red LED on!!!\n");
            bcm2835_delay(delay);
            bcm2835_gpio_clr(LED_RED);
            printf("Red LED off!!!\n");

            bcm2835_gpio_set(LED_GRN);
            printf("Green LED on!!!\n");
            bcm2835_delay(delay);
            bcm2835_gpio_clr(LED_GRN);
            printf("Green LED off!!!\n");

            bcm2835_gpio_set(LED_BLU);
            printf("Blue LED on!!!\n");
            bcm2835_delay(delay);
            bcm2835_gpio_clr(LED_BLU);
            printf("Blue LED off!!!\n");

            delay(delay);
            bcm2835_gpio_set_eds(BTN_B1);
            printf("\nSystem Idle\n");
            }
        else {
            delay(delay);
            //printf("Done!\n");
            }
        }

    return 0;
}

void setup_gpio()
{
    bcm2835_gpio_fsel(LED_RED, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(LED_GRN, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(LED_BLU, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(BTN_B1, BCM2835_GPIO_FSEL_INPT);

    bcm2835_gpio_set_pud(BTN_B1, BCM2835_GPIO_PUD_UP);
    bcm2835_gpio_len(BTN_B1);
}

Context

StackExchange Code Review Q#132449, answer score: 5

Revisions (0)

No revisions yet.