/* PlainLedTest.ino - Plain LED Test Program */ #include "PlainLed.h" // use the PlainLed library // Create preprocessor definitions for the connection pins. #define GREEN_PIN 9 #define YELLOW_PIN 10 #define RED_PIN 11 // Create PlainLed objects: green, yellow, and red. PlainLed green; PlainLed yellow; PlainLed red; // program variables int phase = 0; // control variable for loop unsigned long timer = 0; // millisecond timer int interval = 0; // time interval ms. PlainLed * led = NULL; // LED pointer int ledIndex = 0; // LED index /* setup - Program Initialization Setup void setup () This is where each of the PlainLed 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 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). 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() { 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 led = nextLed(); // select next LED 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 PlainLed * 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. */ PlainLed * nextLed () { if (ledIndex == 0) { ++ledIndex; return &green; } else if (ledIndex == 1) { ++ledIndex; return &yellow; } else if (ledIndex == 2) { ledIndex = 0; return &red; } }