# Dumbest guy in class seeks help understanding code

Near absolute noob here and confirmed dumbest bastard in my class. This project has already been submitted (I used another method to address this requirement) so I am not seeking someone to complete my homework assignment - I just don't understand my instructor's approach to this problem (and yes, I've asked him a lot of questions on it but do not fully understand his responses). I'm hoping other experts' explanations may help me understand the overall approach.

The requirement:

Connect 3 LEDs on 3 digital pins
– 1 at 1 Hz
– 2 at 4 Hz
– 3 at 5 Hz
– 1 Hz
– 3 Hz
– 5 Hz

Instructor's code to address the requirement:

const int LED_1 = 11; //LED connected to digital pin 11
const int LED_2 = 12; //LED connected to digital pin 12
const int LED_3 = 13; //LED connected to digital pin 13

int freqX = 1;
void setup()
{
pinMode(LED_1,OUTPUT); //sets the digital pin as output
pinMode(LED_2,OUTPUT); //sets the digital pin as output
pinMode(LED_3,OUTPUT); //sets the digital pin as output
freqX = 1;

}
//the loop function runs over and over again
void loop() {
if(freqX%4 == 0)digitalWrite(LED_1,HIGH); //turns LED_1 on
if(freqX%2 == 0)digitalWrite(LED_2,HIGH); //turns LED_2 on
digitalWrite(LED_3,HIGH);

if(freqX%8 == 0)digitalWrite(LED_1,LOW); //turns LED_1 off
if(freqX%4 == 0)digitalWrite(LED_2,LOW); //turns LED_2 off
if(freqX%2 == 0)digitalWrite(LED_3,LOW); //turns LED_3 off
delay(200); //waits for a 200 ms
Serial.println("test");
freqX++;
if(freqX == 50) freqX = 1;
}

My questions:

1. why use 8, 4, 2 as divisors in the modulus test?

2. how do the divisors, delay and number of counter cycles interact to generate the required frequencies – what are the relationships among these parameters?

3. why the line of code digitalWrite(LED_3,HIGH); does not require an if statement and applies no divisor?

I have no issues with hardware; just seeking understanding of this code. Thanks in advance.

1. why does an instructor use delay()?

2. why don’t we see the serial prints?

3. Why don’t we trust crt0?

The instructor is basically make each trip through the loop be 200 msec (using delay) which translates into 5 Hz.

Since led3 is supposed to blink at 5 Hz, it get turned on every time through the loop
Led2 is half that so %2 (2.5 Hz)
led1 is 1/4 that so %4 (5/4 Hz)

Then at double that count, the leds are turned off

Not sure why FreqX needs to count up to 50 before resetting, and it also glitches the cadence of the blinks

I'd get a new teacher.

You could impress your instructor, and teach him/her to write better code, by mastering the Blink Without Delay example. Demonstrate how to blink 3 LEDs with arbitrary frequencies!

Tutorial here Blink without delay() explained line-by-line

"Near absolute noob here and confirmed dumbest bastard in my class. This project has already been submitted (I used another method to address this requirement) so I am not seeking someone to complete my homework assignment - I just don't understand my instructor's approach to this problem (and yes, I've asked him a lot of questions on it but do not fully understand his responses). "

Don't blame the instructor for a project you submitted. Just saying.

Thanks - just to be clear, I submitted my project using a coding approach that used if statement and the milli function. The attached code is Instructor's input to class. I don't blame him for anything (well, maybe less than excellent comm skills)

raztuz:
Near absolute noob here and confirmed dumbest bastard in my class.

Clearly not the case. Very much the opposite in fact, as we have not seen (unless posted to other topic boards which I have missed) any of your classmates posting here. I suspect that makes you the smartest by default.

raztuz:
This project has already been submitted (I used another method to address this requirement)

So you should post that code here for comparison because ...

raztuz:
The requirement:
Connect 3 LEDs on 3 digital pins
– 1 at 1 Hz
– 2 at 4 Hz
– 3 at 5 Hz
– 1 Hz
– 3 Hz
– 5 Hz

Fair enough. The thing is, the code you cite from the instructor does not in any way, shape or form, comply with either of these specifications.

raztuz:
My questions:

1. why use 8, 4, 2 as divisors in the modulus test?

That would be appropriate to making the LEDs blink in a binary sequence. Which is again, not what was asked for.

raztuz:
2) how do the divisors, delay and number of counter cycles interact to generate the required frequencies – what are the relationships among these parameters?

In this case, they do not generate the required frequencies. Tough!

raztuz:
3) why the line of code digitalWrite(LED_3,HIGH); does not require an if statement and applies no divisor?

I have no idea. I doubt anyone else does either.

TheMemberFormerlyKnownAsAWOL:
4) why does an instructor use delay()?

It is the simplest - and done properly, perfectly effective - way to address the present limited problem.

blh64:
I'd get a new teacher.

There is much to be said for that recommendation!

I would have done it somewhat differently. But never mind.

"Clearly not the case. "

Well, I'd hold back on that. Why didn't he/she read the ""How to use this forum - please read."" at the top of the discussion list? Just seems like he/she bit off more than they can chew in their project and are scapegoating the instructor. He/she now has you to save the day.

