100% found this document useful (2 votes)
53 views

Ardduino Project

This document contains code for two Arduino projects: 1) A voting machine project that uses buttons and an LCD display to tally votes, show results, and store data in EEPROM memory. 2) A home automation project that controls devices via SMS messages received by a GSM module connected to an Arduino. The SMS codes are processed and used to turn pins on/off to control external devices.

Uploaded by

Galih Dwi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
53 views

Ardduino Project

This document contains code for two Arduino projects: 1) A voting machine project that uses buttons and an LCD display to tally votes, show results, and store data in EEPROM memory. 2) A home automation project that controls devices via SMS messages received by a GSM module connected to an Arduino. The SMS codes are processed and used to turn pins on/off to control external devices.

Uploaded by

Galih Dwi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

ARDUINO SERIAL COMMUNICATION

Serial Communication in Arduino

char temp;
void setup()
{
Serial.begin(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(!Serial.available()); // stay on this line if no data is received serially
if(Serial.available()>0) // check if some data is available to be read
{
temp=Serial.read(); // 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=EEPROM.read(0); //read the status of cflag from memory
v1=EEPROM.read(1);// v1 stores the vote count of party A
v2=EEPROM.read(2); // v2 stores thevote count of party B
v3=EEPROM.read(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);
lcd.begin(16,2);
if(cflag==0)
{
lcd.print("Press button");
lcd.setCursor(0,1);
lcd.print("to vote...");
delay(1000);
}
if(cflag==1)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Voting Closed");
lcd.setCursor(0,1);
v=v1+v2+v3;
lcd.print("Total Votes:");
lcd.print(v);
}
}

void rpt()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Press button");
lcd.setCursor(0,1);
lcd.print("to vote...");
}

void votedifference() // subrotine to calculate vote difference


{
if(v1>v2)
{
if(v2>v3)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("A wins by");
lcd.setCursor(0,1);
lcd.print(v1-v2);
lcd.print(" votes");
}
else
{
if(v1>v3)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("A wins by");
lcd.setCursor(0,1);
lcd.print(v1-v3);
lcd.print(" votes");
}
if(v1<v3)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("C wins by");
lcd.setCursor(0,1);
lcd.print(v3-v1);
lcd.print(" votes");
}
}
}

else
{
if(v1>v3)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("B wins by");
lcd.setCursor(0,1);
lcd.print(v2-v1);
lcd.print(" votes");
}

else
{
if(v2>v3)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("B wins by");
lcd.setCursor(0,1);
lcd.print(v2-v3);
lcd.print(" votes");
}
if(v2<v3)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("C wins by");
lcd.setCursor(0,1);
lcd.print(v3-v2);
lcd.print(" votes");
}
}
}
}

void loop()
{
if(digitalRead(a)==LOW && cflag==0) // if party A button is pressed
{
v1=v1+1;
EEPROM.write(1,v1);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("vote received...");
delay(1500);
rpt();
}

if(digitalRead(b)==LOW && cflag==0) // if party B button is pressed


{
v2=v2+1;
EEPROM.write(2,v2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("vote received...");
delay(1500);
rpt();
}

if(digitalRead(c)==LOW && cflag==0) // if party C button is pressed


{
v3=v3+1;
EEPROM.write(3,v3);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("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;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Total votes:");
lcd.setCursor(0,1);
lcd.print(v);
delay(2000);
rpt();
}

if(digitalRead(closed)==LOW) // if close button is pressed


{
cflag=1;
EEPROM.write(0,cflag);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Voting Closed...");
lcd.setCursor(0,1);
v=v1+v2+v3;
lcd.print("Total Votes:");
lcd.print(v);
while(digitalRead(result)==HIGH);
}

// if result button is pressed and before that close button was pressed
if(digitalRead(result)==LOW && cflag==1)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("A:");
lcd.print(v1);
lcd.setCursor(7,0);
lcd.print("B:");
lcd.print(v2);
lcd.setCursor(0,1);
lcd.print("C:");
lcd.print(v3);
delay(1500);

// logic for result of voting process


if(v1==v2 && v2==v3)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Result Tied");
delay(1500);
}

if(v1==v2 && v1>v3)


{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Tie b/w A and B");
delay(1500);
}

if(v2==v3 && v2>v1)


{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Tie b/w B and C");
delay(1500);
}
if(v1==v3 && v1>v2)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Tie b/w A and C");
delay(1500);
}

if(v1>v2)
{
if(v1>v3)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Party A wins");
delay(1500);
votedifference();
}
else if(v3>v1)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Party C wins");
delay(1500);
votedifference();
}
}

else
{
if(v2>v3 && v1!=v2)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Party B wins");
delay(1500);
votedifference();
}
else if(v3>v2)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Party C wins");
delay(1500);
votedifference();
}
}
}

if(digitalRead(clearbutton)==LOW) // if clear button is pressed


{
for (int i = 0; i < 512; i++)
{
EEPROM.write(i, 0);
}
v1=0;
v2=0;
v3=0;
cflag=0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Memory Cleared");
delay(1500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Press button");
lcd.setCursor(0,1);
lcd.print("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
Serial.begin(9600);
// wait for a while till the serial port is ready
delay(100);
// configure the gsm modemin sms mode
Serial.print("AT+CMGF=1\r");
delay(500);
// route the contents of the received sms on to serial port
Serial.print("AT+CNMI=2,2,0,0,0\r");
// initialize 20X4 the lcd
lcd.begin(20, 4);
//print inittial message on lcd
lcd.print("**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 ( !Serial.available() );
} while ( '"' != Serial.read() );

// read the sms which will have date, time, sms body and the senders number
for(m=0;m<45;m++)
{
while ( !Serial.available() );
inByte = Serial.read();
temp[m]=inByte;
}

// set the cursor position of the lcd


lcd.setCursor(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++)
{
lcd.print(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);
lcd.setCursor(0,2);
lcd.print("device 1 on");
}

if(temp[42]=='D' && temp[43]=='1' && temp[44]=='F')


{
digitalWrite(2,LOW);
lcd.setCursor(0,2);
lcd.print("device 1 off");
}

if(temp[42]=='D' && temp[43]=='2' && temp[44]=='N')


{
digitalWrite(3,HIGH);
lcd.setCursor(0,2);
lcd.print("device 2 on");
}

if(temp[42]=='D' && temp[43]=='2' && temp[44]=='F')


{
digitalWrite(3,LOW);
lcd.setCursor(0,2);
lcd.print("device 2 off");
}

if(temp[42]=='D' && temp[43]=='A' && temp[44]=='N')


{
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
lcd.setCursor(0,2);
lcd.print("all devices on");
}

if(temp[42]=='D' && temp[43]=='A' && temp[44]=='F')


{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
lcd.setCursor(0,2);
lcd.print("all devices off");
}
delay(1500); // give some delay before the next cycle starts
lcd.clear(); // clear the lcd screen
lcd.print("**Device Control..**");// print this message on lcd
}

You might also like