12/31/16

Arduino Blink Sketch

   /*
    * Upload Error: Error opening serial port '/dev/cu.usbmodem1421'
    * Fix: Close Serial Monitor window, reopen after upload is done.
    * 
    * Misc
    * - On Micro, TX LED lights up when Serial print(), println() is called
    * - Serial Monitor can be started after upload or reset
    * - setup() runs once when you press reset or power the board
    * - loop() is a run-loop
    * 
    * Keyboard Shortcuts
    * CS M  Serial Monitor
    */

// const int  kLedPin = LED_BUILTIN;
   const int  kLedPin = 10;
   const long kBlinkInterval = 400;
   const int  kMaxLogCount = 10;

   unsigned long previousMillis = 0;
   boolean onceToken = false;
   int ledState = LOW;
   int logCount = 0;


   void setup() {
       
       const String kBoard = "Arduino Micro, 5V";
       
       Serial.begin(9600);
       delay(6000);  // delay for serial monitor intialization
       Serial.println(kBoard);
       
       pinMode(kLedPin, OUTPUT);  // LED output
       if (kLedPin == LED_BUILTIN) {
           Serial.print("Built-In ");
       }
       Serial.print("LED Pin: ");
       Serial.println(kLedPin);
       
       Serial.print("LED Blink Interval: ");
       Serial.println(kBlinkInterval);
       
       Serial.print("Max Log Count: ");
       Serial.println(kMaxLogCount);
   }


   void loop() {
       
       if (onceToken == false) {
           Serial.println("");
           Serial.println("Loop started");
           onceToken = true;
       }

       unsigned long currentMillis = millis();

       if (currentMillis - previousMillis >= kBlinkInterval) {
           previousMillis = currentMillis;

           if (ledState == LOW) {
               ledState = HIGH;
               log("On");
           }
           else {
               ledState = LOW;
               log("Off");
           }

           digitalWrite(kLedPin, ledState);
       }
   }


   void log(String message) {
       if (logCount < kMaxLogCount) {
           Serial.println(message);
           logCount++;
       }
   }


   // Output
      
      Arduino Micro, 5V
      LED Pin: 10
      LED Blink Interval: 400
      Max Log Count: 10
      
      Loop started
      On
      Off
      On
      Off
      On
      Off
      On
      Off
      On
      Off





No comments:

Post a Comment