11 years, 7 months ago.

i have a question about interrruptin

blow code does not work well

when i switch on, "count" doesn't change.

what the problem?

#include "mbed.h"

InterruptIn input(p5);
DigitalOut led1(LED1);
DigitalOut led2(LED2);

int count = 0;
int pre = 0;

void count_input(){
    count++;
    wait(0.002);
}

int main() {

    input.mode(PullUp);
    input.rise(&count_input);
    
    while(1) {
        if(count%4 == 0){
        led1 = 1;   led2 = 1;
        }
        
        if(count%4 == 1){
        led1 = 1;   led2 = 0;
        wait(0.2);
        led1 = 0;   led2 = 1;
        wait(0.2);
        }
        
        if(count%4 == 2){
        led1 = 0;   led2 = 0;
        }
        
        if(count%4 == 3){
        led1 = 1;   led2 = 0;
        wait(0.3);
        led1 = 0;   led2 = 1;
        wait(0.3);
        }
    }
}

1 Answer

11 years, 7 months ago.

Assuming you got it correctly connected and all, try defining count as volatile int. The compiler might be optimizing too much now.

Accepted Answer

Thank you for your answer. I solve this problem. Changing p20 to p10 and deleting "input.mode(PullUp);"

posted by Keem Kendric 28 Apr 2013