0% found this document useful (0 votes)
25 views

Code W

This document contains code for an Arduino project that controls 4 relays and LEDs using buttons or voice commands. It defines pin connections for the buttons, relays, LEDs, and buzzer. The setup function initializes the pin modes and turns off the relays by default. The loop continuously checks the button states and voice commands to toggle the relays and update their saved states in EEPROM memory. It contains if-statements to handle various commands for controlling each relay and LED.

Uploaded by

Rizwan Fayyaz
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Code W

This document contains code for an Arduino project that controls 4 relays and LEDs using buttons or voice commands. It defines pin connections for the buttons, relays, LEDs, and buzzer. The setup function initializes the pin modes and turns off the relays by default. The loop continuously checks the button states and voice commands to toggle the relays and update their saved states in EEPROM memory. It contains if-statements to handle various commands for controlling each relay and LED.

Uploaded by

Rizwan Fayyaz
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <SoftwareSerial.

h> // call for serial port monitor


#include <EEPROM.h>

char c; //Variable for storing received data

const int Led1 =A0;


const int Led2 =A1;
const int Led3 =3;

const int Button1Pin = A5;


const int Button2Pin = A4;
const int Button3Pin = A3;
const int Button4Pin = A2;

const int Relay1 =4;


const int Relay2 =5;
const int Relay3 =6;
const int Relay4 =7;

const int Buzzer = 2; // buzzer pin no

int i;
String voice;

int mode;

boolean relay1flag =false;


boolean relay2flag =false;
boolean relay3flag =false;
boolean relay4flag =false;

boolean sw1flag =false;


boolean sw2flag =false;
boolean sw3flag =false;
boolean sw4flag =false;

