I need to understand a short code in order to pass IT this year can any1 help me

This is the code, I need to be able to explain what each line of it does.

int ledPins[6] = {2, 3, 4, 5, 6, 7};

void setup()
{
 for(int i = 0; i < 6; i++)
 {
   pinMode(ledPins[i], OUTPUT);
 }
}

void loop()
{
 for(int i = 0; i < 6; i++)
 {
   digitalWrite(ledPins[i], HIGH);
   while(digitalRead(7) == 0)
   {
   }
   delay(800);
   digitalWrite(ledPins[i], LOW);
 }
}

(Code tags added, use the </> button on the menu next time - Thanks. Moderator)

A starting point - Arduino Reference - Arduino Reference

Please remember to use code tags when posting code

What do you understand about how the code operates?

Do you understand int ledPins[6] = {2, 3, 4, 5, 6, 7};
that line of code? Can you describe what that line of code is doing?

In case you haven't figured it out, you need to add [x] to the array callout where it is used in setup() and loop().

pinMode(ledPins[i], OUTPUT);

digitalWrite(ledPins[i], HIGH);


Probably need to this to setup() as well:


pinMode (7, INPUT_PULLUP);
to enable the pullup resistor on the pin, then use a button to connect the pin to Gnd when it is pressed.

CrossRoads:

In case you haven't figured it out, you need to add [x] to the array callout where it is used in setup() and loop().

pinMode(ledPins[x], OUTPUT);

digitalWrite(ledPins[x], HIGH);

Probably need to this to setup() as well:

pinMode (7, INPUT_PULLUP);
to enable the pullup resistor on the pin, then use a button to connect the pin to Gnd when it is pressed.

All that may magically happen if he posted using code tags
(Yup, that worked - Moderator)

Against each line write comments containing what you understand now and then post again. Then we can help with any lines you might have difficulty with.

If you really don't understand any of it then you are not going to pass IT and you should not pass.

Steve

Normally, when someone is up-front that they are asking a school related question, I am willing to offer guidance. But, I won't do the work for anyone who makes it obvious that they haven't performed even the slightest bit of reading or research into the subject. Do you even have the Arduino IDE installed? Do you even have an Arduino board?

See reply #1.

You can easily read up what each of these lines does - if someone here does that for you , you will be back next year with further questions when you next get asked about a code.

You need to learn the basics of coding , and how to research it to get answers . Not being mean , this really is for your benefit.
The object of being asked the question by the course is that by answering it you have gained the knowledge , by asking someone else to do it , then you are not proving you have learnt anything.

Top marks for fessing up it’s for school work tho .

I'll help. The line that reads

while(digitalRead(7) == 0)

will bite you in the butt.

Seriously though, if at the end of the year you can't explain a single line of that code, please don't apply for any jobs in my IT department. If you do understand at least some of that code, and are motivated/interested enough to post it here, I (and many others here) will be happy to tell you what may be wrong and/or give you tips on what would be right.

arduinonewbie123:
This is the code, I need to be able to explain what each line of it does.

Why don't you start by trying on your own and showing us what you think each line does? Then we can correct you if necessary...

Four days now...

I call the thread dead. :roll_eyes: