Photocell program

Hey, everyone. Lately I've been working on the photocell beginner lesson that came with the arduino uno. I am having a problem getting it to actually work though. I wired the LED to precisely what the program says to and I tried to make it less complicated by changing from 8 LED's to 1. So here's the code.

/*
  Bargraph sketch

 Turns on a series of LEDs proportional to a value of an analog sensor.
 Six LEDs are controlled but you can change the number of LEDs by changing
 the value of NbrLEDs and adding the pins to the ledPins array
 */

const int NbrLEDs = 1;
const int ledPins[] = { 5};
const int photocellPin = A0; 
                             
const int wait = 30;

// Swap values of the following two constants if cathodes are connected to Gnd
const boolean LED_ON = HIGH;
const boolean LED_OFF = LOW;

int sensorValue = 0;        // value read from the sensor
int ledLevel = 0;           // sensor value converted into LED 'bars'

void setup() {
  for (int led = 0; led < NbrLEDs; led++)
  {
    pinMode(ledPins[led], OUTPUT);  // make all the LED pins outputs
  }
}

void loop() {
  //sensorValue = analogRead(analogInPin);             // read the analog in value
  sensorValue = analogRead(photocellPin);
  ledLevel = map(sensorValue, 300, 500, 0, NbrLEDs);  // map to the number of LEDs
  for (int led = 0; led < NbrLEDs; led++)
  {
    if (led < ledLevel ) {
      digitalWrite(ledPins[led], LED_ON);     // turn on pins less than the level
    }
    else {
      digitalWrite(ledPins[led], LED_OFF);      // turn off pins higher than 
                                                // the level
    }
  }
}

Is this code over complicated at all? I tried rewriting it and simplifying it, but I also don't really understand the photocell. With the program like this, everything wired to the board the LED stays on and even with the photocell covered nothing happens. What would be my first step to solving this problem? Thanks guys.

  ledLevel = map(sensorValue, 300, 500, 0, NbrLEDs);  // map to the number of LEDs

This is going to map some unknown value, expected to be in the range 300 to 500 to the range 0 to 1. Any value below 500 is going to map to 0 or less. Any value of 500 or more is going to map to 1 or more.

Printing sensorValue would be a good idea. Perhaps the value is not what you think it is.
Understanding what map() does would be a good idea. Perhaps the output is not what you think it is.

PaulS:

  ledLevel = map(sensorValue, 300, 500, 0, NbrLEDs);  // map to the number of LEDs

This is going to map some unknown value, expected to be in the range 300 to 500 to the range 0 to 1. Any value below 500 is going to map to 0 or less. Any value of 500 or more is going to map to 1 or more.

Printing sensorValue would be a good idea. Perhaps the value is not what you think it is.
Understanding what map() does would be a good idea. Perhaps the output is not what you think it is.

Sorry, PaulS I'm completely stumped :frowning: After reading map() a few times I still don't understand it. Why is it called map? That might help it click for me. How would I print sensorValue?

Why is it called map?

Because mapAValueFromOneRangeToAnother() was considered too long.

Suppose you have a sensor that outputs data in one range (0 to 1023, for instance), and you need it in another range (0 to 180, for a servo, or 0 to numLeds for a light bar). How would you go about mapping the value from one range to the other?

How would I print sensorValue?

Serial.print("sensorValue: ");
Serial.println(sensorValue);

(Of course, you'll need to add a Serial.begin() call to setup(), and open the Serial Monitor.)