Go Down

Topic: Need bit help (Read 380 times) previous topic - next topic

Emp_Aditya

Hey all,,


I am new to Ardiuno (actually new to programming too :smiley-wink:  :smiley-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 :D  :D


I already mention all details in program comments section
 

PaulS

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.
The art of getting good answers lies in asking good questions.

robtillaart

#2
Jan 03, 2018, 01:13 pm Last Edit: Jan 03, 2018, 01:13 pm by robtillaart
a 1 minute clean up
Code: [Select]
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 ?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Emp_Aditya


Go Up