Des Moines, WA - USA
Online
God Member
Karma: 21
Posts: 703
|
 |
« Reply #45 on: October 31, 2012, 01:25:45 pm » |
Simple but untested! // 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); } }
|
|
|
|
« Last Edit: November 01, 2012, 01:37:43 am by lloyddean »
|
Logged
|
|
|
|
|
Canberra Australia
Offline
Sr. Member
Karma: 5
Posts: 273
Enthusiastic Newbie
|
 |
« Reply #46 on: October 31, 2012, 01:50:27 pm » |
Thanks for that lloyddean and for the links I haven't seen them before, hopefully they will help me  . 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.
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Online
God Member
Karma: 21
Posts: 703
|
 |
« Reply #47 on: October 31, 2012, 01:54:19 pm » |
Sorry about that, thus the warning - fixed above.
|
|
|
|
|
Logged
|
|
|
|
|
Canberra Australia
Offline
Sr. Member
Karma: 5
Posts: 273
Enthusiastic Newbie
|
 |
« Reply #48 on: October 31, 2012, 03:40:07 pm » |
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++
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Online
God Member
Karma: 21
Posts: 703
|
 |
« Reply #49 on: October 31, 2012, 09:21:04 pm » |
Just a reminder - this is C++ minus the C++ Standard Libraries.
|
|
|
|
|
Logged
|
|
|
|
|
Canberra Australia
Offline
Sr. Member
Karma: 5
Posts: 273
Enthusiastic Newbie
|
 |
« Reply #50 on: October 31, 2012, 09:59:17 pm » |
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  but if your up for a challenge… Thanks again Pedro. https://www.youtube.com/watch?v=0ybwXILwH5I&feature=plcp
|
|
|
|
« Last Edit: November 01, 2012, 12:07:38 am by Pedro147 »
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Online
God Member
Karma: 21
Posts: 703
|
 |
« Reply #51 on: November 01, 2012, 01:40:55 am » |
Again sorry, a silly error on the 'ARRAY_ENTRIES' macro.
Changed code above.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #52 on: November 01, 2012, 01:58:49 am » |
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. ... 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.
|
|
|
|
|
Logged
|
|
|
|
|
Canberra Australia
Offline
Sr. Member
Karma: 5
Posts: 273
Enthusiastic Newbie
|
 |
« Reply #53 on: November 01, 2012, 02:24:49 am » |
Thanks lloyddean it is working like a charm now, and all because of a missing set of square braces and a zero 
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Online
God Member
Karma: 21
Posts: 703
|
 |
« Reply #54 on: November 01, 2012, 11:16:42 am » |
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'. void flashLED(uint8_t pin, int repeat) { for ( unsigned int i = repeat * 2; i--; ) { digitalWrite(pin, !digitalRead(pin)); delay(tmTENTH_SECOND); } }
|
|
|
|
« Last Edit: November 01, 2012, 12:57:30 pm by lloyddean »
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #55 on: November 01, 2012, 11:23:16 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Online
God Member
Karma: 21
Posts: 703
|
 |
« Reply #56 on: November 01, 2012, 12:49:58 pm » |
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.
|
|
|
|
« Last Edit: November 01, 2012, 01:06:54 pm by lloyddean »
|
Logged
|
|
|
|
|
Canberra Australia
Offline
Sr. Member
Karma: 5
Posts: 273
Enthusiastic Newbie
|
 |
« Reply #57 on: November 01, 2012, 03:35:54 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #58 on: November 01, 2012, 03:45:35 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Canberra Australia
Offline
Sr. Member
Karma: 5
Posts: 273
Enthusiastic Newbie
|
 |
« Reply #59 on: November 01, 2012, 03:53:26 pm » |
Point taken Nick. I will be more discerning with any comments in future. You have a good day too.
|
|
|
|
|
Logged
|
|
|
|
|
|