Lesson 05 - Bluetooth

Learn how to connect a bluetooth module to your Arduino and then use that to pair to an iOS or Android phone

By Kevin McAleer,    3 Minutes


LiveStream video


What you’ll learn

In this lesson you will learn how to connect a HC-08 (or HC-09 / HC-10) bluetooth module to your Arduino and then use that to pair to an iOS or Android phone. We will then modify the code written in previous lessons to remotely control our SMARS.


Things’s you’ll needs

Before you begin, you’ll need to make sure you have a couple of things before you start this lesson:

  • A SMARS robot
  • A USB Cable to connect to your computer
  • A Computer that can run the Arduino IDE
  • An HC-08 or higher Bluetooth module

Preparation

  1. Connect the SMARS to the USB cable, and the USB Cable to your computer
  2. Launch the Arduino IDE on your computer
  3. Create a new Sketch, by clicking the New Sketch button

Lets code

You can download the code below from GitHub, but its better to type it in yourself line by line, as you will get to understand what each line means.

Make sure to download the pitches.h and melody.h, from the github link above, to the same folder where you create the code below.


#include <Dabble.h>
#include <SoftwareSerial.h>
#include "pitches.h"

#define CUSTOM_SETTINGS

#define INCLUDE_GAMEPAD_MODULE

// set Motor A to Arduino Pins
int motor_A = 12; // official Arduino Motor Shield uses D12
int motor_B = 13; // official Arduino Motor Shield uses D13
int buzzer = 4;

// set the Motor Speed using the Arduino Pins
int motor_A_speed = 10; // official Arduino Motor Shield uses D3
int motor_B_speed = 11; // official Arduino Motor Shield uses D11

// set the time between motor on and motor off
int wait_in_milliseconds = 100;

void setup() {
  // put your setup code here, to run once:
  Dabble.begin(9600);
  Serial.begin(9600);

    // set the Arduino pin to OUTPUT mode
  pinMode(motor_A, OUTPUT);
  pinMode(motor_B, OUTPUT);
  pinMode(buzzer, OUTPUT);
}

// notes in the melody:
int melody[] = {
  F3, D3, AS2, AS2, AS2, C3, D3, DS3, F3, F3, F3 ,D3 
};

int noteDurations[] = {
  8,  8,   4,   4,   8,  8,  8,  8,  4,  4,   4,  8 
};

struct Note {
 int key;
 int duraiton;
};

void playMelody() {
for (int thisNote = 0; thisNote < 12 ; thisNote++) {

    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(4, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  Dabble.processInput();
  if (GamePad.isUpPressed()) 
    forward();
  if (GamePad.isDownPressed()) 
    backward();
  
  if (GamePad.isLeftPressed()) turnLeft();
  if (GamePad.isRightPressed()) turnRight();
  if (GamePad.isTrianglePressed()) beep();
  if (GamePad.isSquarePressed()) playMelody();
}

< Previous Next >