Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #15 on: October 26, 2012, 07:24:23 am » |
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.
|
|
|
|
Canberra Australia
Offline
Sr. Member
Karma: 5
Posts: 300
Hardcore Arduino addict
|
 |
« Reply #16 on: October 26, 2012, 07:35:25 am » |
Here is what I tried AWOL 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 
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #17 on: October 26, 2012, 08:24:41 am » |
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. for(int i = 3; i < 9; i++) pinMode(i, OUTPUT); should be: 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
Sr. Member
Karma: 5
Posts: 300
Hardcore Arduino addict
|
 |
« Reply #18 on: October 26, 2012, 08:55:00 am » |
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) 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  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
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #19 on: October 26, 2012, 09:01:53 am » |
No problems with that code here 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.
|
|
|
|
Canberra Australia
Offline
Sr. Member
Karma: 5
Posts: 300
Hardcore Arduino addict
|
 |
« Reply #20 on: October 26, 2012, 09:16:07 am » |
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 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  Thanks for your help Pedro.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #21 on: October 26, 2012, 09:22:31 am » |
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.
|
|
|
|
Canberra Australia
Offline
Sr. Member
Karma: 5
Posts: 300
Hardcore Arduino addict
|
 |
« Reply #22 on: October 26, 2012, 09:43:26 am » |
I don't mean to sound unhappy, just frustrated  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 
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 99
Posts: 6784
-
|
 |
« Reply #23 on: October 26, 2012, 09:47:30 am » |
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
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #24 on: October 26, 2012, 09:51:43 am » |
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.
|
|
|
|
Canberra Australia
Offline
Sr. Member
Karma: 5
Posts: 300
Hardcore Arduino addict
|
 |
« Reply #25 on: October 26, 2012, 10:06:19 am » |
All is good that ends well AWOL. Thanks again for your help
|
|
|
|
|
Logged
|
|
|
|
|
Canberra Australia
Offline
Sr. Member
Karma: 5
Posts: 300
Hardcore Arduino addict
|
 |
« Reply #26 on: October 26, 2012, 10:13:09 am » |
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
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #27 on: October 26, 2012, 11:09:42 am » |
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. #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.
|
|
|
|
UK
Offline
Tesla Member
Karma: 99
Posts: 6784
-
|
 |
« Reply #28 on: October 26, 2012, 01:38:21 pm » |
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
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 269
Posts: 17035
Available for Design & Build services
|
 |
« Reply #29 on: October 26, 2012, 01:49:53 pm » |
Maybe I didn't look back far enough: did you initialize mask to 1 so you have a bit to shift across? mask = 1; for (whatever){ anding operation; mask = mask<<1; }
|
|
|
|
|
Logged
|
|
|
|
|
|