/************************************
   name:Photoresistor
   function:  if you shine the photoresistor with a certain light intensity, you will see several LEDs light up.
   If you increase the light intensity, you will see more LEDs light up.
   When you place it in dark environment, all the LEDs will go out.
 *********************************************/
//Email: [email protected]
//Website: www.sunfounder.com

const int NbrLEDs = 8;//8 leds
const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9};//8 leds attach to pin 2-9 respectively
const int photocellPin = A0;  //photoresistor attach to A0
int sensorValue = 0;        // value read from the sensor
int ledLevel = 0;           // sensor value converted into LED 'bars'

void setup()
{
  Serial.begin(9600);  // start serial port at 9600 bps:
  for (int led = 0; led < NbrLEDs; led++)
  {
    pinMode(ledPins[led], OUTPUT);// make all the LED pins outputs
  }
}

void loop()
{
  sensorValue = analogRead(photocellPin); //read the value of A0
  // Serial.print("SensorValue: ");
  // Serial.println(sensorValue); //Print the analog value of the photoresistor
  ledLevel = map(sensorValue, 0, 1023, 0, NbrLEDs);  // map to the number of LEDs
  // Serial.print("ledLevel: ");
  // Serial.println(ledLevel);
  for (int led = 0; led < NbrLEDs; led++)
  {
    if (led <= ledLevel ) //When led is smaller than ledLevel, run the following code.
    {
      digitalWrite(ledPins[led], HIGH);     // turn on pins less than the level
    }
    else
    {
      digitalWrite(ledPins[led], LOW);     // turn off pins higher than
    }
  }
}
$abcdeabcde151015202530354045505560fghijfghij