Including Blink Sketch with other programs

Hello:

Just curious if anyone uses the “blink” sketch in their programs to let you know that your Arduino is running?

Some sketches don’t show much of anything to tell you the Arduino is running code or not. At least maybe for startup troubleshooting it could be handy…..

Thanks
Don

Yes, people commonly use an LED as an activity monitor. It takes only a couple of lines of code.

You can report errors as well, by blinking in a sequence.

1 Like

Yes.

Suggest you always include a Heartbeat LED in your projects.

The on board L LED connected to D13 is ideal.


Here is a skeleton sketch I often start out with, just delete the sections not needed:


//
//
//  WEB link
//
//
//  Version    YY/MM/DD    Comments
//  =======    ========    ====================================================================
//  1.00       23/01/01    Running code
//
//
//
 
 
//********************************************^************************************************
//For diagnostics
//This is used for diagnostics on an UNO or Nano
//
//PINB5 = 13, PINB4 = 12, PINB3 = 11, PINB2 = 10, PINB1 = 9, PINB0 = 8
//
//macro definition for a pulse on UNO pin 13 i.e. PINB5 //63ns
 
//add this line to setup()
//pinMode(13,OUTPUT);
 
#define PULSE62_13       cli(); PINB = bit(PINB5); PINB = bit(PINB5); sei()
 
 
//********************************************^************************************************
//                       S e r i a l   O R   P a r a l l e l   L C D   ?
//********************************************^************************************************

//uncomment if you have a serial LCD                                                <-----<<<<<
#define serialLCDisBeingUsed
 
#ifdef  serialLCDisBeingUsed
 
#include <Wire.h>
 
//Use I2C library:     https://github.com/duinoWitchery/hd44780
//LCD Reference:       https://www.arduino.cc/en/Reference/LiquidCrystal
 
#include <hd44780.h>   //main hd44780 header
 
//NOTE:
//hd44780_I2Cexp control LCD using I2C I/O expander backpack (PCF8574 or MCP23008)
//hd44780_I2Clcd control LCD with native I2C interface (PCF2116, PCF2119x, etc...)
 
#include <hd44780ioClass/hd44780_I2Cexp.h> //I2C expander i/o class header
 
//If you do not know what your I2C address is, first run the 'I2C_Scanner' sketch
//hd44780_I2Cexp lcd(0x3F);
hd44780_I2Cexp lcd(0x27);
 
//********************************************^************************************************
 
#else
 
#include <LiquidCrystal.h>
 
// LCD pin         4   6  11  12  13  14
//                RS  EN  D4  D5  D6  D7
LiquidCrystal lcd(12, 11,  4,  5,  6,  7);
 
#endif
 
//********************************************^************************************************
 
 
#define EXPIRED                         true
#define stillTIMING                     false
 
#define ENABLED                         true
#define DISABLED                        false
 
#define LEDon                           HIGH    //+5V---[220R]---[LED]---PIN
#define LEDoff                          LOW
 
#define OPENED                          HIGH    //+5V---[Internal 50k]---PIN---[switch]---GND
#define CLOSED                          LOW
 
const byte userSwitch                 = 2;
const byte heartbeatLED               = 13;
 
byte lastUserSwitchState              = OPENED;
 
 
//********************************************^************************************************
//                                  T I M E R   s t u f f
//********************************************^************************************************

//a Structure that creates TIMER objects
 
struct makeTimer
{
#define MILLIS        1
#define MICROS        1000  //we can use this value to divide into a variable to get milliseconds
 
#define ENABLED       true
#define DISABLED      false
 
#define YES           true
#define NO            false
 
  //timerType       = what kind of TIMER is this, MILLIS or MICROS
  //enableFlag      = tells us if this TIMER is ENABLED or DISABLED
  //restart         = do we start this TIMER again and again, YES or NO
  //interval        = delay time (ms/us) which we are looking for
  //intervalDefault = if used, this is the powerup default value for "interval"
  //intervalNew     = if used, this is the value we want to change "interval" to
 
