Troniction Troniction

Arduino Car Programming Guide

Arduino Car Programming Guide

You have to write software to control the car via Bluetooth. This guide will walk you through the complete process of programming your Troniction car.

Quick Navigation

  1. Download Source Code - Get the ready-to-upload files
  2. View Source Code - Explore the complete code
  3. Code Explanation - Understand how it works

Section 1: Download Source Code

Troniction Arduino car has 2 source code files:

  1. arduinocar.ino - Main car control code
  2. pitches.h - Musical note definitions

Choose your preferred download option below:

arduinocar.ino

Core Car Code

This is the core software that enables Troniction car running with Bluetooth control.

Download Car Code

pitches.h

Musical Note Definitions

This file defines the notes required to play the 'Happy Birthday' song by Troniction car.

Download Pitches

troniction_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: Source Code Contents

Explore the complete source code for both files below.

File 1: arduinocar.ino

#include "pitches.h"

#define enB 8
#define in4 9
#define in3 10
#define in2 11
#define in1 12
#define enA 13
#define buzzerpin 2

char btsignal;
bool reversing = false;
unsigned long previousMillis = 0;
const long interval = 100;

void setup() {
    pinMode(enA, OUTPUT);
    pinMode(in1, OUTPUT);
    pinMode(in2, OUTPUT);
    pinMode(enB, OUTPUT);
    pinMode(in3, OUTPUT);
    pinMode(in4, OUTPUT);
    pinMode(buzzerpin, OUTPUT);

    set_speed(150);

    digitalWrite(in1, HIGH);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, HIGH);

    Serial.begin(9600);
}

void loop() {
    if(Serial.available()){
        btsignal = Serial.read();
    }

    if(btsignal == 'F'){
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);
    }
    else if(btsignal == 'B'){
        reversing = true;
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);
    }
    else if(btsignal == 'L'){
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);
    }
    else if(btsignal == 'R'){
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);
    }
    else if(btsignal == '0' || btsignal == '1'){
        set_speed(150);
    }
    else if(btsignal == '2'){ set_speed(170); }
    else if(btsignal == '3'){ set_speed(180); }
    else if(btsignal == '4'){ set_speed(200); }
    else if(btsignal == '5'){ set_speed(210); }
    else if(btsignal == '6'){ set_speed(220); }
    else if(btsignal == '7'){ set_speed(230); }
    else if(btsignal == '8'){ set_speed(240); }
    else if(btsignal == '9'){ set_speed(250); }
    else if(btsignal == 'q'){ set_speed(255); }
    else if(btsignal == 'V' || btsignal == 'v'){
        tone(buzzerpin, 2000, 1000);
    }
    else if(btsignal == 'X' || btsignal == 'x'){
        play_happy_birthday();
    }
    else {
        reversing = false;
        digitalWrite(in1, HIGH);
        digitalWrite(in2, HIGH);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, HIGH);
    }

    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;
        if (reversing == true) {
            tone(buzzerpin, 5000, interval);
        }
    }
}

void set_speed(int speed){
    analogWrite(enA, speed);
    analogWrite(enB, speed);
}

void play_happy_birthday(){
    int melodyHappy[] = {
        NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4,
        NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4,
        NOTE_C4, NOTE_C4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_D4,
        NOTE_AS4, NOTE_AS4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4
    };

    int timeHappy[] = {
        4, 4, 2, 2, 2, 1,
        4, 4, 2, 2, 2, 1,
        4, 4, 2, 2, 4, 4, 2, 1,
        4, 4, 2, 2, 2, 1
    };

    int noteDuration = 0;
    int pauseBetweenNotes = 0;
    int sizeMelody = sizeof(melodyHappy)/sizeof(int);

    for (int i = 0; i < sizeMelody; i++) {
        noteDuration = 1000 / timeHappy[i];
        tone(buzzerpin, melodyHappy[i], noteDuration);
        pauseBetweenNotes = noteDuration * 1.30;
        delay(pauseBetweenNotes);
    }
}

File 2: pitches.h

This code 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.

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

Section 3: Code Explanation

Let's break down the code into 5 main sections and understand how each part works.

Code Sections Overview

  1. File Includes - How to include external files
  2. Pin Definitions - Setting up Arduino pins
  3. Variables - Data storage and management
  4. Setup Function - Initial configuration
  5. Loop Function - Main program loop

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.

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.

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;
const long interval = 100;

We can define the character variable 'btsignal' to capture the signal sent from the phone via Bluetooth. The interval variable is defined as 100 milliseconds for the beep sound when reversing.

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.

void setup() {
    pinMode(enA, OUTPUT);
    digitalWrite(in1, HIGH);
}

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 setup function also includes the set_speed function which allows us to control the motor speed:

void set_speed(int speed){
    analogWrite(enA, speed);
    analogWrite(enB, speed);
}

3.5 Loop Function

The code instructions inside the 'loop function' will be executed again and again, hence the word 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.

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

Code Optimization Note

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.


Steps to Build

  1. Get - Get the Components
  2. Wire - Wire together Components
  3. Write - Write the Code
  4. Upload - Upload the Code
  5. Control - Control the Car
  6. Share - Share with Others