Voice Based Light Toggle With Android App In Arduino Mega 2560

In this article, you will learn about the voice based light toggle with Android app In Arduino Mega 2560.

Introduction
  • In this article, I will explain about the voice based light ON/OFF with Android app In Arduino Mega 2560.
  • It can be fully controlled by voice.
  • It can be controlled by the app.
Parts Of Lists
  • Arduino Mega2560
  • Bluetooth
  • Leds- 05
  • Hook Up Wires
  • Bread Board
Connection

Step 1: Connection from Bluetooth To Arduino Mega 2560
 
Bluetooth Board
Vcc 5v
Gnd Gnd
Tx Rx
Rx Tx
 
Step 2: Connection for the LEDs
 
Leds Postive side Negative side
Led1 12 Gnd
Led2 13 Gnd
Led3 14 Gnd
Led4 15 Gnd
Led5 16 Gnd
 
Programming
  1. //Coded By: Angelo Casimiro (4/27/14)    
  2. //Voice Activated Arduino (Bluetooth + Android)    
  3. //Feel free to modify it but remember to give credit    
  4. String voice;  
  5. int  
  6. led1 = 12, //Connect LED 1 To Pin #2    
  7.     led2 = 13, //Connect LED 2 To Pin #3    
  8.     led3 = 14, //Connect LED 3 To Pin #4    
  9.     led4 = 15, //Connect LED 4 To Pin #5    
  10.     led5 = 16; //Connect LED 5 To Pin #6    
  11. //--------------------------Call A Function-------------------------------//     
  12. void allon()  
  13. {  
  14.     digitalWrite(led1, HIGH);  
  15.     digitalWrite(led2, HIGH);  
  16.     digitalWrite(led3, HIGH);  
  17.     digitalWrite(led4, HIGH);  
  18.     digitalWrite(led5, HIGH);  
  19. }  
  20. void alloff()  
  21. {  
  22.     digitalWrite(led1, LOW);  
  23.     digitalWrite(led2, LOW);  
  24.     digitalWrite(led3, LOW);  
  25.     digitalWrite(led4, LOW);  
  26.     digitalWrite(led5, LOW);  
  27. }  
  28. void setup()  
  29. {  
  30.     Serial.begin(9600);  
  31.     pinMode(led1, OUTPUT);  
  32.     pinMode(led2, OUTPUT);  
  33.     pinMode(led3, OUTPUT);  
  34.     pinMode(led4, OUTPUT);  
  35.     pinMode(led5, OUTPUT);  
  36. }  
  37. void loop()  
  38. {  
  39.         while (Serial.available()) { //Check if there is an available byte to read    
  40.             delay(10); //Delay added to make thing stable    
  41.             char c = Serial.read(); //Conduct a serial read    
  42.             if (c == '#')  
  43.             {  
  44.                 break;  
  45.             } //Exit the loop when the # is detected after the word    
  46.             voice += c; //Shorthand for voice = voice + c    
  47.         }  
  48.         if (voice.length() > 0)  
  49.         {  
  50.             Serial.println(voice);  
  51.             //----------Control Multiple Pins/ LEDs----------//     
  52.             if (voice == "*all on")  
  53.             {  
  54.                 allon();  
  55.             } //Turn Off All Pins (Call Function)    
  56.             else if (voice == "*all off")  
  57.             {  
  58.                 alloff();  
  59.             } //Turn On  All Pins (Call Function)    
  60.   
  61.             //----------Turn On One-By-One----------//    
  62.             else if (voice == "*TV on")  
  63.             {  
  64.                 digitalWrite(led1, HIGH);  
  65.             } else if (voice == "*fan on")  
  66.             {  
  67.                 digitalWrite(led2, HIGH);  
  68.             } else if (voice == "*computer on")  
  69.             {  
  70.                 digitalWrite(led3, HIGH);  
  71.             } else if (voice == "*bedroom lights on")  
  72.             {  
  73.                 digitalWrite(led4, HIGH);  
  74.             } else if (voice == "*bathroom lights on")  
  75.             {  
  76.                 digitalWrite(led5, HIGH);  
  77.             }  
  78.             //----------Turn Off One-By-One----------//    
  79.             else if (voice == "*TV off")   
  80.             {  
  81.                 digitalWrite(led1, LOW);  
  82.             } else if (voice == "*fan off")  
  83.             {  
  84.                 digitalWrite(led2, LOW);  
  85.             } else if (voice == "*computer off")  
  86.             {  
  87.                 digitalWrite(led3, LOW);  
  88.             } else if (voice == "*bedroom lights off")   
  89.             {  
  90.                 digitalWrite(led4, LOW);  
  91.             } else if (voice == "*bathroom lights off")   
  92.             {  
  93.                 digitalWrite(led5, LOW);  
  94.             }  
  95.             voice = "";  
  96.         }  
  97.     } //Reset the variable after initiating  
Explanation
  • We can control the LEDs by the voice, through the Android app
  • If we want to turn ON the light, we set the voice as the "Light ON"
  • If we want to turn OFF the light, we set the voice as the "Light OFF"
  • We can also set the voice for "All on" and "All Off".
  • They can work on a one-by-one process.
  • We want to download the app from the play store. 
Output