D. Edward Mitchell 16:00, 14 April 2020 (UTC) Hello World!    groupKOS Developer Share —usually UNDER CONSTRUCTION

Arduino Sketch - com port parsing

From groupKOS Developer Share
Jump to navigation Jump to search



/*  Com port parsing example file: 'com_port_parsing'.
 *  Arduino code examines serial event characters for 
 *  control characters that toggle the flashing LED
 *  blink rate from/to fast/slow.
 *  Blinks LED on pin 13 at a slow or fast rate,  
 *  changed by sending "s", or "f" in the com monitor.  
 *  This example is using serialEvent() and 'Serial.readStringUntil'  
 *  to capture character strings on the COM port ending in a CR ("\n").
 *  Note: Leave the COM terminal in the Arduino IDE on 'NoLineEnding'.
 *  
 *  The circuit:    
 *  LED attached from pin 13 to ground through a 330 ohm limiting resistor.  
 *  
 *  This code example is released to the public domain.  
 *  Author: DonEMitchell, using some code from distributed examples.
 */

const int ledPin =  13;           // The number of the LED pin.
String serialEntry = "";          // Capture variable for SerialEvent().
int ledState = LOW;               // Used to toggle the LED state.

unsigned long previousMillis = 0; // Will store last time LED was updated.
unsigned long currentMillis;

// constants won't change :
long interval = 1000;             // Interval at which to blink (milliseconds).

void setup() {
  Serial.begin(9600);
  while (!Serial) {}              // Wait for serial object initialization.
  pinMode(ledPin, OUTPUT);        // Set the digital pin as output.
}


void loop() {
  // Check to see if it's time to blink the LED.
  currentMillis = millis();
  if (currentMillis - previousMillis >= interval){  // Check elapsed milliseconds against interval.
    previousMillis = currentMillis;                 // Save the last time the LED blinked.
    Serial.print("LED State: ");
    Serial.println(ledState);
    if (ledState == LOW) ledState = HIGH;           // Toggle the LED.
    else ledState = LOW;
    digitalWrite(ledPin, ledState);                 // Set the LED pin with the ledState variable.
  } // end check

  //  Parse serialEntry for control characters (S or F).
  if (serialEntry > "") {
    if (serialEntry == "f" || serialEntry == "F"){ 
      interval = 250; // Faster.
      Serial.println("Detected: '" + serialEntry + "'. Faster interval: " + interval);
      serialEntry = "";
    }
    else if (serialEntry == "s" || serialEntry == "S"){ 
      interval = 2000;  // Slower.
      Serial.println("Detected: '" + serialEntry + "'. Slower interval: " + interval);
      serialEntry = "";
    }
    else{
      Serial.println("Invalid entry: " + serialEntry );
      Serial.println("No change in delay interval. Enter S or F.");
      serialEntry = "";       // Clear the last user entry.
    }
  }
}

// Capture the user-supplied control character
// from the COM port using serialEvent.
void serialEvent() {
  serialEntry = Serial.readStringUntil("\n"); // Read the control character preceding CR (\n). Note: Set COM terminal to 'nolinefeed'.
}