Go Down

Topic: Using Arduino for Slot car timing (Read 4166 times) previous topic - next topic

Louisbaby

Hi all

I am relatively new to Arduino and the world of programming, But I am already hooked on the few things I've tried...

Being the holiday season again, I took out my Scalextric set for some fun with the kids. And in playing a few evenings ago, I thought of creating a slot car lap counter as well as lap timing program using Arduino. Firstly, I'll admit to scouring the Internet in search of a quick tutorial or step-by-step guide. But I haven't found one. But the desire to create one is all the more present now...

So here is what my plan is:

Arduino Mega or Nano (I have both)
Reed switches glued under the track.

And I would like to use my Laptop to read back the info.

Here is the code I found for a simple counter using reed switches:



I'll add more later...

UKHeliBob

Welcome to the forum.

Please read this and follow the advice on posting the code here using code tags.  Using them makes it much easier to copy the program into an editor without downloading it.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

jurs

Being the holiday season again, I took out my Scalextric set for some fun with the kids. And in playing a few evenings ago, I thought of creating a slot car lap counter as well as lap timing program using Arduino. Firstly, I'll admit to scouring the Internet in search of a quick tutorial or step-by-step guide. But I haven't found one. But the desire to create one is all the more present now...
A while ago a guy in the German forum asked for a lap counting/timing code for his "Carrera-Bahn", which is the common brand of slot cars here in Germany.

I created him a small Arduino sketch for this hardware:
- Carrera-Bahn with 2 slot cars
- Arduino UNO
- LCD Keypad Shield as a display
- 2 reed switches glued under the track

The LCD provides a 2-second-changing display for:
- lap counter
- time display (last lap / fastest lap)

The slotcar timing sketch for this hardware is in this posting in the German forum:
http://forum.arduino.cc/index.php?topic=158779.msg1189593#msg1189593
(Comments in that code are German)

I think I've everything prepared so that the sketch also can be used with a MEGA board and with 4 or 6 slots instead of 2 with only a few changes in the code.

Louisbaby

Thanks Jurs

I have to say, Google translate didn't work properly, so I am a little in the dark with a few things.

Firstly the code is one long string - I'm a little unsure on how to seperate it.
Secondly - could I make use of my Laptop for the graphical representation?

jurs

Firstly the code is one long string - I'm a little unsure on how to seperate it.
Which operating system do you use?
How did you copy-and-paste the code into the Arduino-IDE so that you got all code in one single line?

I've just copied the code into the Windows "notepad.exe" editor and saved as a TXT file. I'll attach that. This file should contain "Windows line endings" (CR/LF). Copy the contents from the TXT file into the Arduino-IDE editor.

Secondly - could I make use of my Laptop for the graphical representation?
Currently, the display shows "text output" only.
You can easily send the same text output to Serial instead of LCD, so you could watch in the serial monitor or in any serial terminal software.

What do you mean by "graphical representation"?

Louisbaby

Thanks for that, I will try and decipher the German back to english.

By graphical I ment seeing it on a screen. Not necessarily a graphic picture, that I will attempt later. 

jurs

Thanks for that, I will try and decipher the German back to english.
These days I'm a bit short of time, but perhaps next year, maybe. If anybody needs it with comments in English.

By graphical I ment seeing it on a screen. Not necessarily a graphic picture, that I will attempt later.  
If you just want to see the data on the serial.monitor, you could add Serial initialization in the setup() function by adding:
Code: [Select]
Serial.begin(9600);

And then you could replace every occurance of:
Code: [Select]
   lcd.print(lcdline);
with additional output on Serial:
Code: [Select]
   lcd.print(lcdline);
   Serial.println(lcdline);

Louisbaby

Here is what I have so far...

Not sure about certain of the faults in the code that returns after compiling it.

jurs

Not sure about certain of the faults in the code that returns after compiling it.
You better not try to use space characters in variable names.

So instead of:
Code: [Select]

long lap time [] = ...
long lap time record [] = ...

perhaps better name the variables like:
Code: [Select]

long lapTime [] = ...
long lapTimeRecord [] = ...


Besides of that:
DO NOT replace the commas used as seperating characters between array elements by a point!

vaclav

Here is what I have so far...

Not sure about certain of the faults in the code that returns after compiling it.
You need to go thru the translated code and rename the variables into single "verbose" for C to understand.

For example "lap time processing" could be changed to "Lap_Time_Processing" or "LapTimeProcessing".
There is no other way to correct the code so it will at least compile. Just step by step, each line.


