I have a problem with my code, can u help me?

I want 6 LED’s to form a night light. So when the light goes off, they have to fade in. When the light goes on, the have to fade out.

This is the code I used:

int ledPins = {3, 5, 6, 9, 10, 11}; // LED connected to digital pin 3, 5, 6, 9, 10, 11
int pcellPin = 2; // Photocell connected to analog pin 2
int pcellVal = 0; // define photocell variable
int ledLit = 0; // define boolean

void setup(){
pinMode(ledPins, OUTPUT); // sets the digital pin as output
pinMode(pcellVal, INPUT); // sets the analog pin as input
}

void loop(){
pcellVal = analogRead(pcellPin); // get value from photocell

// Serial.begin(9600); // print values to serial for troubleshooting
// Serial.println(pcellVal);

if (pcellVal < 100 and ledLit == 0){ // check if ambient lights are off and LED not lit
fadeLed(1);} // if so, turn the LED on
else if (pcellVal > 100 and ledLit == 1){ // check if ambient lights are on and LED lit
fadeLed(0);} // if so, turn the LED off
}

void fadeLed(int x){ // fade function
if (x == 1){ // fade LED in
for (int i=0; i <= 255; i++){
analogWrite(ledPins, i);
delay(20);}
ledLit = 1;} // LED status = ON
else{ // fade LED out
for (int i=254; i >= 0; i–){
analogWrite(ledPins, i);
delay(10);}
ledLit = 0;} // LED status = OFF
}

An error says:
LDR.cpp: In function ‘void setup()’:
LDR:7: error: invalid conversion from ‘int*’ to ‘uint8_t’
LDR:7: error: initializing argument 1 of ‘void pinMode(uint8_t, uint8_t)’
LDR.cpp: In function ‘void fadeLed(int)’:
LDR:26: error: invalid conversion from ‘int*’ to ‘uint8_t’
LDR:26: error: initializing argument 1 of ‘void analogWrite(uint8_t, int)’
LDR:31: error: invalid conversion from ‘int*’ to ‘uint8_t’
LDR:31: error: initializing argument 1 of ‘void analogWrite(uint8_t, int)’

Welcome Sophieke,

First, please use the # button to get [ code] tags around your code (preview shows if you did it right

second you need to reread the chapters about using arrays:

