Stopwatch


28 posts   Page 1 of 2   1, 2
by speedyroadster » Wed Aug 29, 2012 3:06 pm
Hi,
I'm very much a novice with Python but I wanted to try to create a stopwatch that would start and stop with a keypress only.
My eventual goal is to create a laptiming system using a Pi and the GPIO pins to count/time laps for my slot car (scalextric) track.
My initial question is how can you ask Python to await a specific keystoke (without having to also press 'Enter' afterwards)?

Many thanks,
Alan
Posts: 15
Joined: Wed Aug 29, 2012 2:56 pm
by BlackJack » Wed Aug 29, 2012 5:02 pm
@speedyroadster: You could configure the terminal from which you start your code for „raw input”, which is kind of low level Unix system programming. Or you may use the `curses` module from the Python standard library. Or maybe a graphic library like `pygame` which has functions to react to single keys.
Code: Select all
while not self.asleep():
    sheep += 1
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
by speedyroadster » Wed Aug 29, 2012 5:30 pm
Thank you BlackJack.
Image
This a timing bridge I made which uses a 741chip to compare to LDRs (situated opposite to the bright LEDs) which then switches via a reed relay the contacts of the remnants of an old pc keyboard housed in the red box. This is then fed into a pc via usb and the computer thinks a key has been pressed. I wrote a program using Basic 256 to run the timer but I would like to get away from taking apart keyboards and use a Pi and Python and the GPIO pins.

I shall investigate the 'Pygame' and 'Curses' and see how I get on.
Regards, Alan
Posts: 15
Joined: Wed Aug 29, 2012 2:56 pm
by Sleep Mode zZ » Wed Aug 29, 2012 10:58 pm
With a bit of googling: http://code.activestate.com/recipes/134892/
Could be the easiest way as it does not require curses or pygame.
Posts: 200
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland
by MadCow42 » Thu Aug 30, 2012 1:56 am
speedyroadster wrote:Thank you BlackJack.
Image
This a timing bridge I made which uses a 741chip to compare to LDRs (situated opposite to the bright LEDs) which then switches via a reed relay the contacts of the remnants of an old pc keyboard housed in the red box. This is then fed into a pc via usb and the computer thinks a key has been pressed. I wrote a program using Basic 256 to run the timer but I would like to get away from taking apart keyboards and use a Pi and Python and the GPIO pins.

I shall investigate the 'Pygame' and 'Curses' and see how I get on.
Regards, Alan


if you already have the bridge set up so it closes a circuit with a relay when the car goes through, that should be dead simple to connect to the RPi's GPIO's for timing. Connect one lead on the relay to the RPi's 3V3 pin, and the other to a GPIO. A couple lines of Python to set up the GPIO pin for input and to read it, and you're golden.

It's probably easier than trying to figure out how to get a keypress event without a Return. :)

Kevin.
Posts: 86
Joined: Sun Jul 01, 2012 12:48 am
by speedyroadster » Thu Aug 30, 2012 9:51 am
Thank you Sleep Mode and MadCow.
Sleep Mode, the link shows code that will take me a while to figure out!
In Basic256 it seems fairly simple:

keypressroutine:
k = key
if k = asc("1") then goto anotherroutine
goto keypressroutine

anotherroutine:


MadCow, yes my goal is to have it just connected to the GPIO pins but I wanted to try out making a stopwatch/lap counter work first of all.

I am such a Noob! Ha ha :lol:
Thank you for all your help
Alan
My Speccy Pi case:
Image
Posts: 15
Joined: Wed Aug 29, 2012 2:56 pm
by rammy » Fri Sep 21, 2012 12:17 pm
Looks like a great project which I am watching with interest.
I am a total noob when it comes to Raspberry Pi and I have just got my 2 kids a Scalextric set (I loved it when I was a kid) so would love to be able to but the Pi and Scalextrix together and make a lap counter / timer maybe output to the TV in the same room.
Any step by step instructions would be greatly appreciated when you have cracked it!

Thanks
Dave
Posts: 2
Joined: Fri Sep 21, 2012 12:03 pm
by Pi-o-neer » Fri Jan 04, 2013 4:26 pm
Hey guys, I am wondering if anyone had any luck getting Raspberry Pi to work with slot cars ? I am looking at making a lap counter or maybe adding a few of those to work out speed along the track.
User avatar
Posts: 3
Joined: Wed Jan 02, 2013 1:56 pm
Location: Melbourne, Australia
by Wibbes » Sun Mar 03, 2013 6:36 pm
Hey guys,
I'm unsure if anyone's still looking to use the pi as a lap counter for a slot car track, but I'm having sum success with it at the moment, will post the code if anyone's interested. Sadly it looks like the timing of it is out abit, so if anyone could help me out there it would be a huge help.