Louisbaby

Having a problem with this line:

void (*isrFunctions [])() = { timing1, timing2 };

It comes back with this fault:

sketch_Reed_slot_mach1:18: error: 'timing1' was not declared in this scope

 void (*isrFunctions [])() = { timing1, timing2 };

                               ^

sketch_Reed_slot_mach1:18: error: 'timing2' was not declared in this scope

 void (*isrFunctions [])() = { timing1, timing2 };

                                        ^

What on earth am I missing?

jurs

What on earth am I missing?
The code I posted was written for Arduino-IDE v1.0.5 and compiled fine with that IDE version.

I just tried a compile test with a more recent Arduino-IDE v1.6.5-r5 and the code compiled fine with that IDE version, although it creates some warnings like:
> warning: comparison between signed and unsigned integer expressions
(you can ignore warnings, or fix the variable type declarations if you like)

So you should not get any trouble compiling the posted code when using Arduino IDE versions between 1.0.5 and 1.6.5, at least.

Some hints:
- use an Arduino IDE version in the range 1.0.5 and 1.6.5 for compiling the code
- first compile the original code "as it is", no "error" should occor, only "warnings" (depends on IDE version)
- better do not mangle the whole source code through a "translator service"
- translate comment for comment for comment
- translate single variable names (without adding space characters in variable names)
- if the code compiles "before translation" but does not compile "after translation", it's the translation causing the error

vaclav

#12
Dec 29, 2015, 04:44 pm Last Edit: Dec 29, 2015, 04:52 pm by Vaclav
Having a problem with this line:

void (*isrFunctions [])() = { timing1, timing2 };

  I am not sure this definition is "legal" as far as C is concerned anyway.


It comes back with this fault:

sketch_Reed_slot_mach1:18: error: 'timing1' was not declared in this scope

 void (*isrFunctions [])() = { timing1, timing2 };

                               ^

sketch_Reed_slot_mach1:18: error: 'timing2' was not declared in this scope

 void (*isrFunctions [])() = { timing1, timing2 };

                                        ^

What on earth am I missing?
Look at the original single slot detector code.

In your code the single code has been replaced with an array , hence it has to be coded / defined as an array - both interrupt pins and the isr. 


Single


Code: [Select]
#define LEDPIN 13  //das wirds der pin sein der meine led blinken lässt?!
#define REEDPIN1 2         //der Eingang der als erste von zwei Interrupts an Pin 2 fungiert?!
#define REEDINTERRUPT1 0
long roundStart1 = -9999;   //verstehe ich noch nicht

//denke das ich das ganze zweimal machen muss wegen der zwei eingänge, versuche das mal in einem script einzubauen

void setup() {
  // put your setup code here, to run once:
  pinMode(LEDPIN, OUTPUT);
  pinMode(REEDPIN1, INPUT_PULLUP);
  attachInterrupt(REEDINTERRUPT1, timing1, CHANGE);

timig1 is single ISR here
}


Array


#define LEDPIN 13
#define SLOTS 2 // minimum lap time in milliseconds, everything underneath interpreted as contact bounce
#define MINROUNDTIME 500 // Pins for connection of reed contacts (D2, D3)

byte reedPins[ ]= {2,3} ; // interrupt numbers to these pins
byte pinInterrupts[ ]= {0,1}; // interrupt handlers

single isr replaced with  pinInterrupts[ ] array here

void (* isrFunctions []) () = {timing1, timing2};
// Start the final lap in milliseconds, volatile because access also from ISR!
volatile long around the top[ ]= {0,0}; // lap time on the last lap in milliseconds
long lap time[ ]= {99990.99990}; // lap time of the fastest lap in milliseconds
long lap time record[ ]= {} 99990.99990; // lap counter
int round number[ ]= {0,0};










odometer

- better do not mangle the whole source code through a "translator service"
This should go without saying.

Besides, you don't need to know German to make sense out of a lot of it. For example, what do you suppose "Millisekunden" are?

Louisbaby

Thanks to everyone who has helped me so far, and I apologize if my questions are silly. I do only have 3 weeks worth of trail-and-error coding practice. So I suppose I shouldn't be taking on such ambitious projects so early. But boys will be boys...

Anyway, I am running Arduino V1.6.6, and I will try to copy and paste your code from the TXT file you sent me, and see if it compiles correctly, and take it from there.


Go Up