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.

  1. arduinocar.ino
  2. 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 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

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();
    //Serial.println(t);
  }
 
  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'){
    set_speed(150);
  }

  else if(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 == 'W'){
    //frontlight = true;
  }
  
  else if(btsignal == 'w'){
    //frontlight = false;
  }
  
  else if(btsignal == 'V'){
    tone(buzzerpin,2000,1000);  
  }

  else if(btsignal == 'v'){
    tone(buzzerpin,2000,1000);  
  }    

  else if(btsignal == 'U'){
    //backlight = true;  
  }

  else if(btsignal == 'U'){
    //backlight = false;  
  }

  else if(btsignal == 'X'){
    play_happy_birthday();
  }

  else if(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) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // Reversing tone
    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, /*line 1*/
               4, 4, 2, 2, 2, 1, /*line 2*/
               4, 4, 2, 2, 4, 4, 2, 1, /*line 3*/
               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

#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

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);
    } 
  } 
Share on Twitter or Facebook

Get the Book


Arduino Car Building

You can download everything you need know to build your own Arduino-based electronic car as a PDF file for your reference.

Troniction Book