Thanks guys
Wibbes
James w Lyons aka wibbes

My pi is a model B what's ures?
Posts: 11
Joined: Sun Mar 03, 2013 6:33 pm
by rammy » Sun Mar 03, 2013 7:18 pm
Definitely interested in any help you can offer.
Posts: 2
Joined: Fri Sep 21, 2012 12:03 pm
by Wibbes » Sun Mar 03, 2013 8:42 pm
Glad to know there's a need for it :) will post in morning after I've removed all the bits that I'm currently working on, trying to make up a menu but its not working and every time you run it it crashes, so watch this space
James w Lyons aka wibbes

My pi is a model B what's ures?
Posts: 11
Joined: Sun Mar 03, 2013 6:33 pm
by Wibbes » Mon Mar 04, 2013 8:54 am
Morning guys,
as promised here my current prototype, currently it only times one lane, a sceond lane should be simple just a copy and paste job i think

in order to work you need to be running pygame and the gpio library ,
if anyone dosent know how to install these items i will be happy to help.

the pin for lap counting is GPIO 17 , it has a pull down resistor on it set in the code, to activate a lap GPIO 17 needs to be pulled high to 3.3v , make sure you don't use the 5v line else you will fry you pi.

running the code just run it under terminal once you are in 'startx' and you should see a black box with a blue border, to start counting press s , to pause press q , to resume s and to exit press escape.

good luck let me know how it goes guys
Attachments
RaceControl.zip
Race control python script
(1.32 KiB) Downloaded 171 times
James w Lyons aka wibbes

My pi is a model B what's ures?
Posts: 11
Joined: Sun Mar 03, 2013 6:33 pm
by Pi-o-neer » Mon Mar 04, 2013 12:04 pm
@Wibbes That's great news, however call me thick but I don't understand how do you actually count laps ( or time .. what event exactly ? ). Is there a sensor or something ?
User avatar
Posts: 3
Joined: Wed Jan 02, 2013 1:56 pm
Location: Melbourne, Australia
by Wibbes » Mon Mar 04, 2013 12:41 pm
Yer you do need a sensor to detect laps, maybe a ir photo diode or switch in the lane, the role of the sensor is only to connect the GPIO pin to ground.

Will draw a little circuit diagram up for you when I return home from college.

Just bare in mind that at the moment the timing is only accurate to About a tenth of a second, hopefully after some more research I can make it accurate to a hundredth then I can implement it at my local club on our 6 lane wooden track :P
James w Lyons aka wibbes

My pi is a model B what's ures?
Posts: 11
Joined: Sun Mar 03, 2013 6:33 pm
by Pi-o-neer » Mon Mar 04, 2013 1:12 pm
The choice of sensor might make some difference as well, from what I have read so far. Here is a pretty good write up - look under Hardware pages | Slot car detectors. I was thinking magnetic reeds underneath - since cars would already have magnets and that doesn't involve cutting track or adding LEDs. I have no idea how well they would work, though.

I am wondering how hard would that be to add more sensors along the track .. so that you could time parts of the path ( e.g. bends, straights etc. ). Obviously, all sensors would have to be identifyable by the RPi.
User avatar
Posts: 3
Joined: Wed Jan 02, 2013 1:56 pm
Location: Melbourne, Australia
by Wibbes » Mon Mar 04, 2013 1:38 pm
Shouldn't be that hard to add sectors if that's what you mean , the pi has 17 GPIO pins that can be used , so for a two lane track you could add up to 8 sets of sensors and still have an free gpio pin for (track power or a start light) ,as for sensors my clubs track uses ir sensors under the start line with ir sources in a bridge above it, this setup is due to the cars we use not having magnets,

On a home track with magnets you could use reed switches, these would only be need to connect the GPIO pin to 3.3v , just remember you need a strong magnet in the cars in order for a reliable count laps

Just a quick edit , if any one knows which would be quicker out of interrupt and just polling the pins could you let me know , would be really help full to try and get the timing as close as possible.
James w Lyons aka wibbes

My pi is a model B what's ures?
Posts: 11
Joined: Sun Mar 03, 2013 6:33 pm
by texy » Mon Mar 04, 2013 3:17 pm
Hi,
its been a long time since I ever played on a scaletrix track. Forgive me for asking, but how do you differentiate between the 2 tracks, when detecting a beam break? Obvously if the transmit/receive pair are accross both tracks then either car can break the beam. If I remember correctly, you can't get a LED in the middle of the track. I think a photo is in order, lol.

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2402
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by Wibbes » Mon Mar 04, 2013 3:39 pm
To achieve the ir detector setup you need to have a sensor under each slot and LEDs shining down at them to stop changes in the light around them setting them off
Image

