Leonardo - Warning System + Voice Recognition

I designed and built a warning system, using 3 sensors, that detect when an object is approaching. The system also has a toggle switch and a buzzer.

When the toggle switch is in the OFF position: If there are no objects nearby the LED turns GREEN, if either sensor 1 or 3 detect an object the LED turns YELLOW and finally if the object is between sensors 1 and 2 or 2 and 3 the LED turns RED.

When the toggle switch is in the ON position: Same as OFF position + buzzer beeping depending on color of LED.

Now I would like to add voice recognition to my system. I purchased a JY-MCU Bluetooth Wireless Serial Port Module and was able to test it using a simple program that changed the color of my LED depending on the command I gave it using my phone.

My problem now is that whenever I tried to add the voice recognition capability to my warning system its not working. The code I’m using makes sense but I’m new at this so I might be missing something. I’ll be posting 3 codes: 1)Warning System 2)Test for Voice Recognition 3) Current non-working code.

//pins ultrasonic
int triggerPort1 = 2;
int echoPort1 = 3;
int triggerPort2 = 4;
int echoPort2 = 5;
int triggerPort3 = 6;
int echoPort3 = 7;
const int buzzer =  8;

//color pins
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
const int ts = 12;

// Init our Vars
int colorRed;
int colorGreen;
int colorBlue;
int tsState = 0;

