-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduino File.ino
159 lines (136 loc) · 2.99 KB
/
Arduino File.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
boolean debug = true;
const byte numChars = 20;
char receivedChars[numChars];
double x;
boolean newData = false;
boolean feedback = true;
String inString = "";
String con = "";
int convertedtoint;
String ab;
String bc;
int a;
int b;
void setup()
{
Serial.begin(9600);
sendStartMessage();
}
void loop()
{
if (Serial.available() > 0) {
recvWithStartEndMarkers();
}
if (newData) {
parseData();
}
}
void sendStartMessage()
{
Serial.println(" ");
Serial.println("Pulse Counting System Activated");
Serial.println(" ");
}
void parseData()
{
newData = false;
if (debug) {
Serial.println( receivedChars );
}
if (strcmp(receivedChars, "HELLO") == 0)
{
Serial.println("HELLO");
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
}
if (strcmp(receivedChars, "START") == 0)
{
sendStartMessage();
}
if (receivedChars[0] == 'P' )
{ while (receivedChars[5] == 'N')
{
recvWithStartEndMarkers();
recvWithStartEndMarkers();
recvWithStartEndMarkers();
recvWithStartEndMarkers();
recvWithStartEndMarkers();
recvWithStartEndMarkers();
recvWithStartEndMarkers();
recvWithStartEndMarkers();
recvWithStartEndMarkers();
recvWithStartEndMarkers();
recvWithStartEndMarkers();
recvWithStartEndMarkers();
if ( receivedChars[4] == 'O' && receivedChars[5] == 'N' ) {
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
delay(1150);
writeString("A");
while (voltage >= 1)
{
voltage = voltage - 1;
a++;
}
b = voltage * 100;
ab = String(a);
bc = String(b);
delay(1150);
Serial.write(a);
delay(1150);
Serial.write(b);
a = 0;
b = 0;
}
if ( receivedChars[4] == 'O' && receivedChars[5] == 'F' ) {
loop() ;
}
}
}
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
rc = Serial.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
String converter(float voltage) {
const int BUF_MAX = 64;
char buf[BUF_MAX];
const int VAL_MAX = 16;
char val[VAL_MAX];
strcpy_P(buf, (const char*) F(""));
dtostrf(voltage, 8, 6, val);
return strcat(buf, val);
}
void writeString(String stringData) {
for (int i = 0; i < stringData.length(); i++)
{
Serial.write(stringData[i]);
}
}