Headlights

In the following code example, you will learn the different methods with which you can control the headlights of your Joy-Car in a similar way to a real car. You will learn how to use the indicators correctly to indicate turns or lane changes. You will also learn how to activate the hazard warning lights to draw attention to your vehicle in emergency situations. The example also shows you how to switch on the high beam to improve visibility in poor light conditions. You can implement all of these functions easily and intuitively.
Headlights

This controls the headlights. A white light is activated at the front and a red light at the rear.
Indicator

Check the indicators for one side (left/right) of the Joy-Car. The front and rear headlights on the selected side start to flash yellow.
Hazard lights

Use this to control the warning light. All headlights start to flash yellow.
Brake light

This controls a brighter, red light on the rear headlights of your Joy-Car.
Reversing light

Use this to check the reversing light. This illuminates a white light on the rear headlights.
Code example
In MakeCode, you can find all the functions available for controlling the headlights of the Joy-Car below the menu item LEDs. In the sample code provided, these functions are used to demonstrate the various lighting options of the Joy-Car. You can see how to activate the indicators, turn on the warning light or use the high beam to understand how to control the headlights in practice. These examples will show you how easy it is to use the lights of your Joy-Car in your own code.
Definition of the headlights

Low beam
The following method allows us to switch the dipped headlights of the Joy-Car on and off. The variable on
decides whether the low beam is activated or deactivated when the method is called. By default, the on
variable is set to True
, so that the low beam is switched on if no other specification is made.
Within the method, the previously defined LEDs for the low beam are either set to the color white or red to simulate the low beam, or to black to switch the LEDs off, depending on the status. This clearly structures the switching on and off of the low beam in the code and enables flexible control of the Joy-Car's light function.
Brake light
The following method works in a similar way to the method for the low beam, but is specifically intended for the outer rear lights of the Joy-Car to simulate a brake light. When the brake lights are activated, the outer rear lights are set to a bright red. The variable on
is used again to define whether the brake lights should be switched on or off.
This method offers a simple way of flexibly controlling the brake light and adapting it to the driving conditions of the Joy-Car.
Reversing light
The following method works on the same principle as the methods described above for the dipped beam and brake light, but is designed for the reversing light of the Joy-Car. Here, the rear left outer LED is set to a bright white to simulate the reversing light. Control is again via the variable on
, which determines whether the reversing light is activated or deactivated.
This method enables precise control of the reversing light and increases safety when reversing.
Indicators & hazard warning lights
The following method works in a similar way to the previous ones, but is slightly more complex as it simulates the flashing of the indicators and hazard lights. In order to control the flashing without interrupting the entire program sequence, a time measurement is carried out. This allows the flashing effect to be generated without the program having to stop.
The variable last_ind_act
is used for this purpose. It saves the time of the last change of state of the blinker. Each time the blinker is to change its state (on/off), the method checks whether at least 400 milliseconds have passed since the last change. If this is the case, the status of the blinker is switched and last_ind_act
is set to the current runtime. As with the other methods, the variable on
controls whether the indicators are activated or deactivated.
Code example
In MicroPython, the various functions of the headlights are stored in separate methods so that you can easily integrate them into your code if required. For example, if you want to control the indicators, the hazard light or the high beam, you can simply call the appropriate method. Each function is controlled independently, and by using a Boolean value on
, you can switch the respective function on and off without affecting other headlight functions. This enables flexible and modular control of the headlights without conflicts between the different light modes.