Slot car timer problems

I am building a race timer and tree for slot car drag racing. I am using optocoupler modules to read when the car is blocking the sensors at the start of the race. Once the car is staged and blocking the sensor I have separate nano that operates a drag racing tree that shows that the car is staged with led lights and drops through three yellow bulbs and then a green one. Each yellow bulb has a 500 millisecond delay and when the bottom yellow bulb lights it has a 500 millisecond delay then it sends a signal to my uno that the race has started. The cars leave and run through three optocouplers then an final one at the finish line.

THE PROBLEM : I need to get a time stamp at the exact time the cars leave the staging sensor and when the first block the three sensors along the track and finally the exact time they block the finish sensor.
Being new to arduino I’ve tried everything I can to get this to work but nothing is accurate enough. Need to find millis when stage sensor has a falling edge and a rising edge on the rest of the sensors to be able to do some math and display time intervals from the start to the finish and from start to sensors on the other three sensors.
Any help would be awesome and thanks in advance.

Welcome to the forum!

Post the sketch that worked but was not accurate enough.

Actually it sounds like you'd have to post both sketches for us to see where you are at and whether you are on tack, so to speak, or not.

Use "Autoformat" in the IDE to make your code pretty.

Use "copy for forum" in the IDE and paste the code here so it continues to look pretty.

You probably should post the schematic you are working to.

What, no schematic? Draw one, pencil or pen, shoot a picture to post. show all,parts and also where power is coming from.

a7

Hi, @shawnmart
Welcome to the forum.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Post your code, and details of the timing you are currently getting, and the accuracy you would like to get.

Hi,

A circuit diagram would be good too.
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

I have changed the code so much to try and get this to work. Really need a way to capture the millis when a sensor first reads high then I can get a good code. I’ve tried if statements and from what I can tell with my ociliscope depending on where the arduino is in the code I get a trigger to read from 5 to 20 milliseconds. Some kind of interrupt where I can read the millis or micros seamed to be the ticket but I can’t figure it out. I’d like to have an accuracy of around .001 to .003. Thank you.

Is that 1 to 3 milliseconds?

and currently you are only getting to 20 milliseconds?

Would be useful to understand the circuit you are using. Picture/diagram.

I have been poling and it depends where in the loop the program is when the sensor reads high. I can get it down to .002 but not consistently.

How do you determine that the time you are getting from the Arduino is not accurate? How do you measure the actual time that you compare to the reported time?

It would be useful to see any code that ever did any measurement of any kind.

a7

Help us help you.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Post a schematic.

Post an image of your project.

Which Micro Controller are you using?

Is this simulator code?

Please describe the problem better then you just did.

I am using 2 Chanel’s on the oscilloscope one is triggered when sensor goes from low to high and one is is triggered on an output pin that digitalWrites high when that event is true. I am new to this forum. But when I figure out how to post a picture I will post a picture of my fastest times and my slowest times.

You would be better off figuring out how to post your code, and doing that.

Pictures are not worth 1000 words. We can trust you to use your oscilloscope, and we don't doubt your findings.

a7


Yellow is sensor trigger and blue is D/O pin trigger. This has about 3 ms delay. My readings range from half a ms to around 20 ms between both. I would think this is because of where in the loop the arduino is when the event occurs. It seems like the form is only going to let me post one picture at a time.


Fastest reading here. But for the most part they are 3 to 20 ms apart.

Have a look into port manipulation.
This page gives you some basic info: https://create.arduino.cc/projecthub/Hack-star-Arduino/learn-arduino-port-manipulation-2022-wokwi-systems-simulat-10f9af
There's a lot of good videos on youtube which explain it better though.

I have tried the pin change interrupts but my problem is when the cars pass through the optocouplers the car breaks the beam and an interrupt occurs then the car leaves the beam and a second interrupt occurs. I just need the micros at the exact time that the first interrupt goes from high to low on that edge and I don’t need the second interrupt on that sensor at all. Then the car goes down the track to another sensor and the procedure repeats on that sensor. Five sensors total and then I can do some math and display a reaction time and elapsed times from start to each sensor.

Where is your code?

How can we troubleshoot this issue without seeing your code?

1 Like

No one can help you without seeing the code. POST. YOUR. CODE.