Simple Serial 3 Sketch:
/*
This is a little sketch that just grabs data from the serial monitor
and tells you what it got. Gives me a simple space to try to understand
what happens in the setup and loop parts...
The mental model I'm using is that there is a 'mailbox' on the serial
monitor that gets checked each time the Serial.available call is run. If
no mail Serial.available returns a 0 and if there is 'mail' then it returns
a 1. This return value is tested in some sort of conditional statement that
preceeds the Serial.read which 'opens' the mail.
To test my understanding I enter various characters into the serial monitor
input line and watch when and how the sketch responds. This helped develop my
understanding...serialData is a future project to figure out how to input
numerical data into the sketch through the serial monitor.
SimpleSerial2
This version replaces the if(Serial.available()) loop with while(! Serial.available())
loop to make the sketch pause and wait for input instead of running ahead.
SimpleSerial3
Tidy up the experiments and create structure that allows user to enter debug mode and
then checks each cycle of the loop to see if you want to continue in the debug mode.
Bruce Emerson 10/21/17
*/
int pinLED = 13; // set LED pin
int serialData = 0; // I want to explore inputing a # for a pinout
char chRead = 'n'; // my choice of name for the input from serial monitor
// chRead = y or Y means debug is 'on'
char nextRead = 'n'; // my choice of name for another input from serial monitor
void setup()
{
pinMode(pinLED, OUTPUT); // initialize digital pin pinLED as an output..
Serial.begin(9600); // start up serial monitor.
while (! Serial); // wait for serial monitor to start before cont...
Serial.println("Enter 'Y' or 'y' whenever to begin debug: "); // What it says...
}
void loop()
{
/*
This next section watches to see if the user enters a 'Y' or 'y' (just
the character) into the serial monitor. This is my mental model of checking
the 'mailbox'. When something is in 'mailbox' it is read and checked to see
what was entered. Once you enter debug mode the code checks each cycle to
see if you want to stay in the debug mode. This entire section can be dropped
in at the head of the loop section and should be transparent to the rest of
the sketch.
*/
if (Serial.available()){ // watching to see if something shows up
Serial.println("Input from user detected: "); // Shouldn't show up until after input
chRead = Serial.read(); // read the character
// delay(1000); // in case I want to slow things down
}
if (chRead =='Y' || chRead == 'y'){ // check the character to see if Y/y
Serial.println("running in debug mode:"); // affirms debug mode
Serial.println("return to run mode (y/n)?: "); // option to return to run
while (! Serial.available()){ // this a way of stalling the code until
delay(10); // somthing shows up in the 'mailbox'
}
nextRead = Serial.read(); // read data in 'mailbox'
if (nextRead =='Y' || nextRead == 'y'){ // reset to run mode after information
Serial.print("Returning to run mode: ");
chRead = 'n';
// delay (2000); // in case I want to slow things down
}
}
/*
From here on the sketch is running and if you want to do something to assist
debugging just test 'if (chRead =='Y' || chRead == 'y'){' and check stuff.
*/
Serial.println("running loop");
if (chRead =='Y' || chRead == 'y'){ // debug
Serial.println("Turning 13 on: ");
}
digitalWrite(pinLED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
if (chRead =='Y' || chRead == 'y'){ // debug
Serial.println("Turning 13 off: ");
}
digitalWrite(pinLED, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
// delay(2000); // in case I want to slow things down
// the rest of the loop executes here
}