domingo, 21 de diciembre de 2014

Selecting melodies via bluetooth with your smartphone or tablet

In this example, some melodies will be played using a piezoelectric buzzer (speaker).
From a mobile device, using the app CommanDuinoBT, you could select the melody that you want to play or if you do not want to play any melody. By sending the characters '1', '2' or '3', you could play a melody and the character 's' to stop playing. This can be done by connecting your mobile device with Arduino via bluetooth.
Before continuing, you should know how the app CommanDuinoBT works, so it is recommended to check out this tutorial to start using this application.

The tones will be generated by the Arduino, so you have to use the tone() command with the frequencies of certain notes.

Needed hardware
  • Arduino Uno
  • Piezoelectric buzzer (speaker)
  • A 100Ω resistor
  • Bluetooth module
  • Breadboard
  • Cables 

In this case an HC-06 slave bluetooth module has been used.
Connect the speaker to digital pin 8 and is necessary to use digital pins 2 and 3 for bluetooth communication .
Before implementing the following circuit, it is important to ensure that the bluetooth module is configured as slave. To make the settings you can check out one of the following tutorials: Configuring a slave bluetooth module or configuration of a slave/master module as slave.


Circuit


After implementing the circuit, download and install the CommanDuino.h library if you have not yet done it. Here you can download the CommanDuino.h library and if you want to know how to install it, please review the tutorial How to install an Arduino library?

The code below uses the pitches.h extra file, which contains all the pitch values for typical notes. This will be usefull to generate a sound because the tone() command has 3 parameters, the pin on which to generate the tone, the frequency of the note in hertz and the duration of the sound in milliseconds: tone(pin, frequency, duration).

In addition, the code includes the CommanDuino.h library to control the Arduino from a mobile device. The CDsetting() function configures the bluetooth communication. The command() function lets you control the Arduino board; because of this, this function must be necessarily included in the code. The CDreceiveChar() function will be used to send a character to the Arduino, with which you can choose which melody will sound.


Code

// library used for serial communication
#include <SoftwareSerial.h>
#include <CommanDuino.h>     // library used to control Arduino board
                             // using the CommanDuino app
#include "pitches.h"  // contains all the pitch values 
                        // for typical notes
#define PAUSE       0      // a pause in the melody
#define WAIT      3000   // pause after finishing the melody and                                        // before starting the other one
#define BUZZER      8  // pin used for the buzzer
                       
// Array with all the notes of the melody 1
const int melody_1[] = {
    NOTE_A5, NOTE_B5, NOTE_AS5, NOTE_A5, NOTE_G5, NOTE_A5, PAUSE, PAUSE,   PAUSE, PAUSE,
    NOTE_G5, NOTE_G5, NOTE_F5, NOTE_E5, NOTE_G5, NOTE_F5, NOTE_F5, NOTE_E5,           NOTE_GS4, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_A5, NOTE_B5, NOTE_AS5, NOTE_A5, NOTE_G5, NOTE_A5, PAUSE, NOTE_B5,           NOTE_D6, PAUSE, NOTE_AS5, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_B5, NOTE_AS5, NOTE_A5, NOTE_G5, NOTE_AS5, NOTE_B5, NOTE_B5, NOTE_AS5,       PAUSE, NOTE_A5, PAUSE, PAUSE, PAUSE, PAUSE, //bis
    NOTE_B5, NOTE_AS5, NOTE_A5, NOTE_G5, NOTE_AS5, NOTE_B5, NOTE_B5, NOTE_AS5,       PAUSE, NOTE_A5, PAUSE, PAUSE, PAUSE, PAUSE, //bis
    NOTE_B5, NOTE_AS5, NOTE_A5, NOTE_G5, NOTE_A5, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_G5, NOTE_G5, NOTE_F5, NOTE_E5, NOTE_G5, NOTE_F5, NOTE_F5, NOTE_E5,           NOTE_A4
};

// Array with all the notes of the melody 2
const int melody_2[] = {
    NOTE_C4, PAUSE, NOTE_G3, NOTE_G3, NOTE_A3, PAUSE, NOTE_G3, 0, NOTE_B3, PAUSE,     NOTE_C4, PAUSE
};

// Array with all the notes of the melody 3
const int melody_3[] = {
    NOTE_E5, PAUSE, PAUSE, PAUSE, NOTE_E5, PAUSE, PAUSE, PAUSE, NOTE_E5, PAUSE,       PAUSE, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_E5, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, NOTE_E5, PAUSE, PAUSE,
    NOTE_FS57, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_FS48, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE,         PAUSE, PAUSE,
    NOTE_B45, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE,         PAUSE, PAUSE, PAUSE,
    NOTE_FS48, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_DS42, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_A4, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE,           PAUSE,
    NOTE_AS48, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_A45, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_GS43, PAUSE, PAUSE, PAUSE,
    NOTE_FS48, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_E5, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_FS56, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_GS56, PAUSE, PAUSE, PAUSE,
    NOTE_F5, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_FS56, PAUSE, PAUSE, PAUSE,
    NOTE_E5, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_C5, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE, PAUSE,
    NOTE_CS58, PAUSE, PAUSE, PAUSE,
    NOTE_AS48, PAUSE, PAUSE, PAUSE,
};   
int note;            // each note in an array
int totalNotes ;    // number of melody notes
int duration;        // note length
int betweenNotes;      // pause between two notes
char melodyNumber;   // melody number that will play