void setup()
{

Serial.begin(9600); // serial port bit per secnd speed

pinMode(Button1Pin, INPUT); //assigns button pins as inputs


pinMode(Button2Pin, INPUT); //assigns button pins as inputs
pinMode(Button3Pin, INPUT); //assigns button pins as inputs
pinMode(Button4Pin, INPUT); //assigns button pins as inputs

digitalWrite(Button1Pin, HIGH); //sets internal pulldown resistors


digitalWrite(Button2Pin, HIGH); //sets internal pulldown resistors
digitalWrite(Button3Pin, HIGH); //sets internal pulldown resistors
digitalWrite(Button4Pin, HIGH); //sets internal pulldown resistors

pinMode (Buzzer,OUTPUT);
pinMode (Led1,OUTPUT);
pinMode (Led2,OUTPUT);
pinMode (Led3,OUTPUT);

pinMode (Relay1,OUTPUT);
pinMode (Relay2,OUTPUT);
pinMode (Relay3,OUTPUT);
pinMode (Relay4,OUTPUT);

digitalWrite(Buzzer,LOW);

digitalWrite(Relay1,LOW);
digitalWrite(Relay2,LOW);
digitalWrite(Relay3,LOW);
digitalWrite(Relay4,LOW);

for(int i=0;i<2;i++)
{
digitalWrite(Relay1,HIGH);
digitalWrite(Relay2,HIGH);
digitalWrite(Relay3,HIGH);
digitalWrite(Relay4,HIGH);
digitalWrite(Led1,HIGH);
digitalWrite(Led2,HIGH);
digitalWrite(Led3,HIGH);
digitalWrite(Buzzer,HIGH);
delay(500);
digitalWrite(Buzzer,LOW);
digitalWrite(Relay1,LOW);
digitalWrite(Relay2,LOW);
digitalWrite(Relay3,LOW);
digitalWrite(Relay4,LOW);
digitalWrite(Led1,LOW);
digitalWrite(Led2,LOW);
digitalWrite(Led3,LOW);
delay(500);
}

relay1flag=EEPROM.read(1);delay(20);
relay2flag=EEPROM.read(2);delay(20);
relay3flag=EEPROM.read(3);delay(20);
relay4flag=EEPROM.read(4);delay(20);

digitalWrite(Buzzer,HIGH);delay(100);digitalWrite(Buzzer,LOW);delay(100);
digitalWrite(Buzzer,HIGH);delay(100);digitalWrite(Buzzer,LOW);delay(100);
digitalWrite(Buzzer,HIGH);delay(100);digitalWrite(Buzzer,LOW);
mode=0;
voice="";
}
//
===================================================================================
===
void loop()
{

int button1State = digitalRead(Button1Pin); // button state define


int button2State = digitalRead(Button2Pin); // button state define
int button3State = digitalRead(Button3Pin); // button state define
int button4State = digitalRead(Button4Pin); // button state define

if(relay1flag==1){digitalWrite(Relay1,HIGH);digitalWrite(Led1,HIGH);}
if(relay1flag==0){digitalWrite(Relay1,LOW);digitalWrite(Led1,LOW);}

if(relay2flag==1){digitalWrite(Relay2,HIGH);digitalWrite(Led2,HIGH);}
if(relay2flag==0){digitalWrite(Relay2,LOW);digitalWrite(Led2,LOW);}

if(relay3flag==1){digitalWrite(Relay3,HIGH);digitalWrite(Led3,HIGH);}
if(relay3flag==0){digitalWrite(Relay3,LOW);digitalWrite(Led3,LOW);}

if(relay4flag==1)digitalWrite(Relay4,HIGH);
if(relay4flag==0)digitalWrite(Relay4,LOW);

//=============================================
if( button1State==LOW && sw1flag==0)
{
sw1flag=1;
relay1flag=!relay1flag;
EEPROM.write(1,relay1flag);
//lcd.begin(16, 2);
digitalWrite(Buzzer,HIGH);delay(300);digitalWrite(Buzzer,LOW);
}
if( button1State==HIGH && sw1flag==1) {sw1flag=0;}
//===================================================================
if( button2State==LOW && sw2flag==0)
{
sw2flag=1;
relay2flag=!relay2flag;
EEPROM.write(2,relay2flag);
digitalWrite(Buzzer,HIGH);delay(100);digitalWrite(Buzzer,LOW);
}
if( button2State==HIGH && sw2flag==1) {sw2flag=0;}
//===================================
if( button3State==LOW && sw3flag==0)
{
sw3flag=1;
relay3flag=!relay3flag;
EEPROM.write(3,relay3flag);
digitalWrite(Buzzer,HIGH);delay(200);digitalWrite(Buzzer,LOW);
}
if( button3State==HIGH && sw3flag==1) {sw3flag=0;}
//=====================================================================
if( button4State==LOW && sw4flag==0)
{
sw4flag=1;
relay4flag=!relay4flag;
EEPROM.write(4,relay4flag);
digitalWrite(Buzzer,HIGH);delay(200);digitalWrite(Buzzer,LOW);
}
if( button4State==HIGH && sw4flag==1) {sw4flag=0;}

//============================================
while (Serial.available())
{ //Check if there is an available byte to read
c=0;
delay(10); //Delay added to make thing stable
c = Serial.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
Serial.println(voice);
}
Serial.println(voice);

//===================================
if (voice.length() < 0)voice="";

if (voice.length() > 0)
{
//===================
if(voice == "*Led1#") {voice="";relay1flag=!
relay1flag;EEPROM.write(1,relay1flag);}
else if(voice == "*led1#") {voice="";relay1flag=!
relay1flag;EEPROM.write(1,relay1flag);}
else if(voice == "*led 1#") {voice="";relay1flag=!
relay1flag;EEPROM.write(1,relay1flag);}
else if(voice == "*Led 1#") {;voice="";relay1flag=!
relay1flag;EEPROM.write(1,relay1flag);}
else if(voice == "*led one#"){voice="";relay1flag=!
relay1flag;EEPROM.write(1,relay1flag);}
else if(voice == "*ledone#") {voice="";relay1flag=!
relay1flag;EEPROM.write(1,relay1flag);}
else if(voice == "*Ledone#") {voice="";relay1flag=!
relay1flag;EEPROM.write(1,relay1flag);}
else if(voice == "*LED 1#") {voice="";relay1flag=!
relay1flag;EEPROM.write(1,relay1flag);}
else if(voice == "*LED1#") {voice="";relay1flag=!
relay1flag;EEPROM.write(1,relay1flag);}
else if(voice == "*LEDONE#") {voice="";relay1flag=!
relay1flag;EEPROM.write(1,relay1flag);}
else if(voice == "*1#") {voice="";relay1flag=!
relay1flag;EEPROM.write(1,relay1flag);}

else if(voice == "*Led2#") {voice="";relay2flag=!


relay2flag;EEPROM.write(2,relay2flag);}
else if(voice == "*led2#") {voice="";relay2flag=!
relay2flag;EEPROM.write(2,relay2flag);}
else if(voice == "*Led 2#") {voice="";relay2flag=!
relay2flag;EEPROM.write(2,relay2flag);}
else if(voice == "*led 2#") {voice="";relay2flag=!
relay2flag;EEPROM.write(2,relay2flag);}
else if(voice == "*led to#") {voice="";relay2flag=!
relay2flag;EEPROM.write(2,relay2flag);}
else if(voice == "*led two#"){voice="";relay2flag=!
relay2flag;EEPROM.write(2,relay2flag);}
else if(voice == "*ledto#") {voice="";relay2flag=!
relay2flag;EEPROM.write(2,relay2flag);}
else if(voice == "*ledtwo#") {voice="";relay2flag=!
relay2flag;EEPROM.write(2,relay2flag);}
else if(voice == "*LED2#") {voice="";relay2flag=!
relay2flag;EEPROM.write(2,relay2flag);}
else if(voice == "*LED 2#") {voice="";relay2flag=!
relay2flag;EEPROM.write(2,relay2flag);}
else if(voice == "*2#") {voice="";relay2flag=!
relay2flag;EEPROM.write(2,relay2flag);}

else if(voice == "*Led3#") {voice="";relay3flag=!


relay3flag;EEPROM.write(3,relay3flag);}
else if(voice == "*led3#") {voice="";relay3flag=!
relay3flag;EEPROM.write(3,relay3flag);}
else if(voice == "*Led 3#") {voice="";relay3flag=!
relay3flag;EEPROM.write(3,relay3flag);}
else if(voice == "*led 3#") {voice="";relay3flag=!
relay3flag;EEPROM.write(3,relay3flag);}
else if(voice == "*led three#"){voice="";relay3flag=!
relay3flag;EEPROM.write(3,relay3flag);}
else if(voice == "*led thre#") {voice="";relay3flag=!
relay3flag;EEPROM.write(3,relay3flag);}
else if(voice == "*ledthree#") {voice="";relay3flag=!
relay3flag;EEPROM.write(3,relay3flag);}
else if(voice == "*ledthre#") {voice="";relay3flag=!
relay3flag;EEPROM.write(3,relay3flag);}
else if(voice == "*LED3#") {voice="";relay3flag=!
relay3flag;EEPROM.write(3,relay3flag);}
else if(voice == "*LED 3#") {voice="";relay3flag=!
relay3flag;EEPROM.write(3,relay3flag);}
else if(voice == "*LED THREE#"){voice="";relay3flag=!
relay3flag;EEPROM.write(3,relay3flag);}
else if(voice == "*3#"){voice="";relay3flag=!
relay3flag;EEPROM.write(3,relay3flag);}

else if(voice == "*Led4#") {voice="";relay4flag=!


relay4flag;EEPROM.write(4,relay4flag);}
else if(voice == "*led4#") {voice="";relay4flag=!
relay4flag;EEPROM.write(4,relay4flag);}
else if(voice == "*Led 4#") {voice="";relay4flag=!
relay4flag;EEPROM.write(4,relay4flag);}
else if(voice == "*led 4#") {voice="";relay4flag=!
relay4flag;EEPROM.write(4,relay4flag);}
else if(voice == "*led for#") {voice="";relay4flag=!
relay4flag;EEPROM.write(4,relay4flag);}
else if(voice == "*led fore#") {voice="";relay4flag=!
relay4flag;EEPROM.write(4,relay4flag);}
else if(voice == "*led four#") {voice="";relay4flag=!
relay4flag;EEPROM.write(4,relay4flag);}
else if(voice == "*ledfor#") {voice="";relay4flag=!
relay4flag;EEPROM.write(4,relay4flag);}
else if(voice == "*ledfore#") {voice="";relay4flag=!
relay4flag;EEPROM.write(4,relay4flag);}
else if(voice == "*ledfour#") {voice="";relay4flag=!
relay4flag;EEPROM.write(4,relay4flag);}
else if(voice == "*LED4#") {voice="";relay4flag=!
relay4flag;EEPROM.write(4,relay4flag);}
else if(voice == "*LED 4#") {voice="";relay4flag=!
relay4flag;EEPROM.write(4,relay4flag);}
else if(voice == "*4#") {voice="";relay4flag=!
relay4flag;EEPROM.write(4,relay4flag);}

voice="";

} // main loop close


//==========================================

You might also like