Pages: 1 [2] 3 4 ... 6   Go Down
Author Topic: Apprentice coder  (Read 5060 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26273
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your led array as posted has only six elements, but your bit pattern spans eight.
Post your actual code instead of the second snippet, please.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

Here is what I tried AWOL

Code:

int timer = 100;
int pins [6] = { 3, 4, 5, 6, 7, 8 };
char led = B10000001;
int mask = 1;

 

 void setup ()
 
  {
    for(int i = 3; i < 9; i++) pinMode(i, OUTPUT);
  }
 
 
 void loop ()
 
 {

for(int i=0; i<7; i++)
{


 { if((mask & led) == 0)
  digitalWrite(pins[i], LOW);
  else digitalWrite(pins[i], HIGH);

 mask = mask << 1;
 
 }
}
}


I see what you mean re the discrepancy between six array elements and a byte for the bit pattern  smiley-mr-green
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Tools + Auto Format menu item needs to become your friend.

You need to learn where { and } are needed (and where they are not, but are a good idea), and where they are not.

The code after an if or else statement should be in { and }. The if statement itself does not need to be.

The code for a for or if statement should be placed on different lines from the statement.

Code:
    for(int i = 3; i < 9; i++) pinMode(i, OUTPUT);
should be:
Code:
    for(int i = 3; i < 9; i++)
    {
      pinMode(i, OUTPUT);
    }

You can add a Serial.begin() call to setup() and Serial.print() statements to loop() to see what is happening.
Logged

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

Thank you for your response PaulS.  I see what you are saying regarding the curly braces and using the serial monitor  and point taken,  but if I change  the code in setup to how you suggest I get errors
"expected initialiser before"for"
"expected constructor, destructor or type conversion before < token"
"expected constructor, destructor or type conversion before ++ token"

Also the initial code that I posted (the code that did work)
Code:
     int timer = 100;
  int pins [6] = { 3, 4, 5, 6, 7, 8 };
  boolean led [6] = { HIGH, LOW, LOW , LOW, LOW , HIGH };
 
  void setup ()
 
  {
    for(int i = 3; i < 9; i++) pinMode(i, OUTPUT);
  }
 
   void loop ()
 
  {
    for(int i=0; i<7; i++) digitalWrite(pins[i], led[i]);
  }
also had this for loop inside the curly braces and I am not doubting your expertise  smiley-red  but I do not understand how your suggestion can help with the problem that I am having. Am I missing something  Pedro.
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26273
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No problems with that code here
Quote
Binary sketch size: 934 bytes (of a 30,720 byte maximum)

That is to say, the compiler is OK with it.
I still have issues about the size of the arrays (or the loop lengths, whichever way you look at it)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

AWOL yes that compiled ok for me too. With regard to the "I still have issues about the size of the arrays (or the loop lengths, whichever way you look at it)" I  added two more LED's (now eight) and changed the code to
Code:
int timer = 100;
int pins [8] = {
  3, 4, 5, 6, 7, 8, 9, 10 };
char led = B10000001;
int mask = 1;



void setup ()

{
for(int i = 3; i < 11; i++)
  pinMode(i, OUTPUT);
}


void loop ()

{

  for(int i=0; i<9; i++)
  {


    {
      if((mask & led) == 0)
        digitalWrite(pins[i], LOW);
      else digitalWrite(pins[i], HIGH);


      mask = mask << 1;


    }
  }
}

but still no luck. I suppose what I am asking is does the format of the code appear to be correct.The " mask = mask << 1;" at the end of the code doesn't quite look right to my very inexperienced eye  smiley-cool

Thanks for your help Pedro.
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26273
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for(int i=0; i<9; i++)
Still unhappy.

How about the debug prints?

You might want to move "mask" and its initialsation into "loop ()"
« Last Edit: October 26, 2012, 09:24:53 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

I don't mean to sound unhappy, just frustrated  smiley-mr-green But suffice to say AWOL, that edit you just did suggesting
"You might want to move "mask" and its initialsation into "loop ()" did the trick. I tried the dedug print but something just flashed across the screen at light speed and  I couldn't remember enough about the serial monitor function to make it stay still until I could read it. So thanks very much  for your help and that gives me something to work with. Also thanks to PaulS for his suggestions and help. You fellas are so cluey  smiley-cool
Logged


UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

did the trick.

The latest code you posted is still running off the end of the array. It might run and look OK, but it's not right yet.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26273
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't mean to sound unhappy, just frustrated
No, it is me that is still unhappy with the loop I quoted.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

All is good that ends well AWOL. Thanks again for your help
Logged


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

did the trick.

The latest code you posted is still running off the end of the array. It might run and look OK, but it's not right yet.

Can you offer any suggestions as to how I can fix this problem, because I prefer not to labour under any misapprehensions that the code is correct. Thanks PeterH.
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26273
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you've declared an array, then it is best to use it everywhere.
At the moment you're not using it in "setup()".

Also, if you make the array of the correct type, "byte", you can use the size of the array to control your loops, so you should never step off the end.
Code:
#define N_ELEMENTS(array) (sizeof(array)/sizeof(array[0]))
.

(In fact, this method works for arrays of any datatype)
« Last Edit: October 26, 2012, 11:14:15 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for(int i=0; i<9; i++)

You're processing elements 0 .. 8 of an array that contains elements 0 .. 7.

Best to make your 'for' loop index be the same as the index of the array being processed in the loop, and define the loop in the same way that you defined the array. The best way is to work out the size of the array as AWOL shows you. If the array is explicitly sized when it is declared, another option is to specify the size using a constant and use that same constant as the bound of the for loop. Here you've defined them separately and they have ended up different. One of the fundamental principles of good design is Don't Repeat Yourself (DRY). If you're defining two things that need to match then you should look for ways to eliminate one of the definitions.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 536
Posts: 27061
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe I didn't look back far enough:
did you initialize mask to 1 so you have a bit to shift across?
Code:
mask = 1;
for (whatever){
anding operation;
mask = mask<<1;
}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

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