In my quest for learning the foreign language of code (humor) I am working in the book Electronics for Dummies and going through the projects there are some good examples. I am doing my own experiments with the code to get a better understanding (I have come to realize this is a good way to get a better understanding of how this new language works).
So, in the book there is a sketch that flashes 8 LEDs in sequence 1 at a time for a set time (determined by the Time variable).
The code in the book:
// this program flashes LEDs connected to digital pins 0 - 7, they are flashed one at a time in sequence 0 - 7 for time duration
int Time;
int Pin;
// the time and pin variables
void setup() {
Pin = 0;
while (Pin < 8)
{
pinMode(Pin, OUTPUT);
Pin++;
}// set pins 0 - 7 as outputs
Time = 150;
}
void loop() {
Pin = 0;
while (Pin < 8)
{
digitalWrite(Pin, HIGH);
delay(Time);
digitalWrite(Pin, LOW);
Pin++;
}
This code runs as expected. Then my thought was to modify this to have the LEDs flash on 1 at a time and stay on until the 8th had lit then go off and start again. I shifted the digitalWrite(Pin, LOW); to the last line thinking the LEDs would light 1 at a time and go off... no the first 2 lit up, paused, then the first one was lit and progressed to light the rest in sequence and all stay on. Code is here:
// this program flashes LEDs connected to digital pins 0 - 7, they are flashed one at a time in sequence 0 - 7 for time duration
int Time;
int Pin;
// the time and pin variables
void setup() {
Pin = 0;
while (Pin < 8)
{
pinMode(Pin, OUTPUT);
Pin++;
}// set pins 0 - 7 as outputs
Time = 150;
}
void loop() {
Pin = 0;
while (Pin < 8)
{
digitalWrite(Pin, HIGH);
delay(Time);
Pin++;
digitalWrite(Pin, LOW);
}
}
The thing I am not getting, well 2, is why the LEDs are not turning off (I must be missing something) and why 2 LEDs come on at first, then goes to 1 and progresses.
Please excuse my ignorance here, trying to understand. Any guidance to get my thick skull on the right track would be greatly appreciated.
No change, to be sure I di as instructed, here is the code:
// this program flashes LEDs connected to digital pins 0 - 7, they are flashed one at a time in sequence 0 - 7 for time duration
int Time;
int Pin;
// the time and pin variables
void setup() {
Pin = 0;
while (Pin < 8)
{
pinMode(Pin, OUTPUT);
Pin++;
}// set pins 0 - 7 as outputs
Time = 150;
}
void loop() {
Pin = 0;
while (Pin < 8)
{
digitalWrite(Pin, HIGH);
Pin++;
delay(Time);
}
digitalWrite(Pin, LOW);
}
I didn't find any such program in my ancient copy of Electronics for Dummies. But never mind.
I assume this is being done on an Uno R3? If so, you should avoid using pins 0 and 1 as they're also used for the Serial port. Even though your sketch doesn't use Serial, using them for other purposes is something to be avoided for the beginner.
Here's a sketch that lights 8 LEDs hooked up to pins 2 through 9 in sequence. First one LED, then two, then three, all the way to eight and then repeat.
If you are bound and determined not to shift your LEDs, simply change the value of firstLEDpin to 0.
// this program flashes LEDs connected to digital pins 2 - 9, they are flashed one at a time in sequence 2 - 9 for time duration
const byte numLEDs = 8;
const byte firstLEDpin = 2;
int Time;
int Pin;
// the time and pin variables
void setup() {
Pin = 0;
while( Pin < numLEDs ) {
pinMode(Pin + firstLEDpin, OUTPUT);
Pin++;
} // set pins 2 - 9 as outputs
Time = 150;
}
void loop() {
Pin = 0;
while( Pin < numLEDs ) {
digitalWrite(Pin + firstLEDpin, HIGH);
delay(Time);
Pin++;
}
for( Pin = 0; Pin < numLEDs; ++Pin ) {
digitalWrite(Pin + firstLEDpin, LOW);
}
delay(Time);
}
Did as you explained. Relocated the wiring on the UnoR3 and now the 2 LEDs that were on in the beginning are off then the sketch lights 1 at a time. Yet all of them stay on :^/
I am in the 3rd edition listing 2-6: just in case...
And yes UnoR3 is the board. I have rearranged the connections and the sketch as recommended by LarryD. It did eliminate the 2 LEDs on at the beginning.
I will give your suggestion a try also, trying to wrap my mind around it all. I will get it, might take a few...
Your sketch does not have any code to turn all of the LEDs off at once. After you have turned each of them on one by one add a second for loop that turns each of them off one by one but with no delay() in it
// this program flashes LEDs connected to digital pins 2 - 9, they are flashed one at a time in sequence 2 - 9 for time duration
int Time;
int Pin;
int State;
// the time and pin variables
void setup() {
Pin = 2;
while (Pin < 10)
{
pinMode(Pin, OUTPUT);
Pin++;
}// set pins 2 - 9 as outputs
Time = 150;
State = HIGH;
}
void loop() {
Pin = 2;
while (Pin < 10)
{
digitalWrite(Pin, State);
delay(Time);
Pin++;
}
if (State == HIGH) State = LOW; else State = HIGH;
}
When I was young, we had this “paper computer”, there was long paper for “instructions” and few for “registers”, hard paper with windows for these papers metioned and paper with “instructions allowed”.
And we read “actual instruction” (like “add 5 to register A”) and interpreted it (if in register A was 7, we crossed it and wrote 12 there), then moved to next instruction.
I use similar technique even today, when I meet “strange“ problem with code.
Well I use text editor and “variables” and so on, but going step by step thru program and emulate it with my own hands make some “strange” things clear.
I suggest to you to do the same.
If you just look on code in foreign language, your eyes catch on the recognised part and you brain interpolate the rest somehow (it is how human functions).
When you force your hands to actually register each step, you will immediately spot, that the “second version of code in your question” force you to lit off the next LED, not the one, you wanted.
And that it is strange.
And that you probably wanted to do something else.
Just need to figure out what exactly it was and then write it and then try it manually again …
This way you slowly start to see the way, in which the MCU/CPU see your code and why it behaves like it does. (And what to do, to force it to fullfill you expectations, not just what you wrote.)
@gilhad is exzctly right. Many times I have told ppl to read. The. Code.
At this level, the nice thing is that the sketch is very probably doing what you wrote, even if it isn't yet what you meant.
I'm not saying @riverrunner06 is throwing spaghetti, but we see beginners do that, less than mindfully moving things around or changing if to while, or more amusingly rewriting for statements using well, you get the point or you don't.
It's hard when the code is on the screen, but many times I have told ppl to out a finger on the code.
For fun and just out of curiosity I searched my posts…
put your finger on the code
and read it to see how it works. I played your game
in your loop () function, you will see it illuminat
and see where it happens. a7
of each in turn, run it for for yourself and see ho
and think about what it does step by step. a7
and watch as you "execute" it by hand and see
and trace it step by step. With the update
and see what it is doing line by line. There is no
and march yourself through it 1000000000 times slow
the first while statement waits for the command to fi
and follow through a call to playAudio (). a7
and "execute" it step by step to see the dance whic
and walk yourself through it. If you haven't come t
and see that a character is never read until there is a c
and pretend to be the microprocessor. Assuming noth
and trace the flow. Either by yourself, or let the
and trace it through playing dumb microprocessor
and see what happens, I think you've not got the pa
and step it through line by line, pretending to be
and see why it does what when, call back functions
and follow along like you are really dumb, just doi
of either, they will function identically. arpa123: there is be
and pretend to be the processor, you will quickly s
where it starts, and let it go where the code says.
and see why. Note: I could not test this. a7
this does the same thing a bit differently: if (potValue > 5
and assisted by the copious printing you might ins
and think your way through it and find the errors y
you should be able to see how it works. I could say
I can't, just now in transit, but it sounds and looks li
and pretend you are the dumb computer execute the code li
and convince yourself of this if it isn't what you
and pretend to execute it line by line. You will see that it i
and see the tests that never get a chance. a7
and step through every example. HTH but still thin
and literally "execute" it being as dumb as you can
and step through it like you are a dumb computer
and see it doing what you wrote, not what you meant
in @flashko 's solution. You know what all the statements do in
you don't get, and trace it through line by line playin
and trace it step by step like you were a dumb micr
(sorry, mixing metaphors here) to trace the program
and trace it as you read every line. You only save
around your if / else statement(s) to see why they takes the branch
and follow the logic. Add some Serial . print / pr
and step through it like a dumb microprocessor. # d
and step through it line by line until you see how
and read and "execute" it step by tiny step, every
Fingers are good for other things, too…
put your finger on the resistor and see if it's cold or warm or hot.
put your finger on the regulator once in a coffee break or so and see
Ok, after playing with this and seeing multiple ways to do much the same thing, it is becoming clearer. Your example is yet another way.
I am starting to understand what is happening in the code.
It is good practice not to use the Uno/Nano pins 0 or 1 for any code.
On the Uno/Nano, pins 0 and 1 are the Hardware Serial Tx and Rx pins, but are also used to program/upload a sketch to the Uno/Nano. If you run a program using pins 0 or 1, then programming the Uno/Nano will be interfered. You can recover programming the Uno/Nano by using the Reset button.
I like this idea for learning to write programs. Being taught by "copy/paste" I soon used a troubleshooting tool which was a sheet of paper with columns for registers/variables.
When my programs did not work, my brain would fill-in parts that were not working (fooling me), but writing every "1" and "0" on paper, and seeing the columns and rows of number patterns showed me where my thinking was flawed. This mechanical method of computing helped (helps) me.
Yes, you are correct, the Serial Port is not included in my sketch.
To more directly answer your previous question, no. I do not know why the 2 leading LEDs did not light at the beginning of my sketch once I moved to the pins 2 - 9.
I was guessing when I mentioned about Rx & Tx. And nobody has given me anything about the why here.
Oh well...