I need a Simple Stopwatch

09 May 2011

Hi there,

i will programming a simple Stop watch.

With the Button "s" the time begin up to start -> Min, sec, Milli sec.

With the Button "B" the time stopped.

With the button "C" the time will be reset.

i am a beginner programmer.

all of this schould be screening on the Pc with Tera Term.

Can you help me??


09 May 2011

the Mbed is with the Cable connected to the PC

11 May 2011


You should check the timer function for the stopwatch. for the buttons (if they're mechanical ones) search for the pindetect. You also need to set up serial communication for terraterm.

here you go:






11 May 2011

Remember, the PinDetect library uses a debounce timer and will introduce "dither" into the stopwatch (of upto 20ms).


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

Serial pc(USBTX, USBRX);

Timer t;

PinDetect button_star(p21);
PinDetect button_stop(p22);
PinDetect button_rset(p23);

volatile bool f_start;
volatile bool f_stop;
volatile bool f_reset;

void start(void) { f_start = true; }
void stop(void) {  f_stop  = true; }
void reset(void) { f_reset = true; }

int main() {

    f_start = false;
    f_stop = false;
    f_reset = false;

    // Attach button callbacks.

    // Activate buttons.

    while(1) {
        if (f_start) {
            f_start = false;
            pc.printf("Stopwatch started\n");            
        if (f_stop) {
            f_stop = false;
            // I will leave you to work out how to do hours/min/sec/ms display :)
            pc.printf("%dms elapsed\n", t.read_ms());
        if (f_reset) {
            f_reset = false;
            pc.printf("Stopwatch reset\n");

12 May 2011

I have this programm written:

but the time will be not blink every 1 seconds.

Do you have an idea??

  1. include "mbed.h"

Serial pc(USBTX, USBRX);

Timer timer; void DecodeInput(char input);

int main() {

char input = 'Z';

pc.printf("Start the timer with: 1\r\n"); pc.printf("Stop your time with: 2\r\n"); pc.printf("Reset your Timer with: 3\r\n");

pc.printf("\r\n"); pc.printf("\r\n");

while(1){ input = pc.getc(); DecodeInput(input); } } void DecodeInput(char input)

{ switch(input) {

case '1': timer.start(); pc.printf("Start at:%f Sek\r\n\n", timer.read()); break; case '2': timer.stop(); pc.printf("your time: %f Sek\r\n\n", timer.read()); timer.start(); break;

case '3': timer.reset(); pc.printf("Your Stoptimer will be resetet\n\n\r"); } }

12 May 2011

I would like to know that the split is shown for each second example: 0min: 1sec: 100ms | 0min: 2sec: 200ms | 0min: 3sec: 333ms | .... ..... ......

HOW i do it?

with a interrupt??