/* PulseLedTest.ino - Pulse Width Modulation LED Test Program */ #include "PulseLed.h" // Create preprocessor definitions for the connection pins. #define GREEN_PIN 9 #define YELLOW_PIN 10 #define RED_PIN 11 // Create PulseLed objects: green, yellow, and red. PulseLed green; PulseLed yellow; PulseLed red; // program variables int phase = 0; // control variable for loop unsigned long timer = 0; // millisecond timer int interval = 0; // time interval ms. PulseLed * led = NULL; // LED pointer int ledIndex = 0; // LED index bool fadeEnable = false; // fade enable variable /* setup - Program Initialization Setup void setup () This is where each of the PulseLed objects is initialized using the "begin" function. The pin connection numbers are passed as a parameter where they will be maintained within the object. The preprocessor will use simple substitution to provide the pin numbers that have been defined previously. The default settings of the PulseLed class are used for max, up time, and down time in this program. The "setFading" function is used to enable and disable the fading feature every other time through the set of LEDs. The brief delay is used to ensure that the millisecond timer will not be at 0 when we begin the "loop". Without it, all of the LEDs turn on as soon as the program is started. */ void setup() { green.begin(GREEN_PIN); // initialize the green LED yellow.begin(YELLOW_PIN); // initialize the yellow LED red.begin(RED_PIN); // initialize the red LED delay(10); // brief delay } /* loop - Arduino Main Loop void loop () Drives the testing of a number of light emitting diodes in a continuous sequence. A millisecond timer is used to complete each "phase" of the test sequence. A "timer" maintains the starting time of each phase. A time "interval" is set in each phase to indicate the duration of the state of the LED. The test phases include: 1. LED on for 2 seconds. 2. LED off for 2 seconds. 3. Toggle the LED on for 3 seconds. 4. Check that LED is on and toggle off, 3 seconds. 5. Check that LED is off and toggle on, 4 seconds. 6. Turn off LED, 4 seconds. When all phases have been completed, the sequece repeats using the next LED. (see "nextLed" below). When the complete set of LEDs have been tested, the "fadeEnable" variable is flipped (see "changeEnable" below). This variable is used to either enable or disable fading every other time through the set of LEDs. Each time through the "loop", the "update" function is called for each of the LEDs. This allows the PulseLed class to change the PWM value when the proper time interval has elapsed. Note that the "delay" function is not used in the loop. The millisecond timing mechanism is the prefered method when dealing with real-time events. If there were a button pushed or a sensor reading change, there would still be plenty of time to capture those events. */ void loop() { // update the LEDs green.update(); yellow.update(); red.update(); unsigned long now = millis(); // get the current time if (timer != 0) { // timer is running if (now - timer >= interval) { // time has elasped timer = 0; // reset timer ++phase; // next phase } } else { // timer is not running timer = now; // set new start time if (phase == 0) { // initial phase changeEnable(); // change to enable or disable fading led = nextLed(); // select next LED led->setFading(fadeEnable); // enable or disable fading led->on(); // turn on the LED interval = 2000; // for 2 seconds } else if (phase == 1) { // turn off LED for 2 seconds led->off(); interval = 2000; } else if (phase == 2) { // toggle the LED on led->toggle(); interval = 3000; // 3 seconds } else if (phase == 3) { // check LED state if (led->isOn()) { // LED should be on led->toggle(); // change state } interval = 3000; // 3 seconds } else if (phase == 4) { // check LED state if (led->isOff()) { // LED should be off led->toggle(); // change state } interval = 4000; // 4 seconds } else if (phase == 5) { // turn off the LED led->off(); interval = 4000; // 4 seconds } else { // final phase timer = 0; // reset timer phase = 0; // repeat phases } } } /* nextLed - Select Next LED PulseLed * nextLed () Returns a pointer to the next LED in the order: green, yellow, red. The variable "ledIndex" keeps track of which one is next. After all the selections have been made, the cycle repeats. */ PulseLed * nextLed () { if (ledIndex == 0) { ++ledIndex; return &green; } else if (ledIndex == 1) { ++ledIndex; return &yellow; } else if (ledIndex == 2) { ledIndex = 0; return &red; } } /* changeEnable - Change Fade Enable Variable void changeEnable () Flips the "fadeEnable" variable from its current state when the "ledIndex" indicates that the first LED is the next to be selected. */ void changeEnable () { if (ledIndex == 0) { fadeEnable = ! fadeEnable; } }