Firmware

Warning

The firmware, software that runs inside the microcontroller, is written in Arduino. You can damage your controller when changing the firmware: in most scenarios, you may recover it by uploading the original firmware, but there is a risk of serious problems to the equipment, please check the topic Microcontroller Recovery.

Prerequisites

The microcontroller source code is provided with the Java source code of the lap counter software. Furthermore, you will need to install the Arduino IDE, which can be found in our downloads page. It is recommended that you are able to use the lap counter software before changing the firmware, this will ensure the controller and its drivers are all working properly.

Environment Setup

Inside the Arduino IDE, open the latest version of the microcode from our repository, found in project/arduino subfolder. Afterwards, configure you board as an "Arduino Uno", select the proper communications port and upload the code.

Select "Arduino Uno" board.Select the correct communications port.

If you upload is succesful, you have just sent the firmware to the controller!

Microcontroller Recovery

Ouch! Something went wrong? Before worring too much, try uploading again the original microcode as explained above in 'Environment Setup'. In most cases this will restore you controller.

If you still cannot send the code to the controller, you may have lost the Arduino bootloader. To rewrite the bootloaded you will need to remove the ATMega328 chip and record it using an specific hardware. If you do not want to learn how to write a bootloader, maybe it will be easier to just buy a new ATMega328 with the "Arduino Uno" bootloader, which will probably be cheaper than buying the proper hardware to write it yourself.

Original Firmware

In the following sections we explain the main parts of the original lap counter firmware. Further explanations are given as comments inside the source code itself.

Magic Numbers

Contants used in many places within our code, it is a common practice doing so in case any of those numbers needs to be changed in the future.

States

All states that a sensor can have. 'Blocked' means the sensor is detecting the car and 'unblocked' that the sensor is not reading it. It works as follows:

  1. May block: sensor is clear, waiting for a passing car.
  2. May not unblock: detected a car passing, but it may not yet leave the sensor.
    • If the sensor unblocks now, the car reading will be ignored because it happened too fast to be a car.
  3. May unblock: some time passed (usually less than a millisecond) and the car may leave the sensor.
  4. May not block: the car left the sensor, the lap was counted, but it should not be detected again for a while.

One example that could happen if this system was not used: once a car approaches the sensor, but still too far to set a stable signal, it could trigger the sensor several times very fast counting many laps instead of one. This system ensures that it triggers the counter only once per lap.

Timers

Defines the minimums and maximuns times for some functions, such as the changes of state described above.

Messages

Byte encoding for the communication between the controller and the lap counter software. In general the first bits identify the message type and the last ones some parameter data that can be required.

Messages P2A_ identify the messages sent from the computer to the controller and A2P_ from the controller to the computer.

Pins

Define which ATMega/Arduino pins are connected to what.

Variables

Many global variables used by the firmware. I know, I do not like globals either...

reset() function

When called, reconfigures all pins and initializes all globals. It is called when the controller is started/restarted and may be called if requested by the lap counter software.

send_xxx() functions

Functions used to send data longer than one byte, such as numbers over 255.

setup() function

Called when the controlled is started. It just configures the USB connection and then calls the reset() function.

loop() function

Core function of the firmware. It is called several times in sequence, as soon as the previous call is finished, until the controller is turned off.