Seeking advice on photoresistor led sequence project

Hello everyone.

I recently created the circ-02 project from a tutorial which is basically an 8 led sequencer triggered by pins. Here is a link for more details: http://oomlout.com/a/products/ardx/circ-02/

I thought it would be a great idea to have it turn on when I turned the lights off in my room. After a little research I found the circ-09 tutorial [ http://oomlout.com/a/products/ardx/circ-02/ ] about implementing a photo-resistor so I did the tutorial implementing the 9th pin on the 8 led sequencer.

Both work perfectly on their own when loaded but I am looking to make a hybrid of the two so I can have a sweet night light.

My questions are:

1: Would one photo-resistor work for all 8 leds or would each individual led need its own?

  1. Would the sequencer code and photo-resistor code be compatible running along side with one another when both influence HIGH and LOW? and if yes what would be the best approach?

Codes and photos are posted below. All input is greatly appreciated and please forgive me if this is a no-brainier question ( I am very new to working with arduino).

Circ-02 code:

int ledPins = {2,3,4,5,6,7,8,9};

void setup()
{

for(int i = 0; i < 8; i++){
pinMode(ledPins*,OUTPUT);*

  • } *
    }

*void loop() *
{

  • inAndOut(); *
    }

void inAndOut(){

  • int delayTime = 100;*

  • for(int i = 0; i <= 3; i++){*

  • int offLED = i - 1;*

  • if(i == 0) { *

  • offLED = 3; *

  • } *

  • int onLED1 = 3 - i; *

  • int onLED2 = 4 + i; *

  • int offLED1 = 3 - offLED;*

  • int offLED2 = 4 + offLED;*

  • digitalWrite(ledPins[onLED1], HIGH);*

  • digitalWrite(ledPins[onLED2], HIGH); *

  • digitalWrite(ledPins[offLED1], LOW); *

  • digitalWrite(ledPins[offLED2], LOW); *

  • delay(delayTime);*

  • }*

  • for(int i = 3; i >= 0; i–){*

  • int offLED = i + 1; *

  • if(i == 3) { *

  • offLED = 0; *

  • } *

  • int onLED1 = 3 – i; *

  • int onLED2 = 4 + i; *

  • int offLED1 = 3 – offLED;*

  • int offLED2 = 4 + offLED;*

  • digitalWrite(ledPins[onLED1], HIGH);*

  • digitalWrite(ledPins[onLED2], HIGH); *

  • digitalWrite(ledPins[offLED1], LOW); *

  • digitalWrite(ledPins[offLED2], LOW); *

  • delay(delayTime);*

  • }*
    }
    Circ-09 code:
    int lightPin = 0;
    *int ledPin = 9; *
    void setup()
    {

  • pinMode(ledPin, OUTPUT);*
    }
    void loop()
    {
    int lightLevel = analogRead(lightPin);

lightLevel = map(lightLevel, 0, 900, 0, 255);

lightLevel = constrain(lightLevel, 0, 255);
*analogWrite(ledPin, lightLevel); *
}


1) Please use code tags (# button). 2) Please post small images (i.e. scale them to something reasonable for a forum, like 800px width).

This is wrong:

for(int i = 0; i < 8; i++) {      
    pinMode(ledPins, OUTPUT);
}

You have to access the i-th element of the array:

for(int i = 0; i < 8; i++) {      
    pinMode(ledPins[i], OUTPUT);
}

I think that's more to do with not using code tags, so the [i] gets interpreted as in italic tag.

Hello cisco87,
you can simply trigger the sequence by reading the photoresistor:

void loop()                     
{
  int lightLevel = analogRead(lightPin); 

  // ...

  if( lightLevel < lowThreshold )
  {
     inAndOut();          
  }
  else
  {
     // ...
  }
}

consider that the analog channel is read when the “inAndOut” function completes its execution, once evey 6*100 = 600 millisec.

dxw00d: I think that's more to do with not using code tags, so the [i] gets interpreted as in italic tag.

You're right. I even had that problem while writing the reply :sweat_smile:

That's another good reason to use code tags, btw.

Thank you guys for the advice on the code. Things are looking a bit clearer now with the explanations. Cant wait to get working on it!!! XD