Monday, December 18, 2017

Voice Recognition Sketch

// Voice Recognition
// For BT-Speech-MIT
// Developed by JBWylzan
// www.ihackrobot.blogspot.com



#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX
// Connect the HC-05 TX to Arduino pin 2 RX.
// Connect the HC-05 RX to Arduino pin 3 TX through a voltage divider.
//
int ledPin = 13;
String vox;
int x= 10;
int var=0;


void setup()
{
   // Serial.begin(9600);
   // BTserial.println("Speech Recognition");
    pinMode(ledPin, OUTPUT);
     pinMode(8, OUTPUT);
     pinMode(9, OUTPUT);
     pinMode(10, OUTPUT);
    BTserial.begin(9600); 
}

void loop()
{
     while (BTserial.available())
     {
      delay(x);
      char c = BTserial.read();
      vox += c;
     }
if (vox.length()>0){
    //BTserial.println(vox);
   // digitalWrite(ledPin, HIGH);
    if(vox == "activate")
     {
        digitalWrite(ledPin, HIGH);
     }
   
      if(vox=="off")  
      {
        digitalWrite(ledPin, LOW);
        digitalWrite(8, LOW);
          digitalWrite(9, LOW);
            digitalWrite(10, LOW);
      }

       if(vox=="blue")  
      {
        digitalWrite(8, HIGH);
          digitalWrite(9, LOW);
            digitalWrite(10, LOW);
      }

      if(vox=="bathroom")  
      {
        digitalWrite(9, HIGH);
          digitalWrite(8, LOW);
            digitalWrite(10, LOW);
      }

      if(vox=="car")  
      {
        digitalWrite(10, HIGH);
          digitalWrite(8, LOW);
            digitalWrite(9, LOW);
      }


   if(vox=="rainbow")  
      {
        //pinMode(ledPin, OUTPUT);
        while(var < 5) {        
        colors();
          delay(x);         
          var++;
        digitalWrite(10, LOW);    //same as ledPin
   }
         var=0;    //variable reset
      }

      vox="";   //voice reset
      }
  }

/*      if(vox=="blink")  
      {
        //pinMode(ledPin, OUTPUT);
        while(var < 5) {        
        wink();
          delay(x);         
          var++;
        digitalWrite(13, HIGH);    //same as ledPin
   }
         var=0;    //variable reset
      }

      vox="";   //voice reset
      }
  }

*/
void wink(){
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

void colors() {
  digitalWrite(8, HIGH); 
  digitalWrite(9, LOW);   
  digitalWrite(10, LOW);
  delay(1000);          

     digitalWrite(8, LOW); 
  digitalWrite(9, HIGH);   
  digitalWrite(10, LOW);
  delay(1000);          

     digitalWrite(8, LOW); 
  digitalWrite(9, LOW);   
  digitalWrite(10, HIGH);
  delay(1000);          
}

No comments:

Post a Comment