-
Notifications
You must be signed in to change notification settings - Fork 0
/
RS485-ARDUINO-AND-INDUSTRIAL-DISTANCE-SENSOR.ino
135 lines (128 loc) · 3.69 KB
/
RS485-ARDUINO-AND-INDUSTRIAL-DISTANCE-SENSOR.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//********************************************************************************//
// Name : RS485 - Arduino and Laser Sensor //
// Author : TUENHIDIY //
// Date : 27 July 2019 //
// Version : 0.0 //
// Notes : Arduino UNO R3 communicate with distance laser sensor through RS485 //
// : The distance value is shown on LoLShield screeen //
//********************************************************************************//
#include <avr/pgmspace.h>
#include "Charliplexing.h"
#include "Myfont.h"
#include "Font.h"
#include <HardwareSerial.h>
#define BAUDRATE 9600
// Font 3x5 for the digits
int digits[][15] = {
{1,1,1,1,0,1,1,0,1,1,0,1,1,1,1}
,{0,0,1,0,0,1,0,0,1,0,0,1,0,0,1}
,{1,1,1,0,0,1,1,1,1,1,0,0,1,1,1}
,{1,1,1,0,0,1,1,1,1,0,0,1,1,1,1}
,{1,0,1,1,0,1,1,1,1,0,0,1,0,0,1}
,{1,1,1,1,0,0,1,1,1,0,0,1,1,1,1}
,{1,0,0,1,0,0,1,1,1,1,0,1,1,1,1}
,{1,1,1,0,0,1,0,0,1,0,0,1,0,0,1}
,{1,1,1,1,0,1,1,1,1,1,0,1,1,1,1}
,{1,1,1,1,0,1,1,1,1,0,0,1,0,0,1}
};
void setup()
{
LedSign::Init(DOUBLE_BUFFER);
Serial.begin(BAUDRATE,SERIAL_8N1); //Using Serial port with 9600 baudrate, non-parity, 8 data bits, 1 stop bit.
pinMode(A1, OUTPUT); //DE/RE Controling pin of RS-485
pinMode(A0, OUTPUT); //LED Indicator
}
void loop()
{
byte getdata[2];
byte High_Byte;
byte Low_Byte;
unsigned int wordx;
boolean D7=0;
// Enable Received
digitalWrite(A1,LOW); //DE/RE=LOW Receive Enabled
// Receive data from Laser Sensor
if(Serial.available())
{
digitalWrite(A0,HIGH);//LED
for (byte i=0; i<2; i++)
{
getdata[i]=Serial.read();
delay(15);
}
}
else
{
digitalWrite(A0,LOW); //LED
LedSign::Clear();
Myfont::Draw(0, 'F');
Myfont::Draw(9, 'F');
delay(1000);
LedSign::Clear();
}
// Check the correct data
if(Serial.available())
{
if (bitRead(getdata[0],0)==0)
{
Low_Byte=getdata[0];
}
if (bitRead(getdata[1],0)==1)
{
High_Byte=getdata[1];
}
// Two bytes conversion
// | 0 0 D11 D10 D9 D8 D7 1 | | D6 D5 D4 D3 D2 D1 D01 0 |
// | High_Byte | | Low_Byte |
D7 = bitRead(High_Byte,1);
Low_Byte=(Low_Byte & 0xFE) >> 1;
bitWrite(Low_Byte, 7, D7);
High_Byte=(High_Byte & 0x3F) >> 2;
wordx = (High_Byte << 8) | Low_Byte;
// Print all digits on LoLShield screeen.
if(wordx < 250)
{
LedSign::Clear();
Myfont::Draw(0, 'L');
Myfont::Draw(9, 'L');
}
else if ((wordx >= 250)& (wordx <= 4020))
{
LedSign::Clear();
// first digit - thousand
for (int i=0; i<15; i++)
{
LedSign::Set((i % 3)+ 0,(i/3)+2, digits[(wordx/1000) % 10][i]);
}
// second digit - hundred
for (int i=0; i<15; i++)
{
LedSign::Set((i % 3)+ 4,(i/3)+2, digits[(wordx/ 100) % 10][i]);
}
// third digit - Ten
for (int i=0; i<15; i++)
{
LedSign::Set((i % 3)+ 8,(i/3)+2, digits[(wordx/10) %10][i]);
}
// fourth digit - Unit
for (int y=0; y < 9; y++)
{
if (y < (wordx %10))
{
LedSign::Set(13, y, 1); // set the LED on
}
else
{
LedSign::Set(13, y, 0); // set the LED off
}
}
}
else
{
LedSign::Clear();
Myfont::Draw(0, 'H');
Myfont::Draw(9, 'H');
}
}
LedSign::Flip();
}