Go Down

Topic: Apprentice coder (Read 5 times) previous topic - next topic

lloyddean

#45
Oct 31, 2012, 07:25 pm Last Edit: Nov 01, 2012, 07:37 am by lloyddean Reason: 1
Simple but untested!

Code: [Select]

// forum    - <http://arduino.cc/forum/index.php/topic,126814.msg977920.html#msg977920>
//
// arrays   - <http://www.cprogramming.com/tutorial/lesson8.html>
// const    - <http://duramecho.com/ComputerInformation/WhyHowCppConst.html>

#define ARRAY_ENTRIES(ARRAY)    (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t       pinLED_0        =  3;
const uint8_t       pinLED_1        =  4;
const uint8_t       pinLED_2        =  5;
const uint8_t       pinLED_3        =  6;
const uint8_t       pinLED_4        =  7;
const uint8_t       pinLED_5        =  8;
const uint8_t       pinLED_6        =  9;
const uint8_t       pinLED_7        = 10;

const uint8_t       LED_OFF         = LOW;
const uint8_t       LED_ON          = HIGH;

const uint8_t       pinsLEDS[]      = { pinLED_0, pinLED_1, pinLED_2, pinLED_3, pinLED_4, pinLED_5, pinLED_6, pinLED_7 };

const unsigned long tmTENTH_SECOND   = 100UL;

void setLED(uint8_t pin, uint8_t state)
{
   digitalWrite(pin, state);
}

void flashLED(uint8_t pin, int repeat)
{
   for ( int i = repeat; i--; )
   {
       digitalWrite(pin, !digitalRead(pin));
       delay(tmTENTH_SECOND);

       digitalWrite(pin, !digitalRead(pin));
       delay(tmTENTH_SECOND);
   }
}

void loop()
{
   for ( int i = 0; i < ARRAY_ENTRIES(pinsLEDS); i++ )
   {
       flashLED(pinsLEDS[i], 8);
       delay(tmTENTH_SECOND);
   }
}

void setup()
{
   for ( int i = ARRAY_ENTRIES(pinsLEDS); i-- ; )
   {
       pinMode(pinsLEDS[i], OUTPUT);
       setLED(pinsLEDS[i], LED_OFF);
   }
}

Pedro147

Thanks for that lloyddean and for the links I haven't seen them before, hopefully they will help me  8). When I tried to compile your code there is an error in void loop,  line 42 " i LED  not declared in this scope" How do I fix that please.

lloyddean

Sorry about that, thus the warning - fixed above.

Pedro147

Thank you for that and no need to be sorry ...your helping me. When I get home from work I'll try that code out and then attempt to fathom how it works. I like your style of coding which is obviously heavily based on C++

lloyddean

Just a reminder - this is C++ minus the C++ Standard Libraries.

Go Up