Controlling Charlieplexed LEDs with sensors, code advice needed!

Hi guys,

I would just like to say that I am in awe of all the amazing things that can be achieved with arduino and a good set of brains, I appear to be lacking in the latter!

Here is my problem:

I have managed to successfully wire 20 LEDs to 5 pins of my arduino (To give you an idea of my skill set - this took about 8 hours of serious concentration on a wiring diagram!) and am running a code I found on an instructable about charlieplexing which turns each one on in order, and they are working beautifully (to give myself a pat on the back!)

Now I have mastered the practical side of this little project, I obviously need to master the code. Boy am I struggling with this. ALL I need to do is to include a line in my code somewhere which tells all of the LEDs to turn off when the light levels are high (I have a PIR sensor with its own LEDs which will light up and trigger my LDR when movement is sensed) - so I’m kind of designing the opposite to a PIR controlled security light.

Here is the code I am running through each Charlieplexed LED, and I know that the first rule of asking for help on forums is that you must have tried your hardest to write a successful code yourself before asking for help, but I honestly don’t know where to start! I have written codes to turn one LED on and off using a LDR but I don’t know whereabouts in this code to input the information from that code because this appears to be made of three separate arrays? " int c[5][4][2] "?

If anyone could tell me the kind of code I would need to write to say “ALL LEDs should be on until the light level on the LDR is +500 then they should ALL turn off” that would be great.

Sorry if this is an irritating post, but if those of you with any insight in to this issue could shed some light on it then I will send you a virtual mulled wine at this seasonable and charitable time of year!

Thanks so much.

#define A 8
#define B 9
#define C 10
#define D 11
#define E 12
#define sensor 1

#define DELAY 500

int c[5][4][2] =
{
  { {A, B}, {A, C}, {A, D}, {A, E} },
  { {B, A}, {B, C}, {B, D}, {B, E} },
  { {C, A}, {C, B}, {C, D}, {C, E} },
  { {D, A}, {D, B}, {D, C}, {D, E} },
  { {E, A}, {E, B}, {E, C}, {E, D} }
};


void setup()
{
  pinMode( A, INPUT );
  pinMode( B, INPUT );
  pinMode( C, INPUT );
  pinMode( D, INPUT );
  pinMode( E, INPUT );
}

void display( int frame[5], int duration )
{
  int times = 0;
  int x = 0;
  int y = 0;

  while( times < duration )
  {

  for( y = 0; y < 5; y++ )  
  {
    for( x = 0; x < 4; x++ )
    {
      setup();
      if( frame[y] & (0b100 >> x) )
      {
        light( c[y][x] );
        delayMicroseconds(DELAY);
        times++;
      }
    }
  }

  }
}
void light( int pins[2] )
{
  pinMode( pins[0], OUTPUT );
  digitalWrite( pins[0], HIGH );

  pinMode( pins[1], OUTPUT );
  digitalWrite( pins[1], LOW );
}

void test( int pins[2] )
{
  setup();
  light(pins);
  delay(500);
}

void test_loop()
{
  for( int i=0; i<5; i++ ){
    for( int j=0; j<4; j++ ){
      test(c[i][j]);
    }
  }
}

void loop()
{
  test_loop();
}

To use Charlieplexing you have to set the unused lines to INPUT so they don’t supply spurious Ground (LOW) signals. Yes, this means you have to do digitalWrite(pin, LOW) and pinMode(pin, INPUT) to ALL of the pins before you set one to digitalWrite(pin, HIGH) and pinMode(pin, OUTPUT) and another to digitalWrite(pin, LOW) and pinMode(pin, OUTPUT).