  //
  //****************************************
  //There are a minimum number of member values for the TIMER object.
  //Example:
  //give this TIMER a name "myTimer"
  //   makeTimer myTimer =
  //   {
  //assign values to the members that make up this TIMER
  //timerType (MILLIS or MICROS), enableFlag, restart, interval, intervalDefault, intervalNew
  //     MICROS, ENABLED, YES, 200ul, 200UL, 200ul,
  //   };
  //
  // You have access to:
  // Variables: myTimer.timerType, myTimer.enableFlag, myTimer.restart, myTimer.interval,
  //            myTimer.intervalDefault, myTimer.intervalNew
  //
  // Functions: myTimer.checkTime(), myTimer.enableTimer(), myTimer.disableTimer(),
  //            myTimer.expireTimer(), myTimer.restartTimer()
  //****************************************
 
  //these are the bare minimum members that need defining
  int           timerType;
  bool          enableFlag;
  bool          restart;
  unsigned long interval;
 
  //when you do not define these at compile time,
  //these get initialized to 0 when a TIMER is instantiated
  unsigned long intervalDefault;
  unsigned long intervalNew;
  unsigned long previousTime;
  unsigned long currentTime;
 
  //****************************************
  //Function to check if this TIMER has expired ex: myTimer.checkTime();
  bool checkTime()
  {
    currentTime = getTime();
 
    //has this TIMER expired ?
    if (currentTime - previousTime >= interval)
      //Note: if delays of < 2 millis are needed, use micros() and adjust "interval" as needed
    {
      //should this TIMER start again?
      if (restart == ENABLED)
      {
        //get ready for the next iteration
        previousTime = currentTime;
      }
 
      //this TIMER has expired
      return true;
    }
 
    //this TIMER has not expired
    return false;
 
  } //END of   checkTime()
 
  //****************************************
  //Function to enable and reset this TIMER, ex: myTimer.enableTimer();
  void enableTimer()
  {
    enableFlag = ENABLED;
 
    //initialize previousTime to current millis() or micros()
    previousTime = getTime();
 
  } //END of   enableTimer()
 
  //****************************************
  //Function to disable this TIMER, ex: myTimer.disableTimer();
  void disableTimer()
  {
    enableFlag = DISABLED;
 
  } //END of   disableTimer()
 
  //****************************************
  //Function to force this TIMER to expire ex: myTimer.expireTimer();
  void expireTimer()
  {
    //force this TIMER to expire now
    previousTime = getTime() - interval;
 
  } //END of   expireTimer()
 
  //****************************************
  //Function to restart this TIMER ex: myTimer.restartTimer();
  void restartTimer()
  {
    //reset this TIMER
    previousTime = getTime();
 
  } //END of   restartTimer()
 
  //****************************************
  //Function to return the current time
  unsigned long getTime()
  {
    //return the time             i.e. millis() or micros()
    if (timerType == MILLIS)
    {
      return millis();
    }
 
    else
    {
      return micros();
    }
 
  }; //END of   getTime()
 
}; //END of   structure “makeTimer”
 
 
//********************************************^************************************************
//example:
//create and initialize the "test" TIMER object
//****************************************
//makeTimer test =
//{
//timerType (MILLIS or MICROS), enableFlag, restart, interval, intervalDefault, intervalNew
//          MILLIS,              DISABLED,     NO,    2000ul,      2000ul,          0ul
//this is a millis() based TIMER, it is disabled, it does not restart,
//it's interval is 2 seconds, it's default is 2 seconds, new value is 0ul
//};
 
//****************************************
makeTimer heartbeat =
{
  //timerType (MILLIS or MICROS), enableFlag, restart, interval, intervalDefault, intervalNew
  MILLIS, ENABLED, YES, 500ul
};
 
//****************************************
makeTimer switches =
{
  //timerType (MILLIS or MICROS), enableFlag, restart, interval, intervalDefault, intervalNew
  MILLIS, ENABLED, YES, 50ul
};
 
//****************************************
makeTimer stateMachine =
{
  //timerType (MILLIS or MICROS), enableFlag, restart, interval, intervalDefault, intervalNew
  MILLIS, ENABLED, YES, 50ul
};
 
//All TIMERs are now defined
 
 
//********************************************^************************************************
//State Machine Stuff
enum SM {Startup, State1, State2, State3};
 
