Go Down

Topic: controlling 8 LEDs by analog input of 5V...... orderly  (Read 738 times) previous topic - next topic


i was given the home assignment of using a 5V volts supply by controlling it with potentiometer at analog pin A0, and by setting different levels for 8 LEDs connected at 0-7 orderly.... suppose 0.2V-0.6V for LED 1 then 0.7-1.3V for Led 2 and so on... I had successively Lead all Leds on from 0V to 5V but it does not turn off chronologically as they were turning on....i have written the program for it.... and trying simulation in proteus.... help me out.....

I am attaching code and circuit........


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

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

- 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... ::)

Code: [Select]
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);
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake


LedPins[] = {0, 1, 2, 3,....

Assuming you're using an Uno, then you shouldn't use pin 0 and 1.
They are the TX/RX pins, and already used by the USB<>Serial chip.


When you do it for real use resistors in line with each LED.

Go Up