Code for voice recognition

//Voice recognition code

#if defined(ARDUINO) && ARDUINO >= 100

#include “Arduino.h

#include SoftwareSerial.h” SoftwareSerial port(12,13);

#else

#include “WProgram.h”

#include “NewSoftSerial.hNewSoftSerial port(12,13);

#endif

#include “EasyVR.h” EasyVR easyvr(port);

int speakerPin = 11;

int length = 100; /* number of notes */

char notes_NOI_DE_LA_MARE[] = mfsslSDSlls mfsslSDSls mfssfmrdrmd mfssfmrmrd; /* orden de las notas representadas por sus iniciales, un espacio no hay musica

*/

int beats_NOI_DE_LA_MARE[] = { 2, 1, 1, 2, 1, 1, 2, 1, 1, 3, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1,1, 3, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 3, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 3 };

char notes_HAPPY_BIRTHDAY[] = “sslsDS sslsRD ssMSlls RRDsls“; /* orden de las notas representadas por sus iniciales, un espacio no hay musica */

int beats_HAPPY_BIRTHDAY[] = { 2, 1, 2, 2, 2, 3, 1, 2, 1 , 2, 2, 2, 3, 1, 2, 1, 2, 2, 2, 2, 3,1, 2, 1, 2, 2, 2, 3 };

char notes_HIMNE_ALEGRIA[] = SDRRDSlsslSSl SDRRDSlsslSls lSslSDSslSDSlsll SDRRDSlsslSls; /* orden de las notas representadas por sus iniciales, un espacio no hay musica

*/

int beats_HIMNE_ALEGRIA[] = { 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2};

int tempo = 200;

void playTone (int tone, int duration) {

for (long i = 0; i < duration * 1000L; i+=tone * 2)

{ digitalWrite(speakerPin, HIGH); delayMicroseconds(tone); digitalWrite(speakerPin, LOW); delayMicroseconds(tone);

}

}

void playNote (char note, int duration) {

char names[] = {‘d‘,r,m’,f,s’,l,S,D, ‘R, ‘M’};

int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 900, 875 };

char names2[] = {‘R,M}; int tones2[] = {900,875}; for (int i = 0; i < 8; i++) {

if (names[i] == note) { playTone(tones[i], duration);

}

}

}

//Grupos y comandos enum Groups

{

GROUP_0 = 0,

GROUP_1 = 1,

};

enum Group0

{

G0_ARDUINO = 0,

};

enum Group1

{

G1_NOI_DE_LA_MARE = 0,

G1_HAPPY_BIRTHDAY = 1,

G1_HIMNE_ALEGRIA = 2,

};

EasyVRBridge bridge; int8_t group, idx; void setup()

{

pinMode(speakerPin, OUTPUT); pinMode(0, OUTPUT); pinMode(1, OUTPUT); pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); pinMode(9, OUTPUT);

if (bridge.check())

{

cli();

bridge.loop(0, 1, 12, 13);

}

// run normally Serial.begin(9600); port.begin(9600);

if (!easyvr.detect())

{

Serial.println(“EasyVR not detected!”); for (;;);

}

easyvr.setPinOutput(EasyVR::IO1, LOW); Serial.println(“ EasyVR detected!”); easyvr.setTimeout(5); easyvr.setLanguage(3);

group = EasyVR::TRIGGER; /*empezar un grupo para personalizar*/

}

void action(); void loop()

{ Serial.print(Say a command in Group“); Serial.println(group); easyvr.recognizeCommand(group);

do

{

/* esperar al comando hablado*/

}

while (!easyvr.hasFinished()); idx = easyvr.getCommand(); if (idx >= 0)

{

/* mostrar mensaje */ uint8_t train = 0;

char name[32]; Serial.print(“Command: ); Serial.print(idx);

if (easyvr.dumpCommand(group, idx, name, train))

{

Serial.print(” = “); Serial.println(name);

}

else Serial.println();

easyvr.playSound(0, EasyVR::VOL_FULL);

/* realiza una accn */ action();

}

else /* errores or fuera de tiempo límite */

{

if (easyvr.isTimeout()) Serial.println(Timed out, try again...”); int16_t err = easyvr.getError();

if (err >= 0)

{

Serial.print(“Error ); Serial.println(err, HEX);

}

group = GROUP_0;

}

}

void action()