void setup()
{
  //declare INPUTS and OUTPUTS for 3 sensors, buzzer and toggle switch
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(triggerPort1, OUTPUT);
  pinMode(echoPort1, INPUT);
  pinMode(triggerPort2, OUTPUT);
  pinMode(echoPort2, INPUT);
  pinMode(triggerPort3, OUTPUT);
  pinMode(echoPort3, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(ts, INPUT);

  digitalWrite(buzzer, HIGH);
}

void loop()
{
  tsState = digitalRead(ts); //read toggle switch

  //sensor 1
  long time1, distance1;
  digitalWrite(triggerPort1, LOW);
  digitalWrite(triggerPort1, HIGH);
  delayMicroseconds(10);
  digitalWrite( triggerPort1, LOW);
  time1 = pulseIn(echoPort1, HIGH); //time1 it took for signal to comeback
  distance1 = (time1 / 2) / 29.1; //convert time to distance

  //sensor 3
  long time2, distance2;
  digitalWrite(triggerPort2, LOW);
  digitalWrite(triggerPort2, HIGH);
  delayMicroseconds(10);
  digitalWrite( triggerPort2, LOW);
  time2 = pulseIn(echoPort2, HIGH); //time2 it took for signal to comeback
  distance2 = (time2 / 2) / 29.1; //convert time to distance

  //sensor 3
  long time3, distance3;
  digitalWrite(triggerPort3, LOW);
  digitalWrite(triggerPort3, HIGH);
  delayMicroseconds(10);
  digitalWrite( triggerPort3, LOW);
  time3 = pulseIn(echoPort3, HIGH); //time3 it took for signal to comeback
  distance3 = (time3 / 2) / 29.1; //convert time to distance

  Serial.print(distance1);
  Serial.print(" CM 1 \t"); //Prints distance1 on serial monitor
  //Serial.println("");

  Serial.print(distance2);
  Serial.print(" CM 2  \t");//Prints distance2 on serial monitor
  //Serial.println("");

  Serial.print(distance3);
  Serial.print(" CM 3  \t"); //Prints distance3 on serial monitor
  Serial.println("");

  if (tsState == HIGH) // if switch high buzzer will turn on
  {
    // if all sensors have distance >50 led = green and buzzer=off
    if (distance1 > 50 && distance2 > 50 && distance3 > 50)
    {
      setColor(255, 0, 255); // green
      digitalWrite(buzzer, HIGH);
      delay(1000);
    }
    //if just sensor1 distance <= 50 turn yellow and turn on buzzer
    else if ( (distance1 <= 50 && distance2 > 50 && distance3 > 50) )
    {
      setColor(0, 0, 255); // yellow
      digitalWrite(buzzer, HIGH);
      delay(800);
      digitalWrite(buzzer, LOW);
      delay(200);
      //delay(1000);
    }
    //if just sensor3 distance <= 50 turn LED yellow and turn on buzzer
    else if ( (distance3 <= 50 && distance2 > 50 && distance1 > 50) )
    {
      setColor(0, 0, 255); // yellow
      digitalWrite(buzzer, HIGH);
      delay(800);
      digitalWrite(buzzer, LOW);
      delay(200);
      //delay(1000);
    }
    //if 2 sensors distance <50 turn LED red and buzzer beeps faster
    else if ( (distance1 < 50 && distance2 < 50) || (distance3 < 50 && distance2 < 50))
    {
      setColor(0, 255, 255); // red
      digitalWrite(buzzer, HIGH);
      delay(250);
      digitalWrite(buzzer, LOW);
      delay(700);
      //delay(1000);
    }
  }

  else //if toggle is low, only LED works, buzzer is off
  {
    digitalWrite(buzzer, HIGH);
    
    // if all sensors have distance >50 led = green
    if (distance1 > 50 && distance2 > 50 && distance3 > 50)
    {
      setColor(255, 0, 255); // green
      delay(1000);
    }
    //if just sensor3 distance <= 50 turn LED yellow and turn on buzzer
    else if ( (distance1 <= 50 && distance2 > 50 && distance3 > 50) )
    {
      setColor(0, 0, 255); // yellow
      delay(1000);
    }
    //if just sensor3 distance <= 50 turn LED yellow and turn on buzzer
    else if ( (distance3 <= 50 && distance2 > 50 && distance1 > 50) )
    {
      setColor(0, 0, 255); // yellow
      delay(1000);
    }
    //if 2 sensors distance <50 turn LED red and buzzer beeps faster
    else if ( (distance1 < 50 && distance2 < 50) || (distance3 < 50 && distance2 < 50))
    {
      setColor(0, 255, 255); // red
      delay(1000);
    }
  }
}
 
//function to change LED colors
void setColor(int red, int green, int blue)
{
  /*#ifdef COMMON_ANODE
  red = 255 - red;
  green = 255 - green;
  blue = 255 - blue;
  #endif
  */
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

Sorry for double post, it didn’t allow me to post more than 9k characters.

//color pins
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
const int ts = 12;

// Init our Vars
String voice;
int colorRed;
int colorGreen;
int colorBlue;
int tsState = 0;

void setup()
{
  Serial1.begin(9600);
  //declare INPUTS and OUTPUTS for 3 sensors, buzzer and toggle switch
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(triggerPort1, OUTPUT);
  pinMode(echoPort1, INPUT);
  pinMode(triggerPort2, OUTPUT);
  pinMode(echoPort2, INPUT);
  pinMode(triggerPort3, OUTPUT);
  pinMode(echoPort3, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(ts, INPUT);

  digitalWrite(buzzer, HIGH);
}

void loop()
{ 
  while (Serial1.available()){  //Check if there is an available byte to read
  delay(10); //Delay added to make thing stable 
  char c = Serial1.read(); //Conduct a serial read
  if (c == '#') {break;} //Exit the loop when the # is detected after the word
  voice += c; //Shorthand for voice = voice + c
  }  
  if (voice.length() > 0) {
    Serial1.println(voice); 
//-----------------------------------------------------------------------//    
  //----------Control Multiple Pins/ LEDs----------//  
  if(voice == "*test") {test();}  //Turn Off All Pins (Call Function)
  else if(voice == "*turn off"){off();} //Turn On  All Pins (Call Function)  
  voice="";}}

//function to change LED colors
void setColor(int red, int green, int blue)
{
  
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

void test()
{
  setColor(255, 0, 255); // green
  delay(1000);
  setColor(0, 0, 255); // yellow
  delay(1000);
  setColor(0, 255, 255); // red
  delay(1000);
}

void off()
{
  setColor(255, 255, 255);
}
///pins ultrasonic
int triggerPort1 = 2;
int echoPort1 = 3;
int triggerPort2 = 4;
int echoPort2 = 5;
int triggerPort3 = 6;
int echoPort3 = 7;
const int buzzer =  8;

//color pins
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
const int ts = 12;

// Init our Vars
String voice;
int colorRed;
int colorGreen;
int colorBlue;
int tsState = 0;

void setup()
{
  Serial1.begin(9600);
  Serial.begin(9600);
  //declare INPUTS and OUTPUTS for 3 sensors, buzzer and toggle switch
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(triggerPort1, OUTPUT);
  pinMode(echoPort1, INPUT);
  pinMode(triggerPort2, OUTPUT);
  pinMode(echoPort2, INPUT);
  pinMode(triggerPort3, OUTPUT);
  pinMode(echoPort3, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(ts, INPUT);

  digitalWrite(buzzer, HIGH);
}

void loop()
{ 
  //sensor 1
  long time1, distance1;
  digitalWrite(triggerPort1, LOW);
  digitalWrite(triggerPort1, HIGH);
  delayMicroseconds(10);
  digitalWrite( triggerPort1, LOW);
  time1 = pulseIn(echoPort1, HIGH); //time1 it took for signal to comeback
  distance1 = (time1 / 2) / 29.1; //convert time to distance

  //sensor 3
  long time2, distance2;
  digitalWrite(triggerPort2, LOW);
  digitalWrite(triggerPort2, HIGH);
  delayMicroseconds(10);
  digitalWrite( triggerPort2, LOW);
  time2 = pulseIn(echoPort2, HIGH); //time2 it took for signal to comeback
  distance2 = (time2 / 2) / 29.1; //convert time to distance

  //sensor 3
  long time3, distance3;
  digitalWrite(triggerPort3, LOW);
  digitalWrite(triggerPort3, HIGH);
  delayMicroseconds(10);
  digitalWrite( triggerPort3, LOW);
  time3 = pulseIn(echoPort3, HIGH); //time3 it took for signal to comeback
  distance3 = (time3 / 2) / 29.1; //convert time to distance

  Serial.print(distance1);
  Serial.print(" CM 1 \t"); //Prints distance1 on serial monitor
  //Serial.println("");

  Serial.print(distance2);
  Serial.print(" CM 2  \t");//Prints distance2 on serial monitor
  //Serial.println("");

  Serial.print(distance3);
  Serial.print(" CM 3  \t"); //Prints distance3 on serial monitor
  Serial.println("");
  
  while (Serial1.available()) //Check if there is an available byte to read
  { 
    delay(10); //Delay added to make thing stable 
    char c = Serial1.read(); //Conduct a serial read
    if (c == '#') {break;} //Exit the loop when the # is detected after the word
    voice += c; //Shorthand for voice = voice + c
  }  
  if (voice.length() > 0) 
  {
    Serial1.println(voice);  
    if(voice == "*test") {test();}  //Turn Off All Pins (Call Function)
    else if(voice == "*turn off"){off();} //Turn On  All Pins (Call Function) 
    else if(voice == "driving")
    {
          digitalWrite(buzzer, HIGH);
      
      // if all sensors have distance >50 led = green
      if (distance1 > 50 && distance2 > 50 && distance3 > 50)
      {
        setColor(255, 0, 255); // green
        delay(1000);
      }
      //if just sensor3 distance <= 50 turn LED yellow and turn on buzzer
      else if ( (distance1 <= 50 && distance2 > 50 && distance3 > 50) )
      {
        setColor(0, 0, 255); // yellow
        delay(1000);
      }
      //if just sensor3 distance <= 50 turn LED yellow and turn on buzzer
      else if ( (distance3 <= 50 && distance2 > 50 && distance1 > 50) )
      {
        setColor(0, 0, 255); // yellow
        delay(1000);
      }
      //if 2 sensors distance <50 turn LED red and buzzer beeps faster
      else if ( (distance1 < 50 && distance2 < 50) || (distance3 < 50 && distance2 < 50))
      {
        setColor(0, 255, 255); // red
        delay(1000);
      }
    }
     else if(voice == "merge") 
     {
        // if all sensors have distance >50 led = green and buzzer=off
      if (distance1 > 50 && distance2 > 50 && distance3 > 50)
      {
        setColor(255, 0, 255); // green
        digitalWrite(buzzer, HIGH);
        delay(1000);
      }
      //if just sensor1 distance <= 50 turn yellow and turn on buzzer
      else if ( (distance1 <= 50 && distance2 > 50 && distance3 > 50) )
      {
        setColor(0, 0, 255); // yellow
        digitalWrite(buzzer, HIGH);
        delay(800);
        digitalWrite(buzzer, LOW);
        delay(200);
        //delay(1000);
      }
      //if just sensor3 distance <= 50 turn LED yellow and turn on buzzer
      else if ( (distance3 <= 50 && distance2 > 50 && distance1 > 50) )
      {
        setColor(0, 0, 255); // yellow
        digitalWrite(buzzer, HIGH);
        delay(800);
        digitalWrite(buzzer, LOW);
        delay(200);
        //delay(1000);
      }
      //if 2 sensors distance <50 turn LED red and buzzer beeps faster
      else if ( (distance1 < 50 && distance2 < 50) || (distance3 < 50 && distance2 < 50))
      {
        setColor(0, 255, 255); // red
        digitalWrite(buzzer, HIGH);
        delay(250);
        digitalWrite(buzzer, LOW);
        delay(700);
        //delay(1000);
      }
    }
       
    voice="";
  }
}
  


//function to change LED colors
void setColor(int red, int green, int blue)
{
  
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

void test()
{
  setColor(255, 0, 255); // green
  delay(1000);
  setColor(0, 0, 255); // yellow
  delay(1000);
  setColor(0, 255, 255); // red
  delay(1000);
}

void off()
{
  setColor(255, 255, 255);
}

Next time, just Attach your file. Link is under the Reply box.

CrossRoads: Next time, just Attach your file. Link is under the Reply box.

Not a good idea. Unless something has happened recently, the "Attachments and other options" is badly broken.

If you are very lucky, it will work for you. But for many, it does not work at all.

Really?

Maybe folks have issues with file types or size limitations.

CrossRoads: Really?

Yes, really. They have fouled up the php that serves the link in such a way that it will not open in the browser, but requires to be downloaded as a file and/ or fed to a "helper application". In some browser configurations, this can be reflected back to the browser, in others it will feed to a nominated "helper" such as in the case of your file here and on this particular system, IrfanView, but in other configurations it even fails at the point of saving the file thus cannot be used at all.

CrossRoads: Maybe folks have issues with file types or size limitations.

Nope, no such problems. It is (apparently deliberate) fouling of the attributes served to the browser. Has been mentioned in the "Website and Forum" forum but remains thoroughly fouled up - from difficult to entirely unusable.

Well, I’ll attach them if that helps.

RGBwBuzzer_ToggleSwitch.ino (4.47 KB)

VoiceRecognition.ino (3.89 KB)

RGBwVoiceRec.ino (5.23 KB)

It is (apparently deliberate) fouling of the attributes served to the browser.

Hmm, is that why Adobe keeps opening .ino files? I thought my file associations were just screwed up, as I can't get the IDE to be selected as the default program to open them.

CrossRoads: Hmm, is that why Adobe keeps opening .ino files? I thought my file associations were just screwed up, as I can't get the IDE to be selected as the default program to open them.

That will indeed be the problem.

Discussed in this thread.

But then - you were already in that thread! :astonished:

No help? :(