Serial com. with Arduino
Write a program to transmit data on the PC.
void setup()
{
Serial.begin(9600);
}
void loop() // run over and over
{
Serial.println("Hello world");
}
Simulation:
Write a program to receive data from the PC.
#include<LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7);
int led1=8;
int led2=9;
int led3=10;
int mot1=11;
int mot2=12;
#include<String.h>
int i=0;
char name[20];
void setup()
{
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(mot1,OUTPUT);
pinMode(mot2,OUTPUT);
lcd.begin(16,2);
Serial.begin(9600);
}
void loop()
{
while(Serial.available())
{
char temp=Serial.read();
name[i]=temp;
i++;
if(temp==0x0d)
{
if(strncmp(name,"bulb is on",10)==0)
{
lcd.clear();
lcd.print("bulb is on");
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
i=0;
}
else if(strncmp(name,"led is on",9)==0)
{
lcd.clear();
lcd.print("led is on");
digitalWrite(led2,HIGH);
digitalWrite(led1,LOW);
digitalWrite(led3,LOW);
digitalWrite(mot1,HIGH);
digitalWrite(mot2,LOW);
delay(1000);
digitalWrite(mot1,LOW);
digitalWrite(mot2,HIGH);
delay(1000);
digitalWrite(mot1,LOW);
digitalWrite(mot2,LOW);
i=0;
}
else if(strncmp(name,"fan is on",9)==0)
{
lcd.clear();
lcd.print("fan is on");
digitalWrite(led3,HIGH);
digitalWrite(led2,LOW);
digitalWrite(led1,LOW);
i=0;
}
}
}
}
0 comments:
Post a Comment
if you have any doubt please let me know