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.
http://www.pedroduino.com

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++
http://www.pedroduino.com

lloyddean

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

Pedro147

#50
Nov 01, 2012, 03:59 am Last Edit: Nov 01, 2012, 06:07 am by Pedro147 Reason: 1
I'll remember that. Yes I think that I read somewhere, that you can use some C++ syntax in the Arduino environment, but anything that requires the use of C++ libraries will not work. Thanks again for your help lloyddean.
Update -
Hello lloyddean, I thought that you would like to know how your code performed and there is a slight problem. It flashes the first LED eight times (the number of array elements as far as I can ascertain from the code) but then does not loop through the rest of the LED's . Now I do not wish to make a nuisance of myself (anymore than I already have) and I really only wanted to work this code out as a learning exercise thinking that it can't be that hard, and you know the rest of the story. I adjusted the "const unsigned long tmTENTH_SECOND" from 100 UL TO 500 UL to more clearly see the number of flashes and the interval between groups of flashes, and shot this short clip to show you. I will not be offended if you wish to leave it at that as I'm sure you have other things you would rather be doing than this  8) but if your up for a challenge‚Ķ Thanks again Pedro.
https://www.youtube.com/watch?v=0ybwXILwH5I&feature=plcp
http://www.pedroduino.com

lloyddean

Again sorry, a silly error on the 'ARRAY_ENTRIES' macro.

Changed code above.

Nick Gammon


you can use some C++ syntax in the Arduino environment, but anything that requires the use of C++ libraries will not work.


What are you talking about? The Arduino IDE uses C++. There is nothing about "some" C++ syntax.

Quote
... anything that requires the use of C++ libraries will not work ...


No. Some libraries are implemented. Next you'll be saying that strcpy doesn't work. A library is a library. If it is present you can use it. For example, you can use the STL library if you install it.
http://www.gammon.com.au/electronics

Pedro147

Thanks lloyddean it is working like a charm now, and all because of a missing set of square braces and a zero  8)
http://www.pedroduino.com

lloyddean

#54
Nov 01, 2012, 05:16 pm Last Edit: Nov 01, 2012, 06:57 pm by lloyddean Reason: 1
EDIT: Making the 'for' loops 'i' variable 'unsigned' normalize the 'potential' for overflow vs the original

You can shorten the code a bit with a simple change to 'flashLED'.

Code: [Select]

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

dhenry

Code: [Select]
void flashLED(uint8_t pin, int repeat)
{
    for ( repeat = repeat * 2; repeat--; )
    {
        digitalWrite(pin, !digitalRead(pin));
        delay(tmTENTH_SECOND);
    }
}


saves you two bytes. I do think it better not to double repeat: it creates a potential overflow.

lloyddean

#56
Nov 01, 2012, 06:49 pm Last Edit: Nov 01, 2012, 07:06 pm by lloyddean Reason: 1
The 'potential' overflow exists either way, it's just that this way it comes at you twice as fast!

EDIT: Your way violates a general rule I have about modifying a functions passed in parameters.
EDIT: Modified above to compensate.

Pedro147

Thanks for that lloyddean, and I hope I did not upset you with my apparent ignorance when I said
Quote - "Yes I think that I read somewhere, that you can use some C++ syntax in the Arduino environment, but anything that requires the use of C++ libraries will not work"

I know virtually nothing about any form of coding and that's why I am here to hopefully learn through the help of generous people such as yourself and others. Have a good day Pedro.
http://www.pedroduino.com

Nick Gammon

I responded to that, because tomorrow someone will say "I read somewhere that libraries don't work. Ah yes, it was in a thread by Pedro147."

So your vague recollection (not backed up by any reference) will be taken as fact by other people.
http://www.gammon.com.au/electronics

Pedro147

Point taken Nick. I will be more discerning with any comments in future. You have a good day too.
http://www.pedroduino.com

Go Up