PinDetect lib limits ???

02 Jan 2012

Hi Guys,

I've used the PinDetect (and InterruptIn) lib to attach 14 key's to debounce, and even though the MBED compiler accepts this setup; It hangs during runtime. If i fiddle with setSampleFrequency en SampleCounts, it sort of works. But if i set all setSampleFrequency to 20.... the MBED dies @ runtime.

Is there a limit to how many Interrups i can setup? Or is it related to some limit in the MBED?

I just want to be able to read 14 Digital (debounced) inputs, and transmit a change event (key up and key down) using UDP.... How hard can that be???

Snifff.... and i was so close to get my mbed project working.

Theo :)

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

#define     ON              1
#define     OFF             0

long LoopCount = 0, PollCount = 0;

PinDetect buttonA1(p11); PinDetect buttonA2(p12); PinDetect buttonA3(p13); PinDetect buttonA4(p14);
PinDetect buttonB1(p15); PinDetect buttonB2(p16); PinDetect buttonB3(p17); PinDetect buttonB4(p18);
PinDetect buttonC1(p21); PinDetect buttonC2(p22); PinDetect buttonC3(p23); PinDetect buttonC4(p24);
PinDetect buttonD1(p19); PinDetect buttonD2(p20);

DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4);

Serial lcd(p9, p10);  // tx, rx
Serial usbUART ( USBTX, USBRX );

void keyPressedA1( void ) { usbUART.printf("F");lcd.printf("F");}
void keyPressedA2( void ) { usbUART.printf("G");lcd.printf("G");}
void keyPressedA3( void ) { usbUART.printf("H");lcd.printf("H");}
void keyPressedA4( void ) { usbUART.printf("I");lcd.printf("I");}

void keyPressedB1( void ) { usbUART.printf("A");lcd.printf("A");}
void keyPressedB2( void ) { usbUART.printf("B");lcd.printf("B");}
void keyPressedB3( void ) { usbUART.printf("C");lcd.printf("C");}
void keyPressedB4( void ) { usbUART.printf("D");lcd.printf("D");}

void keyReleasedB1( void ) { usbUART.printf("a");lcd.printf("a");}
void keyReleasedB2( void ) { usbUART.printf("b");lcd.printf("b");}
void keyReleasedB3( void ) { usbUART.printf("c");lcd.printf("c");}
void keyReleasedB4( void ) { usbUART.printf("d");lcd.printf("d");}

void keyPressedC1( void ) { usbUART.printf("L");lcd.printf("L");}
void keyPressedC2( void ) { usbUART.printf("M");lcd.printf("M");}
void keyPressedC3( void ) { usbUART.printf("N");lcd.printf("O");}
void keyPressedC4( void ) { usbUART.printf("K");lcd.printf("P");}

void keyPressedD1( void ) { usbUART.printf("Q");lcd.printf("Q");}
void keyPressedD2( void ) { usbUART.printf("P");lcd.printf("R");}

