Loop For for Arduino used as meter light

Continuing the loop FOR on Arduino

In the previous post we saw an example with the FOR loop of Arduino. It termed as a loop default, that always know few turns will give, this is true but not entirely, in the example above use constant at the time of define the loop knew the number of turns that would give not only before run the FOR but during all the program.

In this example we are going to get a measurement of a light sensor and the digital outputs from the 2 to the 13 to create a light meter.

Explaining the program of the FOR loop for Arduino

  1. Configure all the pins as output and so equal to that in the previous example we will use a FOR loop, easier than writing it 12 times.
  2. Get the value of the sensor (is connects to an input analog) the value is understood between 0 to 1024, we have twelve lights that is Iran turning as more luminosity reaches.
  3. We divide the value of the sensor between 85.34 and save it in an int variable, being the maximum value 11, then we add 2 (thus the Rx and Tx pins left free).
  4. We do a FOR loop from 2 to 13 to pay all pins.
  5. We do a FOR loop from 2 to the value from step 3, so that only those pins, turn on us as we see we don’t know a priori the number of turns of the loop, but if we know exactly how many laps you will give when it starts.

Program

int i;
void setup() {}

  for(i=2; i< 14; i++)
  {
    pinMode(i, OUTPUT);
  }
}

void loop() {}
  int valor_luz;
  valor_luz = analogRead (0);
  valor_luz = valor_luz/85.34;
  valor_luz = valor_luz + 2;

  for(i=2; i< 14; i++)
  {
    digitalWrite(i, HIGH);
  }
  for(i=2; i< valor_luz; i++)
  {
    digitalWrite(i, LOW);
  }
}

Connection and prototyping of LEDs

LED meter with FOR loop
LED meter with FOR loop

The color of the LEDs can be modified to taste of the doer.

Video of the operation

Exhibitor Arduino

If you wish to buy an Arduino for this or other projects click here or on the image.

Arduino genuine one
Genuine Arduino

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.