11 years ago.

Problems creating a timer

I am creating a timer for science fair to measure the time it takes for an object to cover the distance between 2 light gates. When light gate 1 is passed through by the object the voltage to pin 20 will rise. at this point, i would like the timer to start. When the object passes through the second light gate I would like the timer to stop. Upon stopping I would like the elapsed time between start and end to be displayed in micro-seconds on my computer. I am using TeraTerm to do this. I am a novice at coding, this is my first project and it has proven much harder than i expected.

Any help is much appreciated.

posted by Hubert Patrovsky 03 Dec 2013

2 Answers

11 years ago.

Use <<code>> and <</code>> around your code to make it readable. And you don't have to open a new question when you also have your old one.

Then I assume it doesn't work since you ask the question. But how did you wire it, what happens in your program? What do you see in Teraterm? Or what is probably more relevant for your case, which errors do you get during compilation. It seems you copy pasted arduino code. This isn't an arduino, arduino code doesn't work on it.

So while one issue is lack of information from you, the main issue is that your code simply is wrong. Start with simple (example) programs that do work. Then slowly adapt them to meet your demands. For example start with those pages: http://mbed.org/handbook/Timer, http://mbed.org/handbook/DigitalOut, http://mbed.org/handbook/DigitalIn

Yes this is arduino code upon having tried by my self and failed repeatedly i looked around for code that i could use and adapt. Thus i found the arduino code on teachingeneering.org under train 2 sensors. I tried to adapt it to the best of my abilities however i once again failed. In frustration i posted the original arduino code.

posted by Hubert Patrovsky 03 Dec 2013

I will try my best but as the deadline looms i have become exetremly frustrated.

posted by Hubert Patrovsky 03 Dec 2013

Arduino has the loop/setup system as in your code, mbed uses standard C(++), which simply has a main function where it starts, if it arrives at the end no one knows what happens (as in, depends on the platform), so generally you make a neverending loop just like arduino, but using while(1) {}.

Anyway the Timer link shows both how Timer is used and how to print the time to Teraterm, make sure that works for you. Then the DigitalIn example simply directly routes an input to one of the onboard leds. Make sure that works with one of your lightgates. If that works we can go to the next step ;)

posted by Erik - 03 Dec 2013

Thank you i will try this and let you know how it works.

posted by Hubert Patrovsky 04 Dec 2013

The time is not printed into TeraTerm when i ran the program given in the timer link. However I have made sure that the terminal is linked to the processor. This leaves me at a lose as to what is wrong. I have used the examples you provided to write the following :

  1. include "mbed.h"

DigitalIn a(p20); DigitalIn b(p21);

Timer t (); int main () { read() DigitalIn a; while(1) { if(enable) { t.start (); printf("The run commences/n"); read() DigitalIn b; while(1) { if(enable) { t.stop (); printf("Time taken was %f micro-seconds/n", t.read ()); } } } }

It is far from perfect and when i compile the compiler continuasly tells me various things are not properly defined (error 20).

posted by Hubert Patrovsky 04 Dec 2013

1. Still use <<code>> and <</code>>, you can also see the code otherwise is unreadable.

2. If you get an error, tell which line(s) it is.

3. The error is for now irrelevant. Start with getting the basics working. It makes no sense to start making a more complicated program when your basic program isn't functional.

So you do not get anything in Teraterm when running that program? Do you connect to a serial port with teraterm? Is it labelled mbed? If you type in Teraterm, does the led of your mbed flash?

posted by Erik - 04 Dec 2013

I do not get anything when running that program and connected to serial port labeled com7: mbed serial port. When i click on the terminal window and type in Teraterm the led on the mbed flashes however nothing appears in the terminal window

posted by Hubert Patrovsky 04 Dec 2013

That is weird. What are the serial port settings in your teraterm?

posted by Erik - 04 Dec 2013

port:com7 baud rate:9600 Data: 8 bit Parity: none Stop: 1 bit Flow Control: none Transmitter delay: none

posted by Hubert Patrovsky 04 Dec 2013

i am out of time i must begin working on the full fledged program science fair is tomorrow. I have tried everything possible to get Teraterm working however it does not

posted by Hubert Patrovsky 04 Dec 2013

I got teraTerm to work however there seems to be a problem in the program with the timer that would print it's time i used the example program for serial out on the mbed website and everything functioned perfectly

posted by Hubert Patrovsky 04 Dec 2013

Weird, but good. Now that works, somewhat, can you do the DigitalIn example program, but then with your light gates, to see if that works correctly.

posted by Erik - 04 Dec 2013

Yes it works fine!!! :-)

posted by Hubert Patrovsky 04 Dec 2013

Now as something I usually don't do, but for once, I wrote something that probably should work, it compiles at least. But of course I don't have your setup so I cannot test it:

Try:

#include "mbed.h"

Serial pc(USBTX, USBRX);

DigitalIn lightgate1(p20);      //Set correct pin for the top light gate
DigitalIn lightgate2(p21);      //Set correct pin for the bottom light gate
DigitalOut statusLed(LED1);     //Will be high while the target is between the two light gates

Timer timer;

int main() {
    pc.printf("Starting program!\r\n");
    pc.printf("Gate 1 = %d and gate 2 = %d \r\n", lightgate1.read(), lightgate2.read());        //(Without the .read() it gives a warning, this should for sure work)
    while(1) {
        //Reset timer
        timer.reset();
        statusLed = 0;
        pc.printf("Waiting till gate 1 is triggered!\r\n");
        while(lightgate1 == 0);
        
        //Light gate 1 is triggered!
        timer.start();
        statusLed = 1;
        
        while(lightgate2 == 0);
        
        //Light gate 2 is triggered!
        timer.stop();
        statusLed = 0;
        
        pc.printf("Measurement finished\r\n");
        pc.printf("Measured time = %f seconds\r\n", timer.read());
        wait(2);
    }
}

It should print at least something to Teraterm. If it doesn't work, give as much information as possible, mainly what does it print to Teraterm, and if it hangs halfway also the state of the LED.

posted by Erik - 04 Dec 2013
11 years ago.

thank you so much I am so grateful. I do not know how to make it up to you. In the men time have made TeraTerm work by echoing the results back to the terminal from the processor.

I am absolutely overjoyed.

posted by Hubert Patrovsky 04 Dec 2013

So that program works now to measure the time? In that case: :-)

posted by Erik - 04 Dec 2013

Yes it works brilliantly better than i could ever have imagined it is absolutely fantastic.

posted by Hubert Patrovsky 04 Dec 2013