controlling 8 LEDs by analog input of 5V...... orderly

Only because we think of it as voltage it's not a good idea to do that on the micro as well :wink: Just calculate the values from the ADC for the different levels. HINT: you might see something interesting :wink:

And as to the problem, if I only tell you to turn on the light when will you turn off the light?

TIPS

  • DON'T use short variable names! They are there to help you to make the code understandable. pinMode(a, OUTPUT) is as clear pinMode(13, OUTPUT). But pinMode(LedPin, OUTPUT) shows exactly what you try to do.

-Try to pay attention to the layout of the code. (see below how much better it looks)

  • Use smallest possible variable to match what you need

  • Use const for things that don't change on runtime (only change by you changing the code, not because the program runs).

  • And I really really hope you have resistors in series with the leds... ::slight_smile:

const byte  LedPins[] = {0, 1, 2, 3, 4, 5, 6, 7};

void setup()
{
  for (byte i = 0; i < 8; i++) {
    pinMode(a[i], OUTPUT);
  }
  pinMode (A0, INPUT);
}
void loop(){
  float voltage = analogRead(A0) * 0.00489;
  // to turn off LEDs chronologically
  // to turn on LEDs chronologically
  if (voltage >= 0.2 && voltage <= 0.625)
    digitalWrite(a[0], HIGH);
  else if (voltage < 0.2)
    digitalWrite(a[0], LOW);
  else if (voltage >= 0.626 && voltage <= 1.25)
    digitalWrite(a[1], HIGH);
  else if (voltage < 0.65)
    digitalWrite(a[1], LOW);
  else if (voltage >= 1.26 && voltage <= 1.90)
    digitalWrite(a[2], HIGH);
  else if (voltage < 1.26)
    digitalWrite(a[2], LOW);
  else if (voltage >= 1.91 && voltage <= 2.51)
    digitalWrite(a[3], HIGH);
  else if (voltage < 1, 91)
    digitalWrite(a[3], LOW);
  else if (voltage >= 2.52 && voltage <= 3.13)
    digitalWrite(a[4], HIGH);
  else if (voltage < 2.52)
    digitalWrite(a[4], LOW);
  else if (voltage >= 3.14 && voltage <= 3.74)
    digitalWrite(a[5], HIGH);
  else if (voltage < 3.13)
    digitalWrite(a[5], LOW);
  else if (voltage >= 3.75 && voltage <= 4.36)
    digitalWrite(a[6], HIGH);
  else if (voltage < 3.74)
    digitalWrite(a[6], LOW);
  else if (voltage >= 4.37 && voltage <= 5)
    digitalWrite(a[7], HIGH);
  else if (voltage < 4.37)
    digitalWrite(a[7], LOW);
}