{

switch (group)

{

case GROUP_0: switch (idx)

{

case G0_ARDUINO:

/* action code HERE */ group = GROUP_1; break;

}

break;

case GROUP_1: switch (idx)

{

case G1_NOI_DE_LA_MARE: group = GROUP_0;

for (int i = 0; i < length; i++) {

if (notes_NOI_DE_LA_MARE[i] == ‘d’) { digitalWrite(2, HIGH);

delay(100); digitalWrite(2, LOW); delay(1);

}

if (notes_NOI_DE_LA_MARE[i] == r) { digitalWrite(3, HIGH);

delay(100); digitalWrite(3, LOW); delay(1);

}

if (notes_NOI_DE_LA_MARE[i] == m’) { digitalWrite(4, HIGH);

delay(100); digitalWrite(4, LOW); delay(1);

}

if (notes_NOI_DE_LA_MARE[i] == f) { digitalWrite(5, HIGH);

delay(100); digitalWrite(5, LOW); delay(1);

}

if (notes_NOI_DE_LA_MARE[i] == s’) { digitalWrite(6, HIGH);

delay(100);

digitalWrite(6, LOW); delay(1); }

if (notes_NOI_DE_LA_MARE[i] == l) { digitalWrite(7, HIGH);

delay(100); digitalWrite(7, LOW); delay(1);

}

if (notes_NOI_DE_LA_MARE[i] == S) { digitalWrite(8, HIGH);

delay(100);

digitalWrite(8, LOW); delay(1);

}

if (notes_NOI_DE_LA_MARE[i] == D) { digitalWrite(9, HIGH);

delay(100); digitalWrite(9, LOW); delay(1);

}

if (notes_NOI_DE_LA_MARE[i] == ‘ ‘) { delay(beats_NOI_DE_LA_MARE[i] * tempo); /* rest */

} else {

playNote(notes_NOI_DE_LA_MARE[i], beats_NOI_DE_LA_MARE[i] * (tempo * 2));

}

/* pause between notes */ delay(tempo / 2);

}

break;

case G1_HAPPY_BIRTHDAY:

group = GROUP_0;

for (int i = 0; i < length; i++) {

if (notes_HAPPY_BIRTHDAY[i] == ‘d’) { digitalWrite(0, HIGH);

delay(100); digitalWrite(0, LOW); delay(1);

}

if (notes_HAPPY_BIRTHDAY[i] == r) { digitalWrite(1, HIGH);

delay(100); digitalWrite(1, LOW); delay(1);

}

if (notes_HAPPY_BIRTHDAY[i] == m’) { digitalWrite(2, HIGH);

delay(100); digitalWrite(2, LOW); delay(1);

}

if (notes_HAPPY_BIRTHDAY[i] == f) { digitalWrite(3, HIGH);

delay(100);

digitalWrite(3, LOW); delay(1);

}

if (notes_HAPPY_BIRTHDAY[i] == s’) { digitalWrite(4, HIGH);

delay(100); digitalWrite(4, LOW); delay(1);

}

if (notes_HAPPY_BIRTHDAY[i] == l) { digitalWrite(5, HIGH);

delay(100);

digitalWrite(5, LOW); delay(1);

}

if (notes_HAPPY_BIRTHDAY[i] == S) { digitalWrite(6, HIGH);

delay(100); digitalWrite(6, LOW); delay(1);

}

if (notes_HAPPY_BIRTHDAY[i] == D) { digitalWrite(7, HIGH);

delay(100); digitalWrite(7, LOW); delay(1);

}

if (notes_HAPPY_BIRTHDAY[i] == ‘ ‘) { delay(beats_HAPPY_BIRTHDAY[i] * tempo); /* rest */

} else {

playNote(notes_HAPPY_BIRTHDAY[i], beats_HAPPY_BIRTHDAY[i] * tempo);

}

/* pause between notes */ delay(tempo / 2);

}

break;

case G1_HIMNE_ALEGRIA:

group = GROUP_0;

for (int i = 0; i < length; i++) {

if (notes_HIMNE_ALEGRIA[i] == ‘d’) { digitalWrite(0, HIGH);

delay(100); digitalWrite(0, LOW); delay(1);

}

if (notes_HIMNE_ALEGRIA[i] == ‘r) { digitalWrite(1, HIGH);

delay(100); digitalWrite(1, LOW); delay(1);

}

if (notes_HIMNE_ALEGRIA[i] == ‘m’) { digitalWrite(2, HIGH);

delay(100);

digitalWrite(2, LOW); delay(1);

}

if (notes_HIMNE_ALEGRIA[i] == ‘f) { digitalWrite(3, HIGH);

delay(100); digitalWrite(3, LOW); delay(1);

}

if (notes_HIMNE_ALEGRIA[i] == ‘s’) { digitalWrite(4, HIGH);

delay(100);

digitalWrite(4, LOW); delay(1);

}

if (notes_HIMNE_ALEGRIA[i] == ‘l’) { digitalWrite(5, HIGH);

delay(100); digitalWrite(5, LOW); delay(1);

}

if (notes_HIMNE_ALEGRIA[i] == ‘S) { digitalWrite(6, HIGH);

delay(100); digitalWrite(6, LOW); delay(1);

}

if (notes_HIMNE_ALEGRIA[i] == ‘D) { digitalWrite(7, HIGH);

delay(100); digitalWrite(7, LOW); delay(1);

}

if (notes_HIMNE_ALEGRIA[i] == ‘ ‘) { delay(beats_HIMNE_ALEGRIA[i] * tempo); /* rest */

} else {

playNote(notes_HIMNE_ALEGRIA[i], beats_HIMNE_ALEGRIA[i] * tempo);

}

/* pausa entre notas */ delay(tempo / 2);

}

break;

}

break;

}

}

Leave a Reply

The best way to predict the future is to invent it (Alan Kay)