This little blink program for the Arduino blinks an LED to give two short flashes and then a longer pause. The milliseconds are kept in an array ledDelays. It iterates through the array, setting the class=’bash’>interval according where the delayCount pointer is set. The LED state is toggled when the interval is complete.
The char pout[50] is used as a tidy way of outputting to the serial port as some older Arduino board don’t support Serial.printf.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include <Arduino.h> #define MONITOR_LED LED_BUILTIN const int delayCount = 4; // led blink times in millis, LED state: off, on, off, on const uint32_t ledDelays[delayCount] = { 1500, 100, 90, 200}; // add more blinks, like so: // const int delayCount = 6; // const uint32_t ledDelays[delayCount] = { 1500, 100, 90, 200, 50, 100}; int delayID = 0; uint32_t interval; uint32_t millisNow; uint32_t millisPrevious; bool ledToggle = false; char pout[50] = {0}; void setup() { Serial.begin(9600); delay(6000); Serial.println("ready"); pinMode(MONITOR_LED, OUTPUT); interval = ledDelays[0]; } void loop() { millisNow = millis(); if ((millisNow - millisPrevious) >= interval) { digitalWrite(MONITOR_LED, ledToggle); sprintf(pout, "%d, %d, %d", delayID, ledToggle, interval); Serial.println(pout); // Serial.printf("%d, %d, %d\n", delayID, ledToggle, interval); ledToggle = !(ledToggle); // change the state of the LED millisPrevious = millisNow; // blink without delay() interval = ledDelays[delayID]; // set the delay time for the next iteration delayID ++; if (delayID >= delayCount) { // reset the delayID delayID = 0; } } } |