0

I'm following the instructions at http://wiringpi.com/reference/priority-interrupts-and-threads/ using the PI_THREAD and piThreadCreate(myThread) methods and cannot get the LED(s) to blink. Here's the code I'm using:

/*
 * c_pins_01.c
 *  
 * Toggle GPIO pins on/off using threads.
 */

/*
 * includes
 */
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>

/*
 * Global constants and variables.
 */ 

// GPIO pins used 
unsigned char pins[] = { 13, 19, 26, 6, 5 },
                       *p_pins = &pins[0];

#define MAX_PINS(p)     (sizeof(p) / sizeof(p[0]))              

// Delay
#define DELAY   500

/*
 * Pin toggling threads.
 */

PI_THREAD(toggle_pin_13) {
    while(1) {
        digitalWrite(13, LOW);
        delay(DELAY);
        digitalWrite(13, HIGH);
    }
    return(0);
}

PI_THREAD(toggle_pin_16) {
    while(1) {
        digitalWrite(16, LOW);
        delay(DELAY);
        digitalWrite(16, HIGH);
    }
    return(0);
}

/*
 * Main function.
 */
int main(int argc, char **argv) {
    printf("Hello woild from the C programming language!\n");
    printf("Raspberry Pi 3 B board revision: %d\n\n", piBoardRev());

    wiringPiSetupGpio();

    for (int i = 0; i < MAX_PINS(pins); i++) {
        pinMode(pins[i], OUTPUT);
        digitalWrite(pins[i], HIGH);
    }

    int t = piThreadCreate(toggle_pin_13);
    if (t != 0) {
        printf("Thread did not start.\n");
    }

    t = piThreadCreate(toggle_pin_16);
    if (t != 0) {
        printf("Thread did not start.\n");
    }

/*
    while(1) {
        for (int i = 0; i < MAX_PINS(pins); i++) {
            digitalWrite(pins[i], LOW);
        }
        delay(DELAY);
        for (int i = 0; i < MAX_PINS(pins); i++) {
            digitalWrite(pins[i], HIGH);
        }
        delay(DELAY);
    }
*/  

    return(0);
} /* End main() */

And it's not working. The program compiles fine but when executed, the LEDs don't toggle on/off.

Any assistance would be greatly appriceated.

  • I edited your post to include code formatting. To do this, just select the code and choose the "{}" icon in the control bar. Otherwise, the special characters in the code are presumed to be other markup characters and make the code extremely difficult to read. – NomadMaker Jun 3 '18 at 18:58
  • If you correctly copied the code that would suggest the hardware is incorrect. – joan Jun 3 '18 at 19:36
  • If I'm understanding your comment about ``...hardware is incorrect'' the commented-out section of code works; all LEDs blinking on/off at the same time. It's just the threading section of code does not work. – Farad'n Jun 3 '18 at 20:13
  • 1
    I haven't used wiringPi's thread code. I suggest you get a simple example to work with just printfs inside the running code. – joan Jun 3 '18 at 20:44
  • That's the first thing I tried. Two threads and the result was: T1 (Thread1) ran once followed by T2 (Thread2) twice, and the program stopped. This was with printf()s in infinite for(;;){ } loops. – Farad'n Jun 3 '18 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.