forked from scogswell/ArduinoSerialCommand
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSerialCommandHardwareOnlyExample.ino
More file actions
102 lines (82 loc) · 2.43 KB
/
SerialCommandHardwareOnlyExample.ino
File metadata and controls
102 lines (82 loc) · 2.43 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
// Demo Code for SerialCommand Library
// Steven Cogswell
// May 2011
// If you want to use HardwareSerial only, and not have to include SoftwareSerial support, you
// can define SERIALCOMMAND_HARDWAREONLY in SerialCommand.h, which will cause it to build without
// SoftwareSerial support. This makes the library act as it used to before SoftwareSerial
// support was added, and you don't need this next include:
//#include <SoftwareSerial.h>
#include <SerialCommand.h>
#define arduinoLED 13 // Arduino LED on board
SerialCommand SCmd; // The demo SerialCommand object
void setup()
{
pinMode(arduinoLED,OUTPUT); // Configure the onboard LED for output
digitalWrite(arduinoLED,LOW); // default to LED off
Serial.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");
}
void loop()
{
SCmd.readSerial(); // We don't do much, just process serial commands
}
void LED_on()
{
Serial.println("LED on");
digitalWrite(arduinoLED,HIGH);
}
void LED_off()
{
Serial.println("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);
}
else {
Serial.println("Hello, whoever you are");
}
}
void process_command()
{
int aNumber;
char *arg;
Serial.println("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);
}
else {
Serial.println("No arguments");
}
arg = SCmd.next();
if (arg != NULL)
{
aNumber=atol(arg);
Serial.print("Second argument was: ");
Serial.println(aNumber);
}
else {
Serial.println("No second argument");
}
}
// This gets set as the default handler, and gets called when no other command matches.
void unrecognized()
{
Serial.println("What?");
}