-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmillis_serial_timeout.ino
111 lines (96 loc) · 2.79 KB
/
millis_serial_timeout.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
// ---------------------------------------------
// millis_serial_timeout.ino
// ---------------------------------------------
// License: The Unlicense, Public Domain.
// Author: Koepel
// 2019 april 3
// ---------------------------------------------
//
// Read data from the serial port without waiting.
//
// Sometimes a command from the serial monitor has a carriage return,
// or a linefeed, or both or neither.
// To allow all those situations, a timeout with millis() is used
// to use that as well as the end of the serial data.
//
const int ledPin = LED_BUILTIN; // The digital pin to which a led is connected.
unsigned long previousMillis;
const unsigned long timeout = 100; // timeout in milliseconds
char buffer[20];
int index;
void setup()
{
Serial.begin( 9600);
Serial.println( "Enter a command");
Serial.println( "Try \"led on\" or \"led off\"");
pinMode( ledPin, OUTPUT);
// clear the buffer
index = 0;
buffer[0] = '\0';
}
void loop()
{
bool processCommand = false;
if( Serial.available() > 0) // new data received ?
{
int inChar = Serial.read();
previousMillis = millis(); // remember moment of last received data
if( inChar == '\n' || inChar == '\r') // end of line ?
{
processCommand = true;
}
else
{
buffer[index] = (char) inChar; // put new data in the buffer
if( index < (int)(sizeof( buffer) - 1)) // the 'sizeof' returns an unsigned size_t
{
index++;
}
else
{
// The buffer is full, too much data was received.
// Process the command and (for safety) clear
// the remaining of the data.
// With a low baudrate, it is possible that still
// extra characters will come in after this.
processCommand = true;
while( Serial.available() > 0)
{
Serial.read();
}
}
buffer[index] = '\0'; // set a new zero-terminator
}
}
// The timeout should only be active when data is being received.
// That is checked with the 'index' variable, as soon as
// it is higher than zero, then data is being received.
if( index > 0)
{
if( millis() - previousMillis >= timeout)
{
processCommand = true;
}
}
if( processCommand)
{
// The data that is received and stored in the buffer
// will be processed.
// It is possible that the buffer is still empty,
// for example when only a carriage return or linefeed
// was received.
Serial.print( "Command received: ");
Serial.println( buffer);
if( strcmp( buffer, "led on") == 0)
{
digitalWrite( ledPin, HIGH);
}
else if( strcmp( buffer, "led off") == 0)
{
digitalWrite( ledPin, LOW);
}
// clear the buffer
index = 0;
buffer[0] = '\0';
}
}