//Jarvis v 4.3 by C.V.Hariharan //Control upto 2 devices separately int light=13; int fan=12; int tv=8; int charger=4; int i=0; int j=0; String in; String detected[4]; int pin[2]; int m=0; String devices[]={"light","fan","tv","charger","lights","television","TV"}; void setup() { pinMode(light, OUTPUT); pinMode(fan, OUTPUT); pinMode(tv, OUTPUT); pinMode(charger, OUTPUT); Serial.begin(9600); } void loop() { while(Serial.available()==0); Serial.flush(); in = Serial.readString(); Serial.println(in); analyse(in); if(m!=1) { if(in.indexOf("on")>=0) { Serial.println("ON"); object("on"); //digitalWrite(led, HIGH); //Serial.println("Led on"); } if(in.indexOf("off")>=0) { Serial.println("OFF"); object("off"); //digitalWrite(led, LOW); //Serial.println("Led off"); } } } void analyse(String data) { j=0; for(i=0;i<7;i++) { if(in.indexOf(devices[i])>=0) { detected[j]=devices[i]; j++; } } for(i=0;i<4;i++) { Serial.print(detected[i]+" "); Serial.print(j); } mapping(); if(in.indexOf("on")>=0 && in.indexOf("off")>=0) //If both on and off commands are in the same sentence { m=1; if(in.indexOf("on")>in.indexOf("off")) //Here the first device has to be off and the second has to be on { if(in.indexOf(detected[0])in.indexOf(detected[1])) // To check which device comes first in the sentence { Serial.println(detected[0]+" HIGH"); digitalWrite(pin[0], HIGH); Serial.println(detected[1]+" LOW"); digitalWrite(pin[1], LOW); } } if(in.indexOf("off")>in.indexOf("on")) //Here the first device has to be on and the second has to be off { if(in.indexOf(detected[0])in.indexOf(detected[1])) // To check which device comes first in the sentence { Serial.println(detected[0]+" LOW"); digitalWrite(pin[0], LOW); Serial.println(detected[1]+" HIGH"); digitalWrite(pin[1], HIGH); } } } return; } void mapping() //To convert the the words to pin numbers { if(j>1){ if(detected[0] == "lights" || detected[0]=="light") { pin[0] = 13; } if(detected[0] == "fan") { pin[0] = 12; } if(detected[0] == "tv" || detected[0]=="television" || detected[0]=="TV") { pin[0] = 8; } if(detected[0] == "charger") { pin[0] = 4; } if(detected[1] == "lights" || detected[1]=="light") { pin[1] = 13; } if(detected[1] == "fan") { pin[1] = 12; } if(detected[1] == "tv" || detected[1]=="television" || detected[1]=="TV") { pin[1] = 8; } if(detected[1] == "charger") { pin[1] = 4; } } return; } void object(String data) //Executes when only one command, either on or off is in the sentence { if(data == "on") { if(in.indexOf("light")>=0 || in.indexOf("lights")>=0) { digitalWrite(light,HIGH); Serial.println("Lights ON"); //delay(3000); } if(in.indexOf("fan")>=0) { digitalWrite(fan, HIGH); Serial.println("Fan ON"); //delay(3000); } if(in.indexOf("television")>=0 || in.indexOf("TV")>=0 || in.indexOf("tv")>=0) { digitalWrite(tv, HIGH); Serial.println("Television ON"); // delay(3000); } if(in.indexOf("charger")>=0) { digitalWrite(charger, HIGH); Serial.println("Charger ON"); //delay(3000); } if(in.indexOf("everything")>=0) { digitalWrite(light, HIGH); digitalWrite(charger, HIGH); digitalWrite(fan, HIGH); digitalWrite(tv, HIGH); } } if(data == "off") { { if(in.indexOf("light")>=0 || in.indexOf("lights")>=0) { digitalWrite(light,LOW); Serial.println("Light Off"); // delay(3000); } if(in.indexOf("fan")>=0) { digitalWrite(fan, LOW); Serial.println("Fan Off"); //delay(3000); } if(in.indexOf("television")>=0 || in.indexOf("TV")>=0 || in.indexOf("tv")>=0) { digitalWrite(tv, LOW); Serial.println("Television Off"); //delay(3000); } if(in.indexOf("charger")>=0) { digitalWrite(charger, LOW); Serial.println("Charger Off"); //delay(3000); } if(in.indexOf("everything")>=0) { digitalWrite(light, LOW); digitalWrite(charger, LOW); digitalWrite(fan, LOW); digitalWrite(tv, LOW); } } } return; }