void InitButtons(void) {

    //Examples
    //button30.mode( PullDown ); //Button is attached to Vin
    //button30.attach_asserted( &keyPressed30 ); //Key pressed
    //button30.attach_deasserted( &keyReleased30 ); //Key released 
    //button30.setSampleFrequency(); // Defaults to 20ms, starts the actual sampling
    //button30.setSamplesTillAssert( 10 ); // This would mean 10 * 20ms debounce time = 200ms.
    //button30.setSamplesTillHeld( 200 ); // This would mean 200 * 20ms debounce time = 2seconds.
    //button30.attach_asserted_held( &keyPressedHeld );    
    //button30.attach_deasserted_held( &keyReleasedHeld );

    led1 = ON;
    wait(0.5);
    
    buttonA1.mode(PullDown); //Button is attached to Vin
    buttonA1.attach_asserted(&keyPressedA1); //Key pressed
    buttonA1.setSamplesTillAssert( 5 );
    buttonA1.setSampleFrequency(55); //Starts the actual sampling

    buttonA2.mode(PullDown); //Button is attached to Vin
    buttonA2.attach_asserted(&keyPressedA2); //Key pressed
    buttonA2.setSamplesTillAssert( 5 );
    buttonA2.setSampleFrequency(55); //Starts the actual sampling

    buttonA3.mode(PullDown); //Button is attached to Vin
    buttonA3.attach_asserted(&keyPressedA3); //Key pressed
    buttonA3.setSamplesTillAssert( 5 );
    buttonA3.setSampleFrequency(55); //Starts the actual sampling

    buttonA4.mode(PullDown); //Button is attached to Vin
    buttonA4.attach_asserted(&keyPressedA4); //Key pressed
    buttonA4.setSamplesTillAssert( 5 );
    buttonA4.setSampleFrequency(55); //Starts the actual sampling

    led2 = ON;
    wait(0.5);
    
    buttonB1.mode(PullDown); //Button is attached to Vin
    buttonB1.attach_asserted(&keyPressedB1); //Key pressed
    buttonB1.attach_deasserted(&keyReleasedB1); //Key released   
    buttonB1.setSamplesTillAssert( 5 );
    buttonB1.setSampleFrequency(55); //Starts the actual sampling

    buttonB2.mode(PullDown); //Button is attached to Vin
    buttonB2.attach_asserted(&keyPressedB2); //Key pressed
    buttonB2.attach_deasserted(&keyReleasedB2); //Key released   
    buttonB2.setSamplesTillAssert( 5 );
    buttonB2.setSampleFrequency(55); //Starts the actual sampling

    buttonB3.mode(PullDown); //Button is attached to Vin
    buttonB3.attach_asserted(&keyPressedB3); //Key pressed
    buttonB3.attach_deasserted(&keyReleasedB3); //Key released   
    buttonB3.setSamplesTillAssert( 5 );
    buttonB3.setSampleFrequency(55); //Starts the actual sampling

    buttonB4.mode(PullDown); //Button is attached to Vin
    buttonB4.attach_asserted(&keyPressedB4); //Key pressed
    buttonB4.attach_deasserted(&keyReleasedB4); //Key released   
    buttonB4.setSamplesTillAssert( 5 );
    buttonB4.setSampleFrequency(55); //Starts the actual sampling

    led3 = ON;
    wait(0.5);

    buttonC1.mode(PullDown); //Button is attached to Vin
    buttonC1.attach_asserted(&keyPressedC1); //Key pressed
    buttonC1.setSamplesTillAssert( 5 );
    buttonC1.setSampleFrequency(55); //Starts the actual sampling

    buttonC2.mode(PullDown); //Button is attached to Vin
    buttonC2.attach_asserted(&keyPressedC2); //Key pressed
    buttonC2.setSamplesTillAssert( 5 );
    buttonC2.setSampleFrequency(55); //Starts the actual sampling

    buttonC3.mode(PullDown); //Button is attached to Vin
    buttonC3.attach_asserted(&keyPressedC3); //Key pressed
    buttonC3.setSamplesTillAssert( 5 );
    buttonC3.setSampleFrequency(55); //Starts the actual sampling

    buttonC4.mode(PullDown); //Button is attached to Vin
    buttonC4.attach_asserted(&keyPressedC4); //Key pressed
    buttonC4.setSamplesTillAssert( 5 );
    buttonC4.setSampleFrequency(55); //Starts the actual sampling
    
    led4 = ON;
    wait(0.5);
        
    buttonD1.mode(PullDown); //Button is attached to Vin
    buttonD1.attach_asserted(&keyPressedD1); //Key pressed
    buttonD1.setSamplesTillAssert( 5 );
    buttonD1.setSampleFrequency(55); //Starts the actual sampling

    buttonD2.mode(PullDown); //Button is attached to Vin
    buttonD2.attach_asserted(&keyPressedD2); //Key pressed
    buttonD2.setSamplesTillAssert( 5 );
    buttonD2.setSampleFrequency(55); //Starts the actual sampling
    
//Wait timers below stop working

    wait(0.5);
    led4 = OFF;
    wait(0.2);
    led3 = OFF;
    wait(0.2);
    led2 = OFF;
    wait(0.2);
    led1 = OFF;
    
}

int main() {

    lcd.baud(19200);
    usbUART.baud( 115200 );
    usbUART.printf("UDP Tester starting...\r\n");

    LoopCount = 0;
    PollCount = 0;

    InitButtons();  

    PollCount = 0;
    while (1) {

        PollCount ++;

        if ( PollCount %  25000 == 0 ) {led1 = !led1;}
        if ( PollCount %  50000 == 0 ) {led2 = !led2;}
        if ( PollCount %  75000 == 0 ) {led3 = !led3;}
        if ( PollCount % 100000 == 0 ) {led4 = !led4;}

        if ( PollCount % 1000000 == 0 ) {

            PollCount = 0;
            LoopCount ++;

            lcd.printf("LoopCount= %dM\n",LoopCount);

        }
    }
}

