Go Down

Topic: Is built-in "heartbeat" for all Arduino boards feasible? (Read 63 times) previous topic - next topic


I think it would be a handy feature (as default or option) to have the board's LED indicate the performance of the main loop.

For example, if after erase, the LED defaults to 10Hz oscillation or so (iteration count to be based upon detected board), then as the user develops and tests their code it would slow down. Allow the user to adjust iteration count to suit their needs.

Of course, right now the user could add this manually.

For example, on the Arduino Due, this "clean" sketch oscillates the LED at somewhere over 10Hz:

Code: (HEARTBEAT) [Select]
unsigned long iteration;

void setup() {
  pinMode(13, OUTPUT);

void loop() {
  digitalWrite(13, ((iteration += 1) % 10000) > 5000);


you could add a counter in the timer overflow interrupt of the micros() timekeeping.
and after a zillion (to be determined) interrupts it toggles the led by means of direct port manipulation.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


Your method is simple enough to add, why not pump out a header file people can include or add as a library which allows setting these things. Here is what I would do:

Code: [Select]

#include "Arduino.h"

  #define ITERATIONS 10000

#ifndef BEAT_PIN
  #define BEAT_PIN 13

int main(){
  static unsigned long iteration = 0;
  #if defined( USBCON )
  pinMode( BEAT_PIN, OUTPUT );
  while( true ){
     digitalWrite( BEAT_PIN, ((iteration += 1) % ITERATIONS ) > ( ITERATIONS / 2 ) );
  return 0;

And it just gets included.
Code: [Select]
#include "Beat.h"

void setup() { }

void loop() { }

And for 10x speed we can override the values:
Code: [Select]
#define ITERATIONS 1000   //default of 10000
#include "Beat.h"

void setup() { }

void loop() { }

I would aim to use port manipulation though to minimize overhead. And maybe some logic to only toggle if the pin will actually change state.

Go Up