SM machineState = Startup;
 
 
//                                       s e t u p ( )
//********************************************^************************************************
void setup()
{
  Serial.begin(115200);
 
  //used for diagnostics                                                         <-------<<<<<
  //pinMode(13, OUTPUT);
 
  pinMode(heartbeatLED, OUTPUT);
 
  pinMode(userSwitch, INPUT_PULLUP);
 
  //****************************************
  //LCD stuff
  lcd.begin(16, 2);
  lcd.clear();
 
  lcd.setCursor(0, 0);
  //                   111111
  //         0123456789012345
  //             Skeleton
  lcd.print("    Skeleton    ");
 
  lcd.setCursor(0, 1);
  //                   111111
  //         0123456789012345
  //              Sketch
  lcd.print("     Sketch     ");
 
} //END of   setup()
 
 
//                                        l o o p ( )
//********************************************^************************************************
void loop()
{
  //PULSE62_13;
 
  //****************************************                      h e a r t b e a t   T I M E R
  //is it time to toggle the heartbeatLED ?
  if (heartbeat.checkTime() == EXPIRED)
  {
    //Toggle the heartbeatLED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }
 
  //****************************************                      s w i t c h e s   T I M E R
  //is it time to check the switches ?
  if (switches.checkTime() == EXPIRED)
  {
    checkSwitches();
  }
 
  //****************************************                      s t a t e M a c h i n e   T I M E R
  //is it time to check the the State Machine ?
  if (stateMachine.checkTime() == EXPIRED)
  {
    checkMachine();
  }
 
  //****************************************
  //Other non blocking code goes here
  //****************************************
 
} //END of   loop()
 
 
//                               c h e c k S w i t c h e s ( )
//********************************************^************************************************
//handle switches
void checkSwitches()
{
  //****************************************                      u s e r S w i t c h
  //userSwitch code
  byte currentState = digitalRead(userSwitch);
 
  //****************************************
  //was there a change in state ?
  if (lastUserSwitchState != currentState)
  {
    //update to the new state
    lastUserSwitchState = currentState;
 
    //****************************************
    //is the switch closed ?
    if (currentState == CLOSED)
    {
      //do something
    }
 
    //****************************************
    //the switch was opened
    else
    {
      //do something
    }
 
  }//END of   userSwitch code
 
  //****************************************
  //next switch code
  //****************************************
 
} //END of   checkSwitches()
 
 
//                                c h e c k M a c h i n e ( )
//********************************************^************************************************
//service the State Machine
void checkMachine()
{
  switch (machineState)
  {
    //****************************************
    case Startup:
      {
        //do startup stuff
      }
      break;                //END of   case:
 
    //****************************************
    case State1:
      {
        //do something
      }
      break;                //END of   case:
 
    //****************************************
    case State2:
      {
        //do something
      }
      break;                //END of   case:
 
    //****************************************
    case State3:
      {
        //do something
      }
      break;                //END of   case:
 
  } //END of  switch/case
 
} //END of   checkMachine()
 
 
//********************************************^************************************************

Thank you so very much…..very cool stuff!

not the blink itself, but for many projects (especially when the board is not connected to a PC to display data with serial), I use to light On/Off the onboard leds.

I also use it directly for debugging, like turning on then off the power led, depending on the progression of the code (beginning of setup, a function that gives me trouble etc...)

Yes, I do.

void heartBeat(int LedPin)
{
  static bool setUp = false;
  if (!setUp) pinMode (LedPin, OUTPUT), setUp = !setUp;
  digitalWrite(LedPin, (millis() / 1000) % 2);
}

void loop() 
{
heartBeat(LED_BUILTIN);
}

Have a nice day and enjoy coding in C++.

Yes, sometimes I do. It's often tacked on after debugging is complete, though...

Sometimes you can't because pin 13 on Uno/Nano is also SCK for SPI.

@schmarduino ,

This seems to me to be a general discussion, so I've moved it there. Certainly not a tutorial, introductory or otherwise. Please take more care when choosing a forum section.

I thank you.

A perfect example to answer your question.
ACTIVITY blinks every 700ms, ATTENTION lights if a pefsistent error has occurred

If that’s your PCB design, curious to see the whole board ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.