// It should be created an instance of the class CommanDuino, 
// called  App, to call the constructor. The constructor is 
// function that set the pins for the bluetooth communication.
// In this case the digitals pins 2 y 3.
CommanDuino App;

void setup(){
  App.CDsetting();     // set the data rate 
}                        // for the software serial port

void loop(){
  App.command();            // function to control the Arduino board   
  melodyNumber = App.CDreceiveChar(); // receive a char via bluetooth  
  
  switch (melodyNumber) {   // if the char received is '1',
  case '1':                // the melody_1 plays//
    totalNotes = 84;
    duration = 125;
    betweenNotes = 175;
    for(note = 0; note < totalNotes; note++){ // iterate over the 
                                              // notes of the melody 1
      if(melody_1[note] != PAUSE){  // if there is a note, it plays                  tone(BUZZER, melody_1[note], duration);    
        delay(duration);                     
      }
      else{                 // if there is a pause, wait the same
        delay(betweenNotes);  //  time that there is between 2 notes
      }
    }  
    delay(WAIT);   // wait a time before playing the other note
    break;

  case '2':    // if the char received is '2', the melody_2 plays
    totalNotes = 12;
    duration = 125;
    betweenNotes = 175;
    for(note = 0; note < totalNotes; note++){
      if(melody_2[note] != PAUSE){  // if there is a note, it plays
        tone(BUZZER, melody_2[note],duration);
        delay(duration);
      }
      else{                 // if there is a pause, wait the same 
        delay(betweenNotes);   // time that there is between 2 notes
      }             
    }  
    delay(WAIT);        // wait a time before playing the other note
    break;  

  case '3':   // if the char received is '3', the melody_3 plays
   totalNotes = 160;
   duration = 100;
   betweenNotes = 25;
   for(note = 0; note < totalNotes; note++){  // iterate over the 
                                              // notes of the melody 3
     if(melody_3[note] != PAUSE){   // if there is a note, it plays    
       tone(BUZZER, melody_3[note], duration); 
       delay(duration);              // wait the time of the note length
     }                               
     else{                  // if there is a pause, wait the same 
       delay(betweenNotes);   // time that there is between 2 notes  
     }
   }
   delay(WAIT);    // wait a time before playing the other note  
   break; 
   
  case 's':   // if the char received is 's', no melody will play. 
  break;
  }              
}
///////////////////////////////////////////////////////////////////////////////

Before loading the code, you have to create the document pitches.h.
To create the document pitches.h you must do the following:

The first thing is to be within the Arduino environment. Then, you must press Ctrl + Shift + N.
A window opens in the bottom of the Arduino IDE.
Next, you have to write the name of the new document, in this case: pitches.h.
This document contains all the frequencies that are associated with each musical note. It should be noted that the declaration of these variables can be done in the main file, but to avoid that the main code does not look too extensive, it is done in this way.

In the new window, you must paste the following code, this new document will be associated to the main program  by default.

// List of tones made by Brett Hagman
#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_DS42 320
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_FS48 380
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_GS43 430
#define NOTE_A4  440
#define NOTE_A45  450
#define NOTE_AS4 466
#define NOTE_AS48 480
#define NOTE_B4  494
#define NOTE_B45  510
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_CS58 580
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_FS56 760
#define NOTE_FS57 770
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_GS56 860
#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

///////////////////////////////////////////////////////////////////////////////

Compile to check for errors and load it to the Arduino. Here you must ensure that the Bluetooth module is not paired with any other device. If the LED of the bluetooth module flashes quickly, it means that it is not paired; otherwise, unpair it and then pair it with your mobile device.

After that, activate the bluetooth in the mobile and open the application.
Then press the CONNECT button, a list of all available devices will show, seek the bluetooth module and select it to pair it.




Once paired, in the app an Arduino board is displayed, press the Send Data button to send a character to the Arduino. In this example you can choose between 3 melodies; by sending the character '1', the speaker will play the melody 1; while sending character '2', it will play the melody 2 and with the character 3 it will play the melody 3. If you do not want to play any of them, send the character 's'.
The melody will play again and again unless you send another character.




