I did not have a seven segment display so I hooked six LED's up to digital pins 3 to 8 on an Uno and progressed through the tutorial until I successfully got to the stage of running this 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]);
}
The next stage of the tutorial expands the code to use char, a mask and a bitwise AND operation. So obviously I would need to incorporate this code into the previously posted code
I tried to put char led = B10000001; and int mask = 1; at the top of the code prior to setup and the if else and mask code into loop which did compile but did not light LED's 1 and 6 as hoped. I realise that this is a roundabout way to light two LED's but as I said at the start I am doing this to learn how to code and feel that this is an important coding technique to understand and use.
Thank you for taking the time to read this and hopefully explain what I am doing wrong,
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.
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
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 8)
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 8)
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.
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)
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.
So do I just place this definition at the start of the code and then somehow use "(sizeof(array)/sizeof(array[0]))" in the for loops where I was using int i
I apologise for asking so many questions but with all the suggestions and advice that awaited me when I arose this morning, I dont't know how to procede
In regard to PeterH's observation
PeterH:
Pedro147:
for(int i=0; i<9; i++)
You're processing elements 0 .. 8 of an array that contains elements 0 .. 7.
So I also changed i<9 to i<8
Also Crossroads - yes I did
mask = 1;
for (whatever){
anding operation;
mask = mask<<1;
}
didn't I ? 8)
That would fix the problem, but leaves you using an approach which makes this sort of problem likely.
You have a magic number ( 8 ) in several places in your code and the code will go wrong if they do not all match. Better to define the value as a constant and use it in all the places that need it.
Also, get into the habit of using a code block enclosed by { } after every control expression (if, else, for, while, do) even when you only have a single statement in it. It makes it much less likely that you will accidentally end up with a control structure that looks right but is actually wrong.
I also recommend that you put each { and } on a separate line with matching pairs indented by the same amount, and the contents indented one extra level. It makes it much easier to visually scan the structure of your code to confirm it matches your intention.