Send Message with Arduino Serial Communication
1. Objective
The objective of project is talk about, Using serial communication for send message between two Arduino. Transmitter send message with the text without spaces unreadable to receiver. Receiver automatic separate that text to different world which can read.
2. Component Requirement
- Arduino Uno (x2)
- USB Cable
- Button
- Electronic Wire
3. Building
The first build circuit like below show on Figure 1.Figure 1: Arduino Serial Communication Connected |
4. Coding
Transmitter Code:// Code by Tann Thona, 20 November 2017
int button = 13;
String msg="RUPP_MY002_L1_ON";
void setup() {
// put your setup code here, to run once:
pinMode(button,INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(button)==0)
{
Serial.print(msg);
delay(100);
}
else
{
Serial.write('0');
delay(100);
}
}
Receiver Code:
// Code by Tann Thona, 20 November 2017
#define Ref '_'
String rmsg;
int msglength=0;
int cnt=0;
int Reflocated[10]={0};
String data [20];
int x,y=0;
void setup() {
// put your setup code here, to run once:
Serial.setTimeout(30);
Serial.begin(9600);
}
void loop()
{
msglength=0;
cnt=0;
Reflocated[10]={0};
data [20];
x = 0;
y = 0;
// put your main code here, to run repeatedly:
if(Serial.available())
{
rmsg = Serial.readString();
msglength=rmsg.length()-1;
for(int i=0;i<msglength+1;i++)
{
if(Ref ==rmsg[i]){
cnt++;
Reflocated[cnt]=i;
}
}
Reflocated[cnt+1]=msglength;
for(int j=0;j<cnt+1;j++){
if(Reflocated[j+1]==msglength){
y=1;
}
for(int i=Reflocated[j]+x;i<Reflocated[j+1]+y;i++){
data[j]+= rmsg[i];
}
Serial.println(data[j]);
Serial.println("---------------------------");
data[j] = ' ';
x=1;
}
}
}
5. Resulte
In the result of this project show like Figure 2.Figure 2: Result Send Message in Arduino Use VIRTUAL TERMINAL in Proteus. |
- If we don't push button transmitter send message "0" and receiver also receive message "0" too.
- when we push button, transmitter send message “RUPP_MY002_L1_ON" to receiver then receiver separate in world readable RUPP MY002 L1 ON.
-----------------------------------------------------
Royal University of Phnom Penh
Faculty of Engineering
Dep. Telecommunication and Electronic Engineering
Group Member:
1. Tann Thona
2. Thach Soveasna
3. Chhoy Noreath
4. Neth Channa
5. Mok Vira
Instructor: Prof. Chann Tola
Date: 20 November 2017
No comments:
Post a Comment