101 lines
2.8 KiB
C++
101 lines
2.8 KiB
C++
/*
|
|
Serial Call and Response in ASCII
|
|
Language: Wiring/Arduino
|
|
|
|
This program sends an ASCII A (byte of value 65) on startup and repeats that
|
|
until it gets some data in. Then it waits for a byte in the serial port, and
|
|
sends three ASCII-encoded, comma-separated sensor values, truncated by a
|
|
linefeed and carriage return, whenever it gets a byte in.
|
|
|
|
The circuit:
|
|
- potentiometers attached to analog inputs 0 and 1
|
|
- pushbutton attached to digital I/O 2
|
|
|
|
created 26 Sep 2005
|
|
by Tom Igoe
|
|
modified 24 Apr 2012
|
|
by Tom Igoe and Scott Fitzgerald
|
|
Thanks to Greg Shakar and Scott Fitzgerald for the improvements
|
|
|
|
This example code is in the public domain.
|
|
|
|
http://www.arduino.cc/en/Tutorial/SerialCallResponseASCII
|
|
*/
|
|
|
|
#define MASTER 6
|
|
#define CIRCUIT1 7
|
|
#define CIRCUIT2 8
|
|
#define CIRCUIT3 9
|
|
|
|
String inString;
|
|
int MasterState = 0;
|
|
int Circuit1State = 0;
|
|
int Circuit2State = 0;
|
|
int Circuit3State = 0;
|
|
|
|
void setup() {
|
|
// start serial port at 9600 bps and wait for port to open:
|
|
Serial.begin(9600);
|
|
while (!Serial) {
|
|
; // wait for serial port to connect. Needed for native USB port only
|
|
}
|
|
|
|
|
|
pinMode(MASTER, OUTPUT);
|
|
pinMode(CIRCUIT1, OUTPUT);
|
|
pinMode(CIRCUIT2, OUTPUT);
|
|
pinMode(CIRCUIT3, OUTPUT);
|
|
|
|
digitalWrite(MASTER, HIGH);
|
|
digitalWrite(CIRCUIT1, HIGH);
|
|
digitalWrite(CIRCUIT2, HIGH);
|
|
digitalWrite(CIRCUIT3, HIGH);
|
|
}
|
|
|
|
int setState(String cmd, String ref,int pin,int state,int store){
|
|
if (cmd.indexOf(ref) >= 0){
|
|
digitalWrite(pin,state);
|
|
Serial.print("Pin ");
|
|
Serial.print(pin);
|
|
Serial.print(" was set to: ");
|
|
Serial.println(state);
|
|
return !state;
|
|
}
|
|
return store;
|
|
}
|
|
|
|
void loop() {
|
|
// if we get a valid byte, read analog ins:
|
|
|
|
if (Serial.available() > 0) {
|
|
// get incoming byte:
|
|
inString = Serial.readString();
|
|
Serial.print("Command received: ");
|
|
Serial.println(inString);
|
|
|
|
MasterState = setState(inString,"MASTER=ON",MASTER,LOW,MasterState);
|
|
MasterState = setState(inString,"MASTER=OFF",MASTER,HIGH,MasterState);
|
|
|
|
Circuit1State = setState(inString,"CIRCUIT1=ON",CIRCUIT1,LOW,Circuit1State);
|
|
Circuit1State = setState(inString,"CIRCUIT1=OFF",CIRCUIT1,HIGH,Circuit1State);
|
|
|
|
Circuit2State = setState(inString,"CIRCUIT2=ON",CIRCUIT2,LOW,Circuit2State);
|
|
Circuit2State = setState(inString,"CIRCUIT2=OFF",CIRCUIT2,HIGH,Circuit2State);
|
|
|
|
Circuit3State = setState(inString,"CIRCUIT3=ON",CIRCUIT3,LOW,Circuit3State);
|
|
Circuit3State = setState(inString,"CIRCUIT3=OFF",CIRCUIT3,HIGH,Circuit3State);
|
|
|
|
if (inString.indexOf("GETSTATE") >= 0){
|
|
Serial.print("MASTER=");
|
|
Serial.println(MasterState);
|
|
Serial.print("CIRCUIT1=");
|
|
Serial.println(Circuit1State);
|
|
Serial.print("CIRCUIT2=");
|
|
Serial.println(Circuit2State);
|
|
Serial.print("CIRCUIT3=");
|
|
Serial.println(Circuit3State);
|
|
}
|
|
}
|
|
|
|
}
|