forked from scogswell/ArduinoSerialCommand
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialCommandExampleSoftwareSerial.ino
More file actions
136 lines (112 loc) · 3.76 KB
/
SerialCommandExampleSoftwareSerial.ino
File metadata and controls
136 lines (112 loc) · 3.76 KB
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
// Demo Code for SerialCommand Library.
// This example uses a SoftwareSerial object instead of the Hardware Serial port.
//
// Make sure your SoftwareSerial port is actually functioning correctly before
// you use this. See sketch for SoftwareSerialCheck included with this library.
//
// For this demo, all the command output is also echoed to the Hardware Serial port
// which you might find useful for debugging that you have it working correctly.
//
// Steven Cogswell
// October 2013
#include <SoftwareSerial.h>
#include <SerialCommand.h>
#define arduinoLED 13 // Arduino LED on board
// Parameters for SoftwareSerial port. For this example a Sparkfun CP2103 Breakout Module
// was used. Change defines as appopriate for your use.
// CP2103 -- Arduino
// 3.3V 3.3V
// TX-O pin 7
// RX-I pin 6
// GND GND
#define TX 6
#define RX 7
SoftwareSerial testSoftSerial = SoftwareSerial(RX,TX); // The SoftwareSerial Object
SerialCommand SCmd(testSoftSerial); // The demo SerialCommand object, using the SoftwareSerial Constructor
void setup()
{
pinMode(arduinoLED,OUTPUT); // Configure the onboard LED for output
digitalWrite(arduinoLED,LOW); // default to LED off
Serial.begin(9600);
testSoftSerial.begin(9600);
// Setup callbacks for SerialCommand commands
SCmd.addCommand("ON",LED_on); // Turns LED on
SCmd.addCommand("OFF",LED_off); // Turns LED off
SCmd.addCommand("HELLO",SayHello); // Echos the string argument back
SCmd.addCommand("P",process_command); // Converts two arguments to integers and echos them back
SCmd.addDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?")
Serial.println("Ready");
testSoftSerial.println("SoftSerial Port Ready");
testSoftSerial.println("Command Output will also echo to Hardware Serial Port");
}
void loop()
{
SCmd.readSerial(); // We don't do much, just process serial commands
}
void LED_on()
{
Serial.println("LED on");
testSoftSerial.println("(softserial) LED on");
digitalWrite(arduinoLED,HIGH);
}
void LED_off()
{
Serial.println("LED off");
testSoftSerial.println("(softserial) LED off");
digitalWrite(arduinoLED,LOW);
}
void SayHello()
{
char *arg;
arg = SCmd.next(); // Get the next argument from the SerialCommand object buffer
if (arg != NULL) // As long as it existed, take it
{
Serial.print("Hello ");
Serial.println(arg);
testSoftSerial.print("(softserial) Hello ");
testSoftSerial.println(arg);
}
else {
Serial.println("Hello, whoever you are");
testSoftSerial.println("(softserial) Hello, whoever you are");
}
}
void process_command()
{
int aNumber;
char *arg;
Serial.println("We're in process_command");
testSoftSerial.println("(softserial) We're in process_command");
arg = SCmd.next();
if (arg != NULL)
{
aNumber=atoi(arg); // Converts a char string to an integer
Serial.print("First argument was: ");
Serial.println(aNumber);
testSoftSerial.print("(softserial) First argument was: ");
testSoftSerial.println(aNumber);
}
else {
Serial.println("No arguments");
testSoftSerial.println("(softserial) No Arguments");
}
arg = SCmd.next();
if (arg != NULL)
{
aNumber=atol(arg);
Serial.print("Second argument was: ");
Serial.println(aNumber);
testSoftSerial.print("(softserial) Second argument was: ");
testSoftSerial.println(aNumber);
}
else {
Serial.println("No second argument");
testSoftSerial.println("No second argument");
}
}
// This gets set as the default handler, and gets called when no other command matches.
void unrecognized()
{
Serial.println("What?");
testSoftSerial.println("(softserial) What?");
}