11 years, 1 month ago.

counting switch changes

Question is bellow..

Create a system which counts the number of times a digital switch is pressed or changed, and lights an LED when 10 instances have been counted.

how can i debug this code?

this is my code

#include "mbed.h"

DigitalOut rled(p5);
DigitalOut gled(p6);
DigitalIn switch_input(p7);

int main() {
    
    int count = 0;
    rled = 0;
    gled = 0;

    while(count < 10) {
        if(switch_input == 1){
            int check_value = switch_input;
            
            while(check_value){
                if(check_value - switch_input == 1){
                count++;
                check_value = 0;
                }
            }
        }
    }

    rled = 1;
    gled = 1;

}

Hi keem, if you surround code with <<code>> and <</code>> it is displayed nicely. I've done it for you this time.

posted by Stephen Paulger 18 Mar 2013

3 Answers

11 years, 1 month ago.

Hi Keem,

There are a few ways you can track down issues in code on mbed, the simplest to get started is using printf in your code to send messages over the USB serial port (more info here). You can find out about setting up a serial connection on the Terminals page in the handbook.

It is also possible to export your project to offline compilers and debug your code over CMSIS-DAP. At present this is possible with Keil MDK / uVision.

To simplify your code for this project I suggest you look at InterruptIn you can get it to call a function whenever it detects a rising or falling edge (like a button being pushed or release). Andy Kirkham's PinDetect library provides a InterruptIn style functionality but with debouncing, that may also be useful to you.

Steve

Accepted Answer
11 years, 1 month ago.

Interesting way of programming. I take it your switch is a mechanical switch, these tend to bounce. Therefore it is better to use DebounceIn than DigitalIn. Furthermore each use of switch_input implies a new read of the pin. So when you write: <<code>>while(count < 10) { if(switch_input == 1){ int check_value = switch_input;<</code>> The result of the second read may be different from the first. This means that even if the first switch_input==1, the second may be zero (and hence check_value will be zero) and your pulse will be missed. How to debug this? Very difficult, because a bouncing switch gives a number of short pulses before reaching the final value, any change to the program will influence the timing. So you may find that the program works OK when you put in some printf statements, yet fails when you remove them. Other things you can do is: use a led that lights when the switch is actually pressed or use variables that you increment at stategic places and analyse their values at the end of the program to see if there is unexpected behaviour. For a short program like this you are better of with careful analysis.

I'll try. thank you

posted by Keem Kendric 19 Mar 2013
11 years, 1 month ago.

there are a lot of ways to debug - here are perhaps two of the more common ways -

1) printf

Add

Serial pc(USBTX, USBRX);  // at the top, after the #includes

// sprinkle these where it makes sense. note that __LINE__ is the line number of that instruction.
pc.printf("DBG [%d] count: %d \r\n", __LINE__, count);

2) add more leds

You've got a couple of LEDs already, and the mbed has 4 on it (if you're using the LPC1768 for instance). So, you can simply energize combinations of the LEDs depending on where your code is at.

// at the top
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

// where you want them
led1 = 1;  // flash
wait(0.1);
led1 = 0;