fixed your code so it compiles (but I don’t know if it does what you want :slight_smile:
removed some obvious comments

//
//    FILE: test.pde
//  AUTHOR: Sophieke de Voogt
//    DATE: 20-OCT-2012
//
// PUPROSE:
//

int ledPins[] = { 3, 5, 6, 9, 10, 11};           // LED connected to digital pin 3, 5, 6, 9, 10, 11
int pcellPin = 2;                                 // Photocell connected to analog pin 2
int pcellVal = 0;                                 // define photocell variable
int ledLit = 0;                                   // define boolean

void setup()
{
  for (int i=0; i<6; i++) pinMode(ledPins[i], OUTPUT); 
  pinMode(pcellVal, INPUT); 
}

void loop()
{
  pcellVal = analogRead(pcellPin);                  // get value from photocell

  // Serial.begin(9600);                            // print values to serial for troubleshooting
  // Serial.println(pcellVal);

  if ((pcellVal < 100) && (ledLit == 0)){              // check if ambient lights are off and LED not lit
    fadeLed(1);
  }                                  // if so, turn the LED on                     
  else if ((pcellVal > 100) && (ledLit == 1)){         // check if ambient lights are on and LED lit
    fadeLed(0);
  }                                  // if so, turn the LED off
}

void fadeLed(int x){                              // fade function
  if (x == 1){                                      // fade LED in
    for (int i=0; i <= 255; i++){
      analogWrite(ledPins[0], i);
      analogWrite(ledPins[1], 255-i);
      delay(20);
    }
    ledLit = 1;
  }                                   // LED status = ON 
  else{                                             // fade LED out
    for (int i=254; i >= 0; i--){
      analogWrite(ledPins[0], i);
      analogWrite(ledPins[1], 255-i);
      delay(10);
    }
    ledLit = 0;
  }                                   // LED status = OFF
}

I’m sorry, I have no Idea how to reply on these forums, I just don’t know what to do anymore. I have this schoolproject on monday…
And I don’t even know how to use an Arduino… I hope you can help me further. I tried your code, and indeed it doenst show any Error’s anymore.
Still it doesn’t do what I want.

I know how to do it with one LED, but I just can’t seem to run it with 6 of them…
Can you please help e just a little further?
This is my breadboard lay-out:

ldrMultiple.png

JAA je komt uit nederland, mooi. dan kan ik gewoon nederlands praten

(indeed from NL, but please keep the language English on the international section. Note => there is a dutch subsection)

You have to adjust the fadeLEd function so it sets all the leds, now it only sets LED 0,
You need to insert a loop that sets all pins like this.

      for (int p=0; p<6; p++) analogWrite(ledPins[p], i);

Some tips to improve the style and code:

  • remove comments that are obvious from the code (your teacher might disagree)
    use comments to explain the WHAT or WHY of the application not the HOW → the code does this)

  • rename variables to names that are meaningful. e.g. use intensity instead of i => makes the code more readable (like proze :wink:
    (this helps to remove comments that explain that i is intensity :slight_smile:

  • the arduino language (C++) supports boolean types so instead of using an int as boolean you should use a real bool at some places

  • split the fadeLed in two functions fadeOn(int val) and a fadeOff().
    FadeOn(int val) could fade until val is reached, which is not necessary 255, and fadeOff() would start with that value to zero
    The value given to fadeOn() would depend on the value read from the photocell

Hopes this helps,
PS, which school are you from (just to know where they use arduino in education)?

(I go to school in Utrecht, at the HKU (Hogeschool voor de Kunsten Utrecht))

okay, thankyou for helping me!
but, I still have no idea what to do… My problem is: I don’t speak ‘arduino’.
I got this code from the web, and I don’t even know what everything means

can you please help me writing the code, step for step, in easy language…
I’m really starting to panic a little, because of my deadline (which is monday, but I have to finish it today)

I tried to apply your advise in my code, but I have no idea what I’m doing, and ofcourse nothing works…

Those are some of the examples I used:
http://www.hobbytronics.co.uk/arduino-tutorial8-nightlight

http://arduino.cc/forum/index.php/topic,17133.0.html

Added some of my tips to the code.
The homework left for you is to read the code until you understand what happens and add meaningful comments where needed.
(the code should compile)

//
//    FILE: test.pde
//  AUTHOR: Sophieke de Voogt
//    DATE: 20-OCT-2012
//
// PUPROSE: ambilight demo
//
int ledPins[] = {3, 5, 6, 9, 10, 11}; 
int PhotocellPin = 2;
int PhotocellValue = 0;
boolean ledLit = false;

void setup()
{
  for (int pin = 0; pin < 6; pin++) 
  {
    pinMode(ledPins[pin], OUTPUT); 
  }
}

void loop()
{
  PhotocellValue = analogRead(PhotocellPin);              
  // Debug output
  // Serial.begin(9600); 
  // Serial.println(PhotocellValue);

  if ((PhotocellValue < 100) && (ledLit == false)){              
    fadeON();
  }
  else if ((PhotocellValue >= 100) && (ledLit == true)){         
    fadeOFF();
  }  
}

void fadeON()
{
  for (int pin = 0; pin < 6; pin++)
  {
    for (int brightness = 0; brightness < 255; brightness++)
    {
      analogWrite(ledPins[pin], brightness);
    }
    delay(4);
  }
  ledLit = true;
}

void fadeOFF()
{
  for (int pin = 0; pin < 6; pin++)
  {
    for (int brightness =255; brightness > 0; brightness--) // decreasing brightness
    {
      analogWrite(ledPins[pin], brightness);  
    }
    delay(2);
  }
  ledLit = false;
}