And that's how the CDreceiveChar() function works.
The app CommanDuinoBT can be used to control sensors, motors, LED lights or any device via bluetooth. Use it to control the device you want in any project you want to implement, the limit is your imagination.
That's all, if you have any questions or comments let them in the comment box below.

Control Arduino with your smartphone or tablet

CommanDuinoBT is an application that can control over Bluetooth the Arduino Uno, Mini, Pro and Nano shields using a mobile device. This is achieved because the bluetooth that is in the mobile device sends signs to the bluetooth module  that is connected to the Arduino.

This application avoids to write many lines of code because from the mobile device is possible to configure the pins as either inputs or outputs. Similarly, from this app we can change the state of the pins to HIGH or LOW. Besides, you can generate a PWM signal to the pins that support this type of signal.
Here you can download the application from the Play Store.
Or scan it from your phone:




QR Code



 




Before you start using the application, it is necessary to connect the Bluetooth module to Arduino. Before that, we must first configure this module as a slave device. For the configuration you can check this tutorial if you have a master/slave module. Otherwise, if you have a slave module, you can check out this page: Configuring a slave bluetooth module.
Once you have configured the bluetooth module, connect it to the Arduino as shown in the circuit.

Needed hardware
  •   Arduino Uno
  •   2 resistors of 220 Ω
  •   2 LEDs
  •   Slave Bluetooth Module 
  •   Breadboard
  •   Cables


Circuit



After implementing the circuit, download and install the CommanDuino.h library. Here you can download the CommanDuino.h library and if you want to know how to install it, please review the tutorial How to install an Arduino library?

Then, go to the file menu and open the example simple from the CommanDuino library and load it into Arduino. If the circuit is implemented correctly, you can see that the LED on the module flashes quickly, which means it is not paired with any other bluetooth device.
Next, turn on the bluetooth in your mobile and link it with the bluetooth module (if it is not linked), to do this seek the bluetooth module with which you want to link and enter the password. The password is 4 digits and the default is 1111 or 1234. However, to configure the bluetooth module and change its settings, check out the tutorials: Configuring a slave bluetooth module or configuring a master/slave bluetooth module as slave.





                 
















After linking, open the application and press the CONNECT button, it will show a list with the available devices, look for the bluetooth module and click on it to pair it with your mobile.




At this point, you can see that the LED blinks more slowly or does not blink, indicating that it has been paired with your mobile device. Similarly, in the application an Arduino board is displayed, which indicates the same, your mobile is already paired with Arduino.



In the code you can see that the CommanDuino.h library is incuded; also, before the setup() function, you must create an instance of CommanDuino class, that allow to use the library functions. This library has 3 functions, CDsetting(), command() and CDreceiveChar().
Within the setup function you must call the CDsetting() function to configure the bluetooth communication. Digital pins 2 and 3 are used for the communication.
Within the loop, it is necessary to call the command() function, which allows to command or control the Arduino board, in other words, this function perform the instructions received by bluetooth.
Finally, the CDreceiveChar() function allows to receive by bluetooth one character, char type (obviously). In this example this function is not used because is not necessary. However, in this tutorial you can see an example of the usage of this function.


Code

// library used for serial communication
#include <SoftwareSerial.h>  
#include <CommanDuino.h>    // library used to control Arduino board
                                     // using the CommanDuino app

// It should be created an instance of the class CommanDuino, 
// called  App, to call the constructor. The constructor is 
// function that set the pins for the bluetooth communication.
// In this case the digitals pins 2 y 3.
CommanDuino App;

void setup(){
  App.CDsetting();  // set the data rate 
}                         // for the software serial port

void loop(){
  App.command();  // allows to command the Arduino board  
}
///////////////////////////////////////////////////////

Well, the app is very intuitive, easy to use and works as follows:

If you want to set up a pin, simply select it and a options dialog will open. There, you can choose if you want an input or output pin.
When selecting a pin, this will turn yellow. After choosing the settings and pressing the ok button, the button will change color.
If you set a pin as input, it will turn blue; if the pin is configured as output, you must choose whether you want to have a digital output or a PWM waveform (if the pin supports PWM signals). If you chose a digital output with the HIGH state, it will turn red; if placed in LOW, it will turn gray and if configured as PWM, it will turn red in the form of a square wave.

Additionally, there is the Send Data option, with which you can send a character to Aduino. This is useful if you want the Arduino to perform a task when receiving a certain character.

In this simple example, this application is used to turn on and off the LED that is connected to digital pin 13. Likewise with the green LED that is connected to pin 11, but this time using a PWM signal, you could change the LED brightness changing the duty cycle of the PWM signal.












   








With this simple example you have learned how to use the CommanduinoBT app. You can connect other devices, sensors or motors to Arduino and control them from your mobile.

To better understand the use of the CDreceiveChar() function, check out the tutorial: selecting melodies over Bluetooth.

That's all, if you have any questions or comments let them in the comment box below.