02 Jan 2012

I must admit, I have only used denounce with 2 or 3 buttons, But I agree it should still work. Although, your LCD printf, will take 1/2 mS per character, so. this might be an issue.

have you considered a 4*4 key matrix ? where the rows are pulled hi, and dragged low by col.

then on Int. you send 1, 2, 4, 8 to col, ....

hope this helps

Ceri

02 Jan 2012

Ceri, Thanks for the reply :)

Do you mean when the UART is busy writing to the serial LCD, the debounce timers get screwed up?

....thinks long and hard.... Hmz.

I was hoping to make all the 14 keys "event" driven, in stead of polling all of them for changes.

A matix is not really an option, because i need to track ALL possible combinations of keypresses, up to 6 at a time. Some toggle switches, some pushbuttons, some reed relays.

Ah well, i'll revert to the orginal InterruptIn then.

02 Jan 2012

..edit..

I just recoded to the standard InterruptIn lib, but that one seems to have the same problem.

Look at the InitButtons code below. I cannot attach a fall event to buttonD1, when i try, MBED hangs.

I'm, starting to think that i cant have more then an X number of interrupt or something.

Getting desperate here :'(

Help?

Theo.


void InitButtons(void) {

    ledH1 = ON;

    buttonA1.mode(PullDown);
    buttonA1.fall(&keyPressedA1); //Key pressed

    buttonA2.mode(PullDown);
    buttonA2.fall(&keyPressedA2); //Key pressed

    buttonA3.mode(PullDown);
    buttonA3.fall(&keyPressedA3); //Key pressed

    buttonA4.mode(PullDown);
    buttonA4.fall(&keyPressedA4); //Key pressed

    ledH2 = ON;
    
    buttonB1.mode(PullDown);
    buttonB1.fall(&keyPressedB1); //Key pressed
    buttonB1.rise(&keyReleasedB1); //Key released

    buttonB2.mode(PullDown);
    buttonB2.fall(&keyPressedB2); //Key pressed
    buttonB2.rise(&keyReleasedB2); //Key released

    buttonB3.mode(PullDown);
    buttonB3.fall(&keyPressedB3); //Key pressed
    buttonB3.rise(&keyReleasedB3); //Key released

    buttonB4.mode(PullDown);
    buttonB4.fall(&keyPressedB4); //Key pressed
    buttonB4.rise(&keyReleasedB4); //Key released

    ledH3 = ON;

    buttonC1.mode(PullDown);
    buttonC1.fall(&keyPressedC1); //Key pressed

    buttonC2.mode(PullDown);
    buttonC2.fall(&keyPressedC2); //Key pressed

    buttonC3.mode(PullDown);
    buttonC3.fall(&keyPressedC3); //Key pressed

    buttonC4.mode(PullDown);
    buttonC4.fall(&keyPressedC4); //Key pressed

    ledH4 = ON;

    buttonD1.mode(PullDown);   
    buttonD1.fall(&keyPressedD1); 

    buttonD2.mode(PullDown); //<----- MBED DIES trying to add this button !!!!!!!!!!!!!!!!!!!!!!!!
    buttonD2.fall(&keyPressedD2); //Key pressed

    ledH1 = OFF; //<----- LEDs stay on, to prove the subroutine is not finished !
    ledH2 = OFF;
    ledH3 = OFF;
    ledH4 = OFF;

}

02 Jan 2012

<<DOH>> /selfslap

"Any of the numbered mbed pins can be used as an InterruptIn, except p19 and p20."

Moved those 2 to 28 and 27, but it still does not work, when i try to use the PinDetect for all 14 switches. :(

02 Jan 2012

Why don't you change your code, So denounce .. or .. Int set a flag / circular buffer .....

And your main poles buffer, and main does printf

This SHOULD be better, as time delays now only occur In main. And timing critical is in Int.

Hope this helps

Ceri

02 Jan 2012

Good point Ceri,

I'm doing that allready, but i would have liked it to be interupt driven.

I think it's simpler :)

Thanks for the feedback,

Much appriciated!

Theo

... still wondering what the @%!@%#1 is wrong with those other libs.