Arduino Forum

Community => Gigs and Collaborations => Topic started by: Emp_Aditya on Jan 02, 2018, 04:51 pm

Title: Need bit help
Post by: Emp_Aditya on Jan 02, 2018, 04:51 pm
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
 
Title: Re: Need bit help
Post by: PaulS on Jan 02, 2018, 05:42 pm
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.
Title: Re: Need bit help
Post by: robtillaart on Jan 03, 2018, 01:13 pm
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 ?
Title: Re: Need bit help
Post by: Emp_Aditya on Jan 03, 2018, 02:42 pm
thank you all