The Arduino code, based on C+, below is short and simple but has a couple of lines I don't understand:
Code: Select all
//pin 2 is pulled high by pullup resistor
//printing "safe#" to the serial port indicates safe
//printing "notsafe#" to the serial port indicates not safe
bool pinState = LOW;
//-----------------------------------------------------------------------------------------
//--------------------------------------Set safeState--------------------------------------
//-----------------------------------------------------------------------------------------
bool safeState = HIGH; //set desired safeState of the monitor HIGH or LOW
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
void setup() {
pinMode(2, INPUT);
Serial.begin(9600); // initialize serial
Serial.flush(); // flush the port
Serial.print("notsafe#"); // send notsafe# as first state while monitor and client initialize
}
void loop() {
String cmd;
if (Serial.available() > 0) {
cmd = Serial.readStringUntil('#');
if (cmd == "S") {
pinState = digitalRead(2);
if (pinState == safeState) {
Serial.print("safe#");
}
if (pinState != safeState) {
Serial.print("notsafe#");
}
}
}
}
I take it "String cmd;" is just setting up a constant with a not very helpful name and has nothing to do with Windows cmd.
What is the # character for in "cmd = Serial.readStringUntil('#');"?
Where does the S come from in "if (cmd == "S") {". I can see it's from .readStringUntil but why might it be or not be an S?
I've been looking for a list of Serial. functions in the hope I can work things out but I can't find one.
Hope this makes sense.
Regards
Graeme