zoomkat:
Why didn’t he/she read the “How to use this forum - please read.” at the top of the discussion list?

To what effect? To correctly cite the code? Yes, I agree on that.

zoomkat:
Just seems like he/she bit off more than they can chew in their project and are scapegoating the instructor.

Not sure what you are saying here.

raztuz has not made it quite clear whether his code was accepted as correct and performed the task correctly, For that reason he most certainly should post his code here for review by the experts.

My reading of the instructor’s code is that it is rubbish - it does not perform the task correctly, so he has an entirely valid beef. Do we agree on that or not - did you examine that code?

OK, first things first.

You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the “More → Modify” option below the right hand corner of your post - to mark up your code as such using the “</>” icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE itself has a “copy for forum” link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don’t forget to use the “Auto-Format” (Ctrl-T) option first to make it easy to read. If you do not post it as “code” it can easily be quite garbled and is always more difficult to read due to the font.

It is inappropriate to attach it as a “.ino” file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.

"My reading of the instructor's code is that it is rubbish - it does not perform the task correctly, so he has an entirely valid beef. Do we agree on that or not - did you examine that code?"

I can't speak as to the instructor (so what class does he teach?), but the OP used the sympathy line "Near absolute noob here and confirmed dumbest bastard in my class. " to apparently suck you/somebody in on doing his homework. The OP has no code of his own for a project he chose, so you are burning daylight getting it done. I think there is an example that blinks at 1 HZ, so I think the OP could start with that and get partial credit. Just saying...

We shall see.

Starting with his code.

My take was that this was homework-part 2
The instructor gave some code as part of a lesson.
With an assignment of for the student to figure it out.

Reverse engineering is one way to learn.

The Professor, giving a student an assignment and then, not explaining it, is really good teaching.
Figuring out something so simple as that code, no distractions, a simple function is a way to learn.

Get rid of the bits not concerned and you get this simple to understand section.

``````void loop() {
digitalWrite(LED_3,HIGH);
if(freqX%2 == 0)digitalWrite(LED_3,LOW);   //turns LED_3 off
delay(200);                                   //waits for a 200 ms
freqX++;
}
``````

Very clear and very simple to understand.

I kinda wish that professors would pop in now and again and give points to whomever figured out the assignment.

As for teaching, this bit of code may have come from an advanced class with instructions for the same timing, but said you are not allowed to use millis(), or more than 1 delay()

Thanks for all responses. Special thanks to blh64 and dave-in-nj - your comments helped me to gain an improved understanding of the included code (and dave-in-nj, your narrative comments precisely captured my situation).

Wow - I anticipated some, but not quite this level of blowback and questioning of my motives. Clearly I have violated some cultural norms here - tt’s been quite an introduction to this forum.

dave-in-nj:
Get rid of the bits not concerned and you get this simple to understand section.

``````void loop() {
``````

digitalWrite(LED_3,HIGH);
if(freqX%2 == 0)digitalWrite(LED_3,LOW);   //turns LED_3 off
delay(200);                                   //waits for a 200 ms
freqX++;
}

``````

Very clear and very simple to understand.
``````

OK, so this piece of code generates a 2.5 Hz blink, which is not as I understand it, what was specified.

So, raztuz, do you propose to reveal your version of the code as per the forum instructions?

I won't be revealing my code - I see no positive result from that. As you may recall from my original post, my objective was to understand the included code - not ask for a critique of my solution that I know works.

Plus .. . although it appears that it would great fun for some here to bash/impugn my motives further, I don't find it much fun at all. This neighborhood is a little rough for me.

EOM

one of the things that we frown on is doing peoples homework.
at the end of the course, you are given the grade based on your work, not ours.

also, depending on the grades, one expects the student be get employed and said employer expects the new hire to master the craft.

when we do the homework, it does not really help the student in any meaningful way.

as I implied earlier, doing the reverse engineering can be enlightening and can give someone the insight into what others have done. IMHO, that can be more of a teaching moment than a textbook.

the response from those on the forum vary quite a bit. some take great offense at homework being posted.

=============

OK, so this piece of code generates a 2.5 Hz blink, which is not as I understand it, what was specified.

as I see this, LED3 goes OFF with every second pass through the loop.
it goes ON for the remaining loops, thereby creating a square wave of some time.

in post #2 , blh64 said

The instructor is basically make each trip through the loop be 200 msec (using delay) which translates into 5 Hz.

regardless of what is expected, if the student were to take the time to run the sketch and time the sequences, he would unravel the mystery of the code.

now, I would have hoped the professor gave an additional instruction.

@raztuz. Can you more precisely specify what aspect of your instructor’s code you do not understand? How does it differ from the code you developed?

steve20016:
@raztuz. Can you more precisely specify what aspect of your instructor’s code you do not understand? How does it differ from the code you developed?

Post #1 is the professors code
with the questions.
He has said a few times that there is no need or want to compare code
and that his code works so is not going to be posted for discussion.
Comparing code is of no importance and that would only confuse the issue.
his code is not part of the assignment.
The assignment was very clear. review the code and answer 3 questions.

"I won't be revealing my code - I see no positive result from that. As you may recall from my original post, my objective was to understand the included code - not ask for a critique of my solution that I know works."

There you have the rest of the story.