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.

No hay comentarios:

Publicar un comentario