Skip to content

Main Unit Code

Andreas Olsson edited this page Aug 31, 2023 · 4 revisions

Here you will find everything you need to get the code working.

When uploading your code, the J6, J7 and J8 on the PCB needs to be disable!

The board manager

We need to use Barebones ATmega Chips to upload the program. We are not going to use the bootloader on the chip.

First add the following in the settings under Additional boards manager: https://raw.githubusercontent.com/carlosefr/atmega/master/package_carlosefr_atmega_index.json then search for Barebones ATmega Chips and install it.

Then in tool menu you go under board and select atmega328/328p under barebones you find there.

Next set clock to External 16 MHz And processor you set ATmega328p select your programmer (USBasp if you have what i recommend) and press on Burn bootloader to clear the chip and inform about the settings.

When you upload you use upload with programmer

The Library

To use the main unit you need some librarys. There are also some adjustments in some library you need to do to reduce the size.

SDFat

Install SDFat library in Arduino library manager.

Inside SDFat library folder under src you find a file named SdFatConfig.h open that up and go down to row 40 and start enable the following define stuff:

#define USE_FAT_FILE_FLAG_CONTIGUOUS 0

#define ENABLE_DEDICATED_SPI 0

#define USE_LONG_FILE_NAMES 0

#define SDFAT_FILE_TYPE 1

Also on row 52 change from 1 to 0 on this: #define ENABLE_ARDUINO_SERIAL 0

What we did is removed a lot of unneeded stuff that take up a lot of memory!

EasyNextion

Install EasyNextion library in Arduino library manager. This is an small library that only use a few functions for Nextion displays.

RF24

Install RF24 library in Arduino library manager.

RTCLib

Install RTCLib library in Arduino library manager.

The code

Let's talk about the code. There is some stuff you need to know here!

Config & Config file

The config is inside a struct called config and holds the info stored on the sd card. We are save the struct direct on a file on an SD Card. You can change the name of the file under the define CNFFILE Please note that if you open the file in your computer, you will not understand a shit. It's not readable for us, but the arduino can read it.

In the config struct there is set to 5 targets and 5 players. I Don't recommend to have to many, as the more you have the more memory is needed. You can add more targets in the targID and recID array. You don't need to add the targets here, you do that on the touch screen.

The player are under char p1 - p5 and highscore use char hp1-3, hd1-3, hq and hr

The int targets is the amout of targets you are going to use in the game. If you only going to play with 3 targets, it will use the first 3 in the list of targID and recID

The int maxrounds is for quickdraw how many rounds you are going to use. int maxrapid is for how many hits you are going to use in Rapidfire.

The int totalplayer is how many players are going to play the game. The int timed are the time in millisecounds for Timed mode you are going to play. And the int safetime is the time in milliseconds for the failsafe function. This time is transmitted to the targets also.

All settings can be adjusted on the touch screen. Under settings, they are saved to the SD card each time you hit the back button.

The Setup

If the SD not working or Radio not working, the program will not start and the power led will blink and stop light for your information. During the test keep the myRadio.setPALevel on RF24_PA_MIN if you have it on example RF24_PA_MAX and have the targets close when you are testing, you will not get a stable connection.

During the setup it will check for config file, if it not there, it will be created. If it there, it will load all settings and highscore on start.

The loop

The only thing we keep in the loop is myNex.NextionListen(); that will watch for info from the nextion display.

The triggers

We have 7 void triggers in the code. This is the functions for the nextion. Inside the nextion we send commands on buttons with printh 23 02 54 XX where the XX is for example 07 for the void trigger7() code.

The trigger1 is for start the game. It will read a global value on the nextion display depending on what game you press on. Values are: 1 for Quicktime, 2 for Timed mode, 3 for Rapidfire.

The trigger2 is for Test the targets. It will start the sendTest function. The trigger3 is to update higscore info when enter the highscore page. The trigger4 is for update info about Game settings on nextion. The trigger5 is to update info about targets. The trigger6 is for update info about the players. And last trigger7 get info from nextion to be saved in the config struct.

The inform

The inform function is to make the led and summer work. We use it when start a new game, it will blink the power led and beep the summer depending on value you send.

If you use this inform(1, 0, 5); It will only use the buzzer and beep 5 times. The first one is to enable the buzzer, the second is for enable the power led, and the last is how many times to do it.

The play

All games are combinded in one functon. To activate quicktime we need to call play(1); and to activate timed mode we need to call play(2); and last for activate the Rapidfire we need to call play(3);

Depending on your settings, it will loop thru players one by one. The screen informs the next player, and you will hear a beep for 10 times. Also it will use the other settings also like targets, rounds and other stuff.

The savehigh

The save high is to save highscore. This is still in development It's not an advance higscore, it just show the best hit, or the best time from the games.

Clone this wiki locally