Check DigitalIn for set time

09 Mar 2011

I am trying to continually check if a pin turns high for 0.5 seconds. If it remains low for all 0.5 seconds, I want to program to progress to the next step. So far I have

  1. include "mbed.h"

DigitalIn enable1(p5); DigitalIn enable2(p6); DigitalOut led(LED1); DigitalOut led2(LED2);

int main() { while(1) { if(enable1) {

09 Mar 2011

PinDetect library can probably do this for you:-

Import libraryPinDetect

InterruptIn style DigitalIn debounced with callbacks for pin state change and pin state hold.

#include "mbed.h"
#include "PinDetect.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);

PinDetect enable1(p5);
PinDetect enable2(p6);

volatile bool enable1assert;
volatile bool enable2assert;

void enable1callback(void) { enable1assert = true; }
void enable2callback(void) { enable2assert = true; }

int main() {

    enable1assert = false;
    enable1.setSamplesTillHeld( 25 ); // 25 samples at 20ms is 0.5seconds
    enable1.attach_asserted_held( &enable1callback );

    enable1assert = false;
    enable2.setSamplesTillHeld( 25 ); // 25 samples at 20ms is 0.5seconds
    enable2.attach_asserted_held( &enable2callback );

    // Begin sampling.
    enable1.setSampleFrequency(); // Default sample frequency is 20ms.
    enable2.setSampleFrequency(); // Default sample frequency is 20ms.


    while(1) {
        if (enable1assert) {
            led1 = 1;
            enable1assert = false;
        }

        if (enable2assert) {
            led2 = 1;
            enable2assert = false;
        }

    }
}

This is just an example to get you started. The PinDetect library also has attach_deasserted_held for the opposite sense detection. Have a read through the docs and the code to see how it works. Even if it doesn't do exactly what you need there's plenty to learn in there that should show how you can code you own solution.

09 Mar 2011

Thank you for your help, but that did not perform as I had wanted. Also, when I ran the code you gave me, I applied a high to pins 5 and 6 the LED's did not turn on.

09 Mar 2011

Really? Are you sure you don't have an error in your hardware setup? I have used the PinDetect library before and it works quite well.

09 Mar 2011

Bradley, I never said it would work. I said it was a "example". I never actually tried it myself. However, the examples provided with the library do work, they are tried and tested. What you are supposed to do is read the docs with the library and figure it out how to use it. That's your task now. Learn it. Either you can work PinDetect into what you want or if it's not suitable for your purpose read the library code and use it as an example of how to use the timers etc.