Pages: 1 2 3 [4] 5 6   Go Down
Author Topic: Apprentice coder  (Read 4190 times)
0 Members and 1 Guest are viewing this topic.
Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple but untested!

Code:
// 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 Offline
God Member
*****
Karma: 8
Posts: 504
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for that lloyddean and for the links I haven't seen them before, hopefully they will help me  smiley-cool. 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
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry about that, thus the warning - fixed above.
Logged

Canberra Australia
Offline Offline
God Member
*****
Karma: 8
Posts: 504
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Canberra Australia
Offline Offline
God Member
*****
Karma: 8
Posts: 504
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-cool 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
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Changed code above.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Canberra Australia
Offline Offline
God Member
*****
Karma: 8
Posts: 504
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks lloyddean it is working like a charm now, and all because of a missing set of square braces and a zero  smiley-cool
Logged


Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
God Member
*****
Karma: 8
Posts: 504
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
God Member
*****
Karma: 8
Posts: 504
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Point taken Nick. I will be more discerning with any comments in future. You have a good day too.
Logged


Pages: 1 2 3 [4] 5 6   Go Up
Jump to: