Pages: 1 ... 3 4 [5] 6   Go Down
Author Topic: Apprentice coder  (Read 4694 times)
0 Members and 1 Guest are viewing this topic.
Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 514
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In Reply #44 I was asking how I might code the Knightrider type code to flash an LED sequentially x number of  times while looping through the array. I have had some much appreciated help and have learn't some useful techniques but to be honest trying to work out how some of the sections of code work is perplexing to me.  I have "dumbed down" version working but cannot work out how to efficiently code the digitalWrite HIGH/LOW and delay section of the code so that it turns the LED on then off "x" number of times.
Code:

int timer = 300;          
int led[] = { 3, 4, 5, 6, 7, 8, 9, 10 };      
int pinCount = 8;

void setup()
{
  for(int i = 0; i < pinCount; i++)
  {
    pinMode(led[i], OUTPUT);
  }
}

void loop()
{
  for(int i = 0; i < pinCount-1; i++)
  {
    digitalWrite( led[i],HIGH);
    delay(timer);
    digitalWrite( led[i],LOW);
    delay(timer);
    digitalWrite( led[i],HIGH);
    delay(timer);
    digitalWrite( led[i],LOW);
    delay(timer);

  }

  {
    for(int i = pinCount-1; i > 0; i--)
    {
      digitalWrite( led[i],HIGH);
      delay(timer);
      digitalWrite( led[i],LOW);
      delay(timer);
      digitalWrite( led[i],HIGH);
      delay(timer);
      digitalWrite( led[i],LOW);
      delay(timer);
    }
  }
}
As I stated I do not mean to be unappreciative of the help I have received on this to date but if I cannot see how some code is working I will never progress and give you guys some piece and quite.  smiley-red Thanks Pedro.
« Last Edit: November 02, 2012, 05:58:07 am by Pedro147 » Logged


Norfolk UK
Offline Offline
Edison Member
*
Karma: 66
Posts: 2475
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hopefully just using lots of comments will help. A couple of possible errors in your code where the first and last LED will not turn on/off
Code:
int timer = 300;                            // Delay time between LED turning on/off
int led[] = { 3, 4, 5, 6, 7, 8, 9, 10 };    // Array of arduino pins the leds are connected to
int pinCount = 8;                           // The number of LED's connected to pins

void setup()
{
    for(int i = 0; i < pinCount; i++)         // Loop through array from 0 to pinCount-1
    {
        pinMode(led[i], OUTPUT);                // Setting each pin to output
    }
}

void loop()
{
    for(int i = 0; i < pinCount-1; i++)       // Loop through array from 0 to pinCount-2 (Should be i < pinCount)
    {
        digitalWrite( led[i],HIGH);             // Turn LED i on
        delay(timer);                           // Delay a bit
        digitalWrite( led[i],LOW);              // Turn LED i off
        delay(timer);                           // Delay a bit
        digitalWrite( led[i],HIGH);             // Turn LED i on
        delay(timer);                           // Delay a bit
        digitalWrite( led[i],LOW);              // Turn LED i off
        delay(timer);                           // Delay a bit
    }                                           // Back for next i
   
    {                                           // Not needed
        for(int i = pinCount-1; i > 0; i--)     // Loop through array from pinCount-1 to 1 (Should be i >= 0)
        {
            digitalWrite( led[i],HIGH);         // Turn LED i on
            delay(timer);                       // Delay a bit
            digitalWrite( led[i],LOW);          // Turn LED i off
            delay(timer);                       // Delay a bit
            digitalWrite( led[i],HIGH);         // Turn LED i on
            delay(timer);                       // Delay a bit
            digitalWrite( led[i],LOW);          // Turn LED i off
            delay(timer);                       // Delay a bit
        }                                       // Back for next i
    }                                           // Not needed
}                                               // Back to loop start
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

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

In Reply #44 I was asking how I might code the Knightrider type code to flash an LED sequentially x number of  times while looping through the array. I have had some much appreciated help and have learn't some useful techniques but to be honest trying to work out how some of the sections of code work is perplexing to me.  I have "dumbed down" version working but cannot work out how to efficiently code the digitalWrite HIGH/LOW and delay section of the code so that it turns the LED on then off "x" number of times.

... snipped some code ...

 As I stated I do not mean to be unappreciative of the help I have received on this to date but if I cannot see how some code is working I will never progress and give you guys some piece and quite.  smiley-red Thanks Pedro.

A more useful approach (it seems to me) would be for you to ask questions, concerning the code given, as to what you do not understand.

Given the function …

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

Whenever 'digitalWrite', on a pin whose value is set as 'OUTPUT' with 'pinMode', is called it internally tracks the last value the pin was set to.

'digitalRead(pin)' reads the last value sent to 'digitalWrite' and returns it.

The '!' in front of the 'digitalWrite' inverts the value returned.  If 'digitalWrite' returned '0' it changes it to '1' and if it returned '1' it changes it to a '0'.

'0' equals 'false' while any non '0' value equals 'not false'

'digitalWrite' then sets the new value which is the opposite of the current value toggling the LED from 'on' to 'off', 'off' to 'on'

By the way LOW a synonym for '0' and 'HIGH' is a synonym for '1'

DOes this clear anything up for you?
Logged

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

Riva -

Yes Riva regarding the comments, I know that I should have them (slaps back of own wrist  smiley-mr-green  ) as they help to explain my intentions, and sometimes it's easy to forget what you were initially trying to achieve. With the actual pin Count reference in the two for loops of void loop
Quote from your comment -

for(int i = 0; i < pinCount-1; i++)       // Loop through array from 0 to pinCount-2 (Should be i < pinCount)

if I change pinCount - 1 to pinCount in both for loops LED's 1 and 8 will flash on / off four times each while LED's 2 - 7 still flash at the set rate of on / off twice, so I am a little confused there. Thanks for the help and in future I will comment on all my code. 

Lloyddean - 

So if I might paraphrase your explanation. In your  flashLED function, you are setting pin to to a value opposite to what it was last set to. This is achieved by the ! in front of digitalRead. Effectively this function turns the LED on / delay / off ,  on / delay / off  a number of times as dictated by the  numeral 8 in this section of void loop -

 flashLED(pinsLEDS, 8 );

So  in a basic digitalWrite you nominate a pin and then a value (usually HIGH or LOW) In your  flashLED function you set the value via !digitalRead(pin) which inverts the last value of digitalWrite generating the "flashing effect"

The way that I am looking at it, that takes care of the "what to do" part of the function  but the "when to do it" part -

for ( unsigned int i = repeat * 2; i--; )

and the -

void flashLED(uint8_t pin, int repeat)

sections still confuse me. I hope that from my explanation it sounds like some of your hard work here is starting to pay off. Thanks Pedro.


Logged


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

The way that I am looking at it, that takes care of the "what to do" part of the function  but the "when to do it" part -

for ( unsigned int i = repeat * 2; i--; )

and the -

void flashLED(uint8_t pin, int repeat)

sections still confuse me. I hope that from my explanation it sounds like some of your hard work here is starting to pay off. Thanks Pedro.

So 'what' is it you're confused about.  It would help you if you tried to be more specific in relating your confusion as it is very difficult to 'guess' and provide explanations that are useless to you.

EDIT:

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

A flash consists of an 'ON' state of some length, followed by an 'OFF' state of some, perhaps equal, length.  That's two separate state changes that need to occur eight time for a total of 16 state changes.
« Last Edit: November 02, 2012, 07:51:29 pm by lloyddean » Logged

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

Sorry I thought that my question was clear but obviously it was not.I will use the question marks so that no one thinks that I am stating a fact, only asking if I am correct.

What does "void flashLED(uint8_t pin, int repeat)" mean.

Void means - returns no value ?
flashLED - is the name of the function ?
uint8_t and int - are data types ?

In the for loop what does "repeat * 2" - * is a multiplier but that's all I understand here.

Thank you for your patience and did it seem like I understood your explanation re the setLED function.

EDIT - I just saw your edit so I will have to think about that too (and I thought that I was confused before  smiley-cool )
« Last Edit: November 02, 2012, 08:14:51 pm by Pedro147 » Logged


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

Your understanding of what the function does seems correct while your understanding of how to declare and or call functions needs work.  None of that is meant to imply any kind of insult.

Since I'm not into writing long replies (and it's not that I don't think you're not worth the effort) I'll send you off to <http://www.cplusplus.com/doc/tutorial/functions/> for an explanation.

Please keep asking concerning things you don't understand.
Logged

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

Thanks for your patience and the link to hopefully further understanding You are a good teacher and if I could give you an apple I would ( my Aussie sense of humour - we are a strange bunch down here)  smiley-red
Logged


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


What does "void flashLED(uint8_t pin, int repeat)" mean.

Void means - returns no value ?
flashLED - is the name of the function ?
uint8_t and int - are data types ?


Code:
void flashLED(uint8_t pin, int flash_count)

This is the beginnings of the actual definition of a subroutine, much like 'digitalRead' or 'delay'.

The function returns a 'void', void meaning nothing is returned from the function.

The name of the function is 'flashLED' and needs two parameters passed to it.

The first parameter has a suggestive name of 'pin' and the second parameter a suggestive name of 'flash_count'. 'pin' specifies which pin the LED is connected to while the 'flash_count' communicates how many times the LED on 'pin' is to be toggled 'ON' and 'OFF'.

EDITED:
« Last Edit: November 02, 2012, 08:39:49 pm by lloyddean » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


What does "void flashLED(uint8_t pin, int repeat)" mean.

Void means - returns no value ?
flashLED - is the name of the function ?
uint8_t and int - are data types ?


Code:
void flashLED(uint8_t pin, int flash_count)

This is the beginnings of the actual definition of a subroutine, much like 'digitalRead' or 'delay'.

The function returns a 'void', void meaning nothing is returned from the function.

The name of the function is 'flashLED' and needs two parameters passed to it, both of which are of type 'int' which are 16 bit signed values.

Actually one parameter of type uint8_t and the second a type int.

The first parameter has a suggestive name of 'pin' and the second parameter a suggestive name of 'flash_count'. 'pin' specifies which pin the LED is connected to while the 'flash_count' communicates how many times the LED on 'pin' is to be toggled 'ON' and 'OFF'.

Not really 'suggestive names', but rather the actual names of the variables the function will use to utilize the values passed to them within the functions coding statements.


Logged

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

More accurate would be - parameter name suggestive of implied use.

See <http://www.cplusplus.com/doc/tutorial/variables/>
<http://www.cplusplus.com/doc/tutorial/constants/>
<http://www.cplusplus.com/doc/tutorial/operators/>
« Last Edit: November 02, 2012, 09:12:18 pm by lloyddean » Logged

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

lloyddean I knew exactly what you mean't. I try not to be too pedantic smiley-cool Thanks again, I'm playing around with inserting the digitalWrite ! digitalRead ( syntax ? ) into my code in progress, just need to nut out the flash_count aspect, although I will definitely be studying the cplusplus tutorials as I know my understanding needs vast amounts of improvement.
« Last Edit: November 02, 2012, 09:56:12 pm by Pedro147 » Logged


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

Here are the declaration (as opposed to definitions) of some subroutines, functions, that you are more familiar with -

Code:
void pinMode(uint8_t pin, uint8_t mode);
uint8_t getPinMode(uint8_t pin);
void digitalWrite(uint8_t pin, uint8_t val);
int digitalRead(uint8_t pin);
Logged

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

Thanks for that and I am determined to get a firmer grasp of this.
« Last Edit: November 03, 2012, 06:55:41 am by Pedro147 » Logged


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

I have been working on coding a 7 seg LED display to display from zero through to nine in a loop. As I am awaiting delivery of these displays, I am experimenting with using eight LED's in a row to display binary patterns approximating what will be required to display zero through nine when the 7 seg's arrive. The code that I am using is heavily based on Grumpy Mike's tutorial at  http://www.thebox.myzen.co.uk/Tutorial/Arrays.html
Code:

// Thanks to Grumpy Mike  http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

int pins [] = { 2, 3, 4, 5, 6, 7, 8, 9 };
int digit[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
int timer = 1000;
char eightCode[] = { B01111110, B00110000, B01101101, B01111001, B00110011, B01011011, B01011111, B01110000 };
// Array element #       0          1          2          3          4          5          6          7


void setup()
{
  for(int i = 0; i < 8; i++)         // set digital pins as OUTPUTS
    pinMode(pins[i], OUTPUT);
}

void loop()
{

  for(int j = 0; j < 8; j++)        // loop through digit array
  {
    display8(digit[j]);             // function to display elements of eightCode array
    delay(timer);                   // delay between displaying of eightCode elements

  }
}





void display8(int num)
{
  int mask = 1;
  for(int i = 0; i < 8; i++)
  {
    if((mask & eightCode[num]) == 0)
      digitalWrite(pins[i], LOW);
    else digitalWrite(pins[i], HIGH);
    mask = mask << 1;
  }
}

I was just hoping that someone could have a look at the code and point out any glaring mistakes or improvements I could implement. It seems to be doing what I hoped it would do so I suppose that is a good sign.   smiley-cool Thanks Pedro.

Logged


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