Code to make LEDs turn off in an array as controlled by LDR (& charlieplexing q)

Hi everyone,

Another question to reveal how little I know about Arduino!

I am trying to write a code to make (at the moment) 3 LEDs fade down or simply turn off as controlled by an LDR, but in an array.

So the code I want would be "The LEDs should always be on UNLESS the LDR value falls below 800, then turn off LED 1 then wait half a second, then turn off LED 2, then wait half a second, then turn off LED 3" etc.

I have the code to make my LEDs turn on and off in an array, and I have the code to fade 3 LEDs out when the LDR values fall, I just can't put them together successfully! I really am terrible at this.

My end goal for this project is to wire up as many LEDs as possible (Arduino cookbook says charlieplexing can run 64 LEDs from one arduino?) and then for them all to go out one by one in reaction to the values on an LDR, or an infrared ranger. Do you guys think that is even possible? I'm not sure if you can control individual LEDs when they are stuffed together in Charlieplexing?

Any suggestions on my immediate coding problem and/or my very big final project problem would be infinitely appreciated.

Thank you,

Jo

P.s. here is the code for fading 3 LEDs which I'm trying to add array information to:

{
//analog in - values from light sensor

//variables
#define ledpinA 2 
#define ledpinB 7
#define ledpinC 4
#define sensor 1
int val=0;
int bright=0;


//SETUP
void setup()
{
  pinMode(ledpinA,OUTPUT);
  pinMode(ledpinB,OUTPUT);
  pinMode(ledpinC,OUTPUT);
  Serial.begin(9600);
}

//LOOP
void loop()
{
  val=analogRead(sensor);
  Serial.println(val);
  //values up to 917
  val=constrain(val,0,917);
  bright=map(val,917,0,255,0);
  analogWrite(ledpinA,bright);
  analogWrite(ledpinB,bright);
  analogWrite(ledpinC,bright);
  delay(100);
}
}

You don't say where, how or why you want to incorporate arrays. It would be easier to give advice if you posted a program where you attempted to use arrays and failed together with an explanation of what happens and what should happen when you run it.

As to charlieplexing LEDs from an Arduino, the LOL (Lots of Leds) shield has 126 LEDs in a 14 by 9 matrix all of which can be turned on and off at 8 levels of brightness.

{
//analog in - values from light sensor

I think you haven't tried to compile this (neither have I)

Hi chaps,

Yes I have successfully compiled it, that curly bracket was an accident from where i copied and pasted the wrong code originally, that's not part of the actual script I am running, sorry for the confusion.

I am trying to use an array because in our programming class we were given a code for an array of LEDs which chase each other (I believe this might be similar to the kinghtrider example on arduino but I haven't checked), and I believed this would be the most simple way to control all of them?

To control them by saying "the next one in the array" (would that be [1]+1?) should go off next, instead of individually commanding each LED which would make a lengthy code?

The LOL shield would be a good idea but I have to put these LEDs on a sculpture so they need long wires (a bit like Christmas lights, if each light was programmed to go off sequentially).

Perhaps it would be easier to work the LDR information in to the array script, rather than trying to write arrays in to the LDR script. I will try that instead.

To control them by saying "the next one in the array" (would that be [1]+1?) should go off next, instead of individually commanding each LED which would make a lengthy code?

If the LED pin numbers are in an array then the easiest thing to do is usually to iterate through the array using a for loop using the for loop variable as the index to the array. Alternatively if the delay produce by the for loop is unacceptable use the loop() function and a variable incremented each time through loop() as the index to the array.

As to the LOL shield, I was just using it as an example of how many LEDs could be controlled individually from a Uno not as a suggested solution to your project.

Thanks for your suggestion UKheliBob, I will check it out all the same.

If anyone ever comes here looking for an answer to my question, I have just cracked it, here is the code:

//ROTATING THROUGH LEDS

//VARIABLES
#define sensor 1
const int nLEDs=8;
int ledPins[nLEDs]={2,3,4,5,6,7,8,9};
int val=0;
int level=0;


//SETUP
void setup()
{
  for(int i=0; i<nLEDs;i++)
  {
    pinMode(ledPins[i],OUTPUT);
  }
  Serial.begin(9600);
}

//LOOP
void loop()

{
  val=analogRead(sensor);
  Serial.println(val);
  level=map(val,0,700,0,(nLEDs-1));
  
  for (int i=0; i<level; i++)
  {
    digitalWrite(ledPins[i],HIGH);
  }
    delay(10);
    
    for (int i=0;i<nLEDs; i++)
    {
    digitalWrite(ledPins[i],LOW);
  }
}