Arduino Car Coding
You have to write software to control the car via Bluetooth.
The Troniction car coding page is divided into three sections.
Section 1. Download the completed source code files which are ready to upload. Go to Section 1
Section 2. Display of file contents in each source file. Go to Section 2
Section 3. In-depth explanation of the codes in the source files. Go to Section 3
Section 1
Troniction Arduino car has 2 source code files.
- arduinocar.ino
- pitches.h
You can download the files here.
arduinocar.ino
Core Car Code
This is the core software that enables Troniction car running with Bluetooth control.
Download Car Codepitches.h
Musical Note Definitions
This file defines the notes required to play the 'Happy Birthday' song by Troniction car.
Download Pitchestroniction_arduino_car.zip
All in One
This Zip Archive contains all the software source codes required to run the Troniction car.
Download All (Zip Archive)Section 2
File 1: arduinocar.ino
File 2: pitches.h
This code snippet (see the following pitches.h) defines a set of musical note constants with their corresponding frequencies. Each note is represented by a macro with the format NOTE_XY, where X is the note name (A, B, C, etc.), and Y is the octave number.
The frequencies of the notes are defined as integers, representing the number of cycles per second (Hz) for each note. These frequencies determine the pitch of the note when played using sound-generating functions such as tone().
For example, NOTE_C4 corresponds to the note C in the 4th octave and has a frequency of 262 Hz. NOTE_G6 corresponds to the note G in the 6th octave and has a frequency of 1568 Hz.
Another example, NOTE_C1 corresponds to the note C in the 1st octave and has a frequency of 33 Hz. NOTE_D1 corresponds to the note D in the 1st octave and has a frequency of 37 Hz.
By using these defined constants, you can easily refer to specific notes in your code instead of using their numerical frequencies directly. This improves code readability and makes it easier to create melodies or play specific musical sequences.
In music, different notes correspond to different frequencies, which determine their pitch. This code snippet provides a convenient way to refer to musical notes by defining macros for each note with their corresponding frequencies.
The frequencies are expressed as integers, representing the number of cycles per second (Hz) for each note. These frequencies determine the pitch of the note when played using sound-generating functions such as tone() in Arduino.
For example, NOTE_C4 corresponds to the note C in the 4th octave. The number 4 represents the octave, and the letter C represents the note name. The frequency of NOTE_C4 is 262 Hz, which means that when you play this note, the speaker or buzzer will vibrate at a rate of 262 cycles per second, producing the sound of a C note in the 4th octave.
Similarly, NOTE_G6 represents the note G in the 6th octave. The frequency of NOTE_G6 is 1568 Hz, indicating that the corresponding sound will be a G note in the 6th octave.
By using these defined constants, you can easily refer to specific notes in your code without having to remember their exact frequencies. For example, instead of using the frequency value 262 directly, you can use NOTE_C4, which makes the code more readable and easier to understand. This is particularly useful when creating melodies or playing specific musical sequences, as you can simply use the note names and octaves provided in the code snippet to compose your desired tunes.
Section 3
Let’s discuss the code in detail. Let’s take the main Troniction car code file.
The explantion of code can be divided into 5 main sections.
Section 3.1: File includes Go to Section 3.1
Section 3.2: Pin Definitions Go to Section 3.2
Section 3.3: Variable Definitions Go to Section 3.3
Section 3.4: Set up Function Go to Section 3.4
Section 3.5: Loop Function Go to Section 3.5
3.1 File includes
You can add programming instructions in one file to another using ‘File Includes’. You can use the ‘#include’ directive to tell the program to include the file after the name.
#include "pitches.h"
Why this is important? You can write commonly used code in one file and use it again and again by including it in other files. That way you don’t have to repeat writing the same code again and again.
Advantage of having musical notes in a separate file called ‘pitches.h’: On a later day, you can use the same file to define a song other than ‘Happy Birthday’ in one of your programs for Arduino.
In this program, we have separated the musical note definitions to another file called ‘pitches.h’ and included it in our main Troniction car code.
Note: .h extension is used to identify header files.
3.2 Pin Definitions
Using numbers as Arduino pin identifiers is very difficult to handle. Instead, you can define names for pin numbers.
#define enB 8
#define buzzerpin 2
Once you define Arduino pin number 2 as ‘buzzerpin’, it is easier to write the program. It is easier to understand what you have written at a later date.
‘#define’ directive is used to define names for Arduino pin numbers.
3.3 Variable Definitions
Some values in our car control program vary over time. For example, the signal sent by phone via Bluetooth continuously varies when we press different buttons. Therefore we need some identifiers to remember the latest value.
char btsignal;
We can define the character variable ‘btsignal’ to capture the signal sent from the phone via Bluetooth.
const long interval = 100;
Here we have defined the interval variable as 100 milliseconds. When we reverse the Troniction car the time interval between beep sounds is 100 milliseconds.
3.4 Set up Function
Pin modes are defined here. You can define an Arduino pin to be an INPUT or an OUTPUT. here we have defined the enA pin as an OUTPUT pin.
pinMode(enA, OUTPUT);
Pin mode: Whether the pin is an input or an output.
Arduino boards work in digital. Digital output pins can either be a high voltage (+5V) or a low voltage (0). You can programmatically define what is the output voltage of a pin should be. The following line of code sets one pin (in1) of the Arduino board to HIGH voltage (+5V).
digitalWrite(in1, HIGH);
Output pin status: Whether the pin is high or low.
Set Speed Function:
You can write functions in Arduino. If some code lines are going to repeat over and over, then you can create a function call the function again and again without writing the same code again and again.
void set_speed(int speed){
analogWrite(enA, speed);
analogWrite(enB, speed);
}
When we receive signals from the phone via Bluetooth we have to change the speed of the motors again and again depending on the speed signal. So we have created a separate function to handle the code repeating issue. Now we only have to call the function with speed value so that the writing speed to the PWM pins happens automatically.
set_speed(100)
Function: Enables us to create re-usable code snippets.
Void: Function’s return type ‘void’ means the function doesn’t output or return anything, it simply set output pin status.
There is another function in our file. That is for playing the Happy Birthday song.
void play_happy_birthday(){ ... }
The advantage of having the song code in a separate function is whenever we need to play the song, we can just call ‘play_happy_birthday’. (E.g. when a button is pressed we can call this function and play the happy birthday song.)
3.5 Loop Function
The code instructions inside the ‘loop function’ will be executed again and again, hence the word loop.
void loop() { ... }
The Troniction software will read the Bluetooth signal from your phone again and again.
Then it will decide what to do according to the received signal.
Table of possible signals from your phone and actions how to intepret and actuate them as car actions.
Bluetooth Signal | Car Action | |
---|---|---|
F | Go Forward | |
B | Reverse | |
L | Turn Left | |
R | Turn Right | |
1 | Speed 150 | |
9 | Speed 250 | |
V | Horn | |
X | Play Happy Birthday |
The last piece of code generates a beep tone when you reverse the Troniction car.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (reversing == true) {
tone(buzzerpin,5000,interval);
}
}
We can remove redundant speed values, consolidate similar actions and combine the two play_happy_birthday conditions from the above code.
This article was an introductory article which explains the basics with simple flow. For more optimized code, please visit Optimizing Arduino Car Program
Unlock the Secrets: Get Your Copy Now!
Discover the ultimate resource for building your very own remote-controlled Arduino-based electronic car. Download the comprehensive PDF file that contains everything you need to know, right at your fingertips.
Get the Book Now!