As for the reed switch interface which is properly the best suited for a home track I have a circuit diagram below, for lane one in my current code GPIO 17 is the pin used
Image
James w Lyons aka wibbes

My pi is a model B what's ures?
Posts: 11
Joined: Sun Mar 03, 2013 6:33 pm
by bard » Sun Nov 03, 2013 2:51 pm
Hi all,

Very interested in this project!

I have been using a Race Management System (RMS) with an old computer running DOS (forget Windblows... lol) and it works fine with Reed switches. Here is my setup:

non-permanent Ho TOMY AFX track (more layouts ;-) ), 2 or 4 lanes
lot of cars ranging from "pancakes" (no traction magnets but hey, the motor has magnets anyway...) to later chassis.

A 15" piece of track (2 pieces for 4 lanes) with one switch under each lane connected to an old laptop running a DOS timer software. Everything works great but.... I want to add a virtual fuel function to the setup. One of the progs I have does this (SCUDM) but with photocells.
So I designed a simple PCB to use 2 reed switches per lane (simulating pit entry and exit) that would trick the prog (like a stop on the photocell) for refuelling (you have to wait between the 2 switches just like an actual pit stop).
Anyway, never found the time to solder the board and test it... lol

So I'm interested in this project for the following reasons:

Pi is cool and a lot lighter than carrying a laptop
it connects to any TV for large display ability
It runs under Linux and not WIndows so everything is free (Actually, Python code could run on many platforms...)
It can easily handle many configurations: speed trap, timing sectors, refuelling, start lights, jump starts,....

Imagine a configuration menu where you would be able to affect pins to whatever event you like (start light, speed trap,...) or, for non-techies, options to activate (sector timing configuration, fuel management,....) and we would have the ultimate management software!

It seems like the stopwatch is the heart of the system and every other function I listed revolves around that. So after figuring out that part, the rest should be pretty much a little coding and some "cosmetic" stuff, don't you think?

Am I dreaming ?

Just my 2-cent....
Posts: 3
Joined: Sun Nov 03, 2013 2:10 pm
Location: France
by Wibbes » Sun Nov 03, 2013 3:24 pm
hi bard
long time since i have being looking into this, i have started working back in windows to give it a bit more power for a 6 lane setup.
one the main problems for the pi is its speed, especially when trying to monitor 2 or more lanes it becomes very hard to time them accurately , think best i got to be reliable was around 0.25 of a second on 2 lanes maybe 0.2 if i trimmed it down a bit on the drawing cycles for the screen.

to be fair even in windows its hard to do real timings without dedicated external hardware to do the timing( arduino or over micro).
James w Lyons aka wibbes

My pi is a model B what's ures?
Posts: 11
Joined: Sun Mar 03, 2013 6:33 pm
by bard » Sun Nov 03, 2013 4:16 pm
Hi Wibbes,

Yeah, I was checking the net and found out that for "real-time" systems, Arduino is better.... Guess I'll go back to my soldering iron for now (windows: no way!) and investigate a little more about Arduino (seems like there are projects like this going on...).

I'll use my Pi for a media server or arcarde emulator then....

Cheers.
Posts: 3
Joined: Sun Nov 03, 2013 2:10 pm
Location: France
by Wibbes » Sun Nov 03, 2013 4:40 pm
well you could use your pi as a front end display for ure timer , ardunios dont have hdmi (atleast not cheap)

maybe a serial link between an arduino doing the real time stuff and the pi doing the display , positions and less time stuff
James w Lyons aka wibbes

My pi is a model B what's ures?
Posts: 11
Joined: Sun Mar 03, 2013 6:33 pm
by joan » Sun Nov 03, 2013 4:47 pm
Wibbes wrote:well you could use your pi as a front end display for ure timer , ardunios dont have hdmi (atleast not cheap)

maybe a serial link between an arduino doing the real time stuff and the pi doing the display , positions and less time stuff

Would be better still to remove the errors in the code responsible for such poor timings.

I'd be surprised if the Pi couldn't perform better than an Arduino for this timing task.

Remember that the Pi can decode IR signals which are short pulse chains.
User avatar
Posts: 5806
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by Wibbes » Sun Nov 03, 2013 5:26 pm
i think the problem of speed for the pi , is that i havent tryed to code in c# , just using python which is an interpreted langues making the timings even worse , if anyone has an ideas about how easy it would be to speed up in python on the pi, i would be happy to help out the best i can
James w Lyons aka wibbes

My pi is a model B what's ures?
Posts: 11
Joined: Sun Mar 03, 2013 6:33 pm
by Wibbes » Sun Nov 03, 2013 5:39 pm
Just in case anyone wants to see how a counter like this could be done on a track. this is my clubs setup
Image
James w Lyons aka wibbes

My pi is a model B what's ures?
Posts: 11
Joined: Sun Mar 03, 2013 6:33 pm