ARDUINO SERIAL COMMUNICATION
Serial Communication in Arduino
char temp;
void setup()
{
[Link](9600); // Baud rate of 9600bps
pinMode(12,OUTPUT); //pin 12 declared as output
pinMode(13,OUTPUT); // pin 13 declared as output
delay(10); // delay of 10ms
}
void loop()
{
while(![Link]()); // stay on this line if no data is received serially
if([Link]()>0) // check if some data is available to be read
{
temp=[Link](); // read the incoming serial data byte and store in temp
if(temp=='1')
{
digitalWrite(13,HIGH);
digitalWrite(12,LOW);
}
if(temp=='2')
{
digitalWrite(13,LOW);
digitalWrite(12,HIGH);
}
if(temp=='3')
{
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
}
if(temp=='4')
{
digitalWrite(13,LOW);
digitalWrite(12,LOW);
}
}
Voting Machine Using Arduino
#include<LiquidCrystal.h> // lcd library
#include <EEPROM.h> //EEPROM library
LiquidCrystal lcd(13,12,11,10,9,8); // lcd control and data lines
// button pin definitions
int result=5;
int closed=6;
int total=7;
int clearbutton=A0;
int a=4;
int b=3;
int c=2;
// temporary variables used in program
int v1;
int v2;
int v3;
int v;
int cflag;
void setup()
{
cflag=[Link](0); //read the status of cflag from memory
v1=[Link](1);// v1 stores the vote count of party A
v2=[Link](2); // v2 stores thevote count of party B
v3=[Link](3); // v3 stores the vote count of party C
pinMode(a,INPUT); // declaration of buttons as input
pinMode(b,INPUT);
pinMode(c,INPUT);
pinMode(total,INPUT);
pinMode(closed,INPUT);
pinMode(result,INPUT);
pinMode(clearbutton,INPUT);
digitalWrite(a,HIGH); //default status of buttons when not pressed is high
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(total,HIGH);
digitalWrite(closed,HIGH);
digitalWrite(result,HIGH);
digitalWrite(clearbutton,HIGH);
[Link](16,2);
if(cflag==0)
{
[Link]("Press button");
[Link](0,1);
[Link]("to vote...");
delay(1000);
}
if(cflag==1)
{
[Link]();
[Link](0,0);
[Link]("Voting Closed");
[Link](0,1);
v=v1+v2+v3;
[Link]("Total Votes:");
[Link](v);
}
}
void rpt()
{
[Link]();
[Link](0,0);
[Link]("Press button");
[Link](0,1);
[Link]("to vote...");
}
void votedifference() // subrotine to calculate vote difference
{
if(v1>v2)
{
if(v2>v3)
{
[Link]();
[Link](0,0);
[Link]("A wins by");
[Link](0,1);
[Link](v1-v2);
[Link](" votes");
}
else
{
if(v1>v3)
{
[Link]();
[Link](0,0);
[Link]("A wins by");
[Link](0,1);
[Link](v1-v3);
[Link](" votes");
}
if(v1<v3)
{
[Link]();
[Link](0,0);
[Link]("C wins by");
[Link](0,1);
[Link](v3-v1);
[Link](" votes");
}
}
}
else
{
if(v1>v3)
{
[Link]();
[Link](0,0);
[Link]("B wins by");
[Link](0,1);
[Link](v2-v1);
[Link](" votes");
}
else
{
if(v2>v3)
{
[Link]();
[Link](0,0);
[Link]("B wins by");
[Link](0,1);
[Link](v2-v3);
[Link](" votes");
}
if(v2<v3)
{
[Link]();
[Link](0,0);
[Link]("C wins by");
[Link](0,1);
[Link](v3-v2);
[Link](" votes");
}
}
}
}
void loop()
{
if(digitalRead(a)==LOW && cflag==0) // if party A button is pressed
{
v1=v1+1;
[Link](1,v1);
[Link]();
[Link](0,0);
[Link]("vote received...");
delay(1500);
rpt();
}
if(digitalRead(b)==LOW && cflag==0) // if party B button is pressed
{
v2=v2+1;
[Link](2,v2);
[Link]();
[Link](0,0);
[Link]("vote received...");
delay(1500);
rpt();
}
if(digitalRead(c)==LOW && cflag==0) // if party C button is pressed
{
v3=v3+1;
[Link](3,v3);
[Link]();
[Link](0,0);
[Link]("vote received...");
delay(1500);
rpt();
}
// if total button is pressed and close button was not pressed earlier
if(digitalRead(total)==LOW && cflag==0)
{
v=v1+v2+v3;
[Link]();
[Link](0,0);
[Link]("Total votes:");
[Link](0,1);
[Link](v);
delay(2000);
rpt();
}
if(digitalRead(closed)==LOW) // if close button is pressed
{
cflag=1;
[Link](0,cflag);
[Link]();
[Link](0,0);
[Link]("Voting Closed...");
[Link](0,1);
v=v1+v2+v3;
[Link]("Total Votes:");
[Link](v);
while(digitalRead(result)==HIGH);
}
// if result button is pressed and before that close button was pressed
if(digitalRead(result)==LOW && cflag==1)
{
[Link]();
[Link](0,0);
[Link]("A:");
[Link](v1);
[Link](7,0);
[Link]("B:");
[Link](v2);
[Link](0,1);
[Link]("C:");
[Link](v3);
delay(1500);
// logic for result of voting process
if(v1==v2 && v2==v3)
{
[Link]();
[Link](0,0);
[Link]("Result Tied");
delay(1500);
}
if(v1==v2 && v1>v3)
{
[Link]();
[Link](0,0);
[Link]("Tie b/w A and B");
delay(1500);
}
if(v2==v3 && v2>v1)
{
[Link]();
[Link](0,0);
[Link]("Tie b/w B and C");
delay(1500);
}
if(v1==v3 && v1>v2)
{
[Link]();
[Link](0,0);
[Link]("Tie b/w A and C");
delay(1500);
}
if(v1>v2)
{
if(v1>v3)
{
[Link]();
[Link](0,0);
[Link]("Party A wins");
delay(1500);
votedifference();
}
else if(v3>v1)
{
[Link]();
[Link](0,0);
[Link]("Party C wins");
delay(1500);
votedifference();
}
}
else
{
if(v2>v3 && v1!=v2)
{
[Link]();
[Link](0,0);
[Link]("Party B wins");
delay(1500);
votedifference();
}
else if(v3>v2)
{
[Link]();
[Link](0,0);
[Link]("Party C wins");
delay(1500);
votedifference();
}
}
}
if(digitalRead(clearbutton)==LOW) // if clear button is pressed
{
for (int i = 0; i < 512; i++)
{
[Link](i, 0);
}
v1=0;
v2=0;
v3=0;
cflag=0;
[Link]();
[Link](0,0);
[Link]("Memory Cleared");
delay(1500);
[Link]();
[Link](0,0);
[Link]("Press button");
[Link](0,1);
[Link]("to vote...");
}
}
#include<reg51.h>
// define the o/p ports to be connected to L293D
sbit op1=P2^0;
sbit op2=P2^1;
sbit op3=P2^2;
sbit op4=P2^3;
// temp is a variable used here for storing the serially received data byte
unsigned char temp;
//main program begins from here
void main()
{
// initialization commands for serial communication
TMOD=0x20;
TH1=0XFD;
SCON=0X50;
TR1=1;
// infinite loop starts from this point for serial data reception
while(1)
{
while(RI==0); // wait until some data byte is received serially
temp=SBUF; // store the data byte in the temporary variable
RI=0; // clear te RI flag for next data byte reception
// now put comparative statements using if loop
if(temp=='1') // move the robot forward
{
op1=1;
op2=0;
op3=1;
op4=0;
}
if(temp=='2') // move the robot backward
{
op1=0;
op2=1;
op3=0;
op4=1;
}
if(temp=='3') // move the robot towards right
{
op1=1;
op2=0;
op3=0;
op4=0;
}
if(temp=='4') // move the robot towards left
{
op1=0;
op2=0;
op3=1;
op4=0;
}
if(temp=='5') // stop the robot
{
op1=0;
op2=0;
op3=0;
op4=0;
}
}
}
CODE
// code for the home automation using arduino
// gsm module sim900 is used with arduino uno atmega328
// written by UMESH DUTTA
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
//temporary variable to store the contents of the received sms
char temp[50];
int m;
int inByte =0;
void setup()
{
// initialize serial communication with baud rate of 9600 bps
[Link](9600);
// wait for a while till the serial port is ready
delay(100);
// configure the gsm modemin sms mode
[Link]("AT+CMGF=1\r");
delay(500);
// route the contents of the received sms on to serial port
[Link]("AT+CNMI=2,2,0,0,0\r");
// initialize 20X4 the lcd
[Link](20, 4);
//print inittial message on lcd
[Link]("**Device Control..**");
//define pins 2 ad 3 as o/p pins
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
// make default status of pin 2 and 3 low
digitalWrite(2,LOW);
digitalWrite(3,LOW);
delay(500);
}
// loop function starts here
void loop()
{
// clear the temporary variable temp
m=0;
for(m=0;m<45;m++)
{
temp[m]=0;
}
// wait for the reception of the message
// " is a identifier for the start of the message
do
{
while ( ![Link]() );
} while ( '"' != [Link]() );
// read the sms which will have date, time, sms body and the senders number
for(m=0;m<45;m++)
{
while ( ![Link]() );
inByte = [Link]();
temp[m]=inByte;
}
// set the cursor position of the lcd
[Link](0,1);
// after testing it is found that the main body of the sms willl start from psition 42 ie. for
m=42
// I have used 3 digit code for the sms and so position 42, 43 and 44 will contain the main
body of the sms
// print the received sms main body on the lcd screen
for(m=42;m<45;m++)
{
[Link](temp[m]);
}
//now write different conditions and required actions
/*codes are
D1N----------device 1 on
D1F----------device 1 off
D2N----------device 2 on
D2F----------device 2 off
DAN----------all devices on
DAF----------all devices off
*/
// device 1 is connected to pin 2 and device 2 is connected to pin 3 of the arduino
if(temp[42]=='D' && temp[43]=='1' && temp[44]=='N')
{
digitalWrite(2,HIGH);
[Link](0,2);
[Link]("device 1 on");
}
if(temp[42]=='D' && temp[43]=='1' && temp[44]=='F')
{
digitalWrite(2,LOW);
[Link](0,2);
[Link]("device 1 off");
}
if(temp[42]=='D' && temp[43]=='2' && temp[44]=='N')
{
digitalWrite(3,HIGH);
[Link](0,2);
[Link]("device 2 on");
}
if(temp[42]=='D' && temp[43]=='2' && temp[44]=='F')
{
digitalWrite(3,LOW);
[Link](0,2);
[Link]("device 2 off");
}
if(temp[42]=='D' && temp[43]=='A' && temp[44]=='N')
{
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
[Link](0,2);
[Link]("all devices on");
}
if(temp[42]=='D' && temp[43]=='A' && temp[44]=='F')
{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
[Link](0,2);
[Link]("all devices off");
}
delay(1500); // give some delay before the next cycle starts
[Link](); // clear the lcd screen
[Link]("**Device Control..**");// print this message on lcd
}