Need bit help

Hey all,,

I am new to Ardiuno (actually new to programming too :wink: :wink: )

I created a Arduino Program which actually worked but the program look very lenthy and bit complicated to do a simple task (just to turn ON and OFF LEDs).

I want to use IF and FOR in my program which made my program bit complicated.

if someone can simpify it for me pls do :smiley: :smiley:

I already mention all details in program comments section

If_For_voltMetre_loop.ino (1.61 KB)

NOTHING follows a { on the same line.

For loop variables should NOT be global.

What should the program do if voltMetre IS 511?

Use Tools + Auto Format to format your code before posting code.

Other than that, there is nothing wrong with your program, if it does what you want.

a 1 minute clean up

int ledSet1;
int ledSet2;

void setup()
{
  for (ledSet1 = 2; ledSet1 < 5; ledSet1++)
  {
    pinMode(ledSet1, OUTPUT);
  }
  for (ledSet2 = 5; ledSet2 < 8; ledSet2++)
  {
    pinMode(ledSet2,OUTPUT);
  }
}

void loop()
{
  int voltMetre = analogRead(A0);
  if (voltMetre < 511)
  {
    for (ledSet1 = 2; ledSet1 < 5; ledSet1++)
    {
      digitalWrite(ledSet1, HIGH);
    }
    for (ledSet2 = 5; ledSet2 < 8; ledSet2++)
    {
      digitalWrite(ledSet2, LOW);
    }
  }
  if (voltMetre > 511)
  {
    for (ledSet1 = 2; ledSet1 < 5; ledSet1++)
    {
      digitalWrite(ledSet1, LOW);
    }
    for (ledSet2 = 5; ledSet2 < 8; ledSet2++)
    {
      digitalWrite(ledSet2, HIGH);
    }
  }
}

Only add comments when they explain the why of the code.
"repeating" the code statements in text adds very little value.

using spaces between keywords and operators makes code much more readable
and they do not slow the code down runtime

From the cleanup I see that there is no action for the value 511 which is by design or ?

thank you all