top of page
  • BrainlessRBT

Python + Kivy + Arduino Countdown Timer for a Racing Toy

In this article, I will share a countdown timer code which combines Python, Kivy GUI and partially Arduino.



Actually, I did this project for a friend but I haven't completed fully. It was supposed to be an electronic timer switch for a racing car track whatever the name of the toy.

My friend asked, me if I can make something for him that he can run on his PC with some visuals, and just turns off the power of the racing toy when the countdown comes to zero. Well, I also wanted to add either RFID or an optic sensor to countdown the lap time. Normally, these toys have a mechanical lap timer counter for each track or you can buy infrared lap counters and some other off the shelf stuff. There are bunch of products on the market. But, you know, we want to make it ourselves!


As I mentioned before, I haven't fully completed this project. I haven't connected any relay and I commented out the Arduino section on the code. But I will still explain without visuals.


Before...


If you don't know what is Kivy, please first search for it and you will love it! I was using tkinter for GUI on Python until I learned how to use Kivy. And it is cross-platform, meaning that you can develop visual python programs for also Android, iOS and more. Also please check out how to install dependencies of the Kivy. If it is your first time on Kivy and if you haven't used PyCharm Code editor, that maybe a good start for you as well. Hopefully, this code will show some basics of Kivy. Well, to be honest, I am just learning it, I might have tricked the code a bit.


When you are designing your python gui with Kivy, you can add your widgets like button, slider, text box etc. or set colors, fonts either in your code or separately in a file with a .kv extension. You can watch this tutorial series of Tech With Tim on Youtube.


Let's Go...


You arrange the countdown time with the slider. Select Type toggle is not working because as I said before I was planning make a Lap based race by using some sensors as well, but I didn't have time for it.


When you press start, it count downs for initial count down, t-minus 4 seconds to start main count down.




Then light appears. And countdown shows the remaining time. In the code, you can have some good idea about different ways of interacting with the values of widgets such as with Kivy Properties. And also you can understand how you don't use delay but clock object, Clock.schedule. This is very useful and important.






Once it is finished, you can trigger whatever you want in that stop_reyz() function. Or on start in start_reyz()


Reyz = Race :P


For Instance when self.t_minus is 0, it updates the picture with Red light. You can add other triggers there such as I was planing to add relay switch from Arduino.

self.a.digitalWrite(self.input_pin, self.a.HIGH) # Trigger A Pin on Arduino

Full Code:


Python:

.kv

Note: Something important is that your .kv file name should be lower case of your main app without "app". Such as our app is MySliderApp(), then our .kv file name should be myslider.kv

<Controller>:
    
    GridLayout:
        cols:1

        Image:
            source:root.picture_light
            size_hint_x:0.4
            allow_stretch: False

    GridLayout:
        cols:1

        Label:
            id: time_label_id
            text: 'Set time to: ' + str(int(int(slider_id.value)/60))+ " m  " +   str(int(slider_id.value)%60)+ " s"
            color: 1,1,1,1
            font_size: 35

        Slider:
            id: slider_id
            min: 1
            max: 300
            #on_value: root.new_thickness(*args)
           # on_value: root.slider_value(*args)

        Label:
            id: count_label
            text: root.text_upd
            markup: True
            color:root.text_color
            font_size: 45

        Label:
            id: type_race
            text: '[b]Select Type[/b]'
            markup: True
            color: 0.25,0.500,0.25,1
            font_size: 30

        Switch:
            id: switch
            font_size: 30

        Label:
            text: 'Lap Based' if switch.active else ''
            font_size: 25
            color: 0,1,0,1

        Button:
            id: start_button
            text: "Start"
            font_size: 40
            markup: True
            background_color: 0,1,0,1
            on_press: root.start_reyz()

        Button:
            id: stop_button
            text: "Stop"
            font_size: 40
            markup: True
            background_color: 1,0,0,1
            on_press: root.stop_reyz()

Pictures: Pictures are in .png format so there is no background.







Relay:

You can check out this relay on Amazon.














So for now, this is it. I will improve each blog when there are requests. Please write in comments if you have any questions or need help. I will try to do my best. Good Luck!

566 views0 comments
bottom of page