1

this program is supposed to blink led when I press button, but the led start blinking with the start of program and never stop.

button wired to gpio 17 and ground (0 in wiringPi map) led wired to gpio 4 and ground (7 in wiringPi map)

    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <wiringPi.h>

    #define BUTTON_PIN 0

    void myInterrupt(void) {
       digitalWrite (7, HIGH) ; delay (500) ;
       digitalWrite (7, LOW) ; delay (500) ;
    }

    int main(void) {
      if (wiringPiSetup () < 0) {
          fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno));
          return 1;
      }

        pinMode (7, OUTPUT) ;
        pinMode ( 0, INPUT);


  if ( wiringPiISR (BUTTON_PIN, INT_EDGE_FALLING, &myInterrupt) < 0 ) {
      fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
      return 1;
  }

  while ( 1 ) {
  }

  return 0;
}
  • Have you set the gpios to be inputs or outputs somewhere else? Perhaps wiringPi assumes the gpio is an output if you use digitalWrite and assumes it is an input if you use wiringPiISR. How have you wired the button? Is it just one wire direct to ground and the other wire direct to the gpio? – joan Jul 27 '14 at 15:53
  • @joan I modified the code set led in input and interrupt pin output, still same problem, for the button, yes I just wired one side to ground and other to pgio, I think I should wire it to one pin (output mode)????? – Makhlouf GHARBI Jul 27 '14 at 16:12
2

If you put a printf in your callback (myInterrupt) you'll probably notice it is being continually called.

If so the reason is because the input is floating (Google the term).

I'd set the internal pull-up resistor on the BUTTON_PIN gpio to fix it at 3.3V. That way it will normally read high but will read low when the button is pressed.

I'd also explicitly set gpio17 as an input and gpio4 as an output.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.