20 LEDs all ina row... Charlieplexing style! How to address each??

Uhg.
Think I need a break from this.

charlieMatrix4.cpp: In function 'void loop()':
charlieMatrix4:5: error: ISO C++ forbids comparison between pointer and integer
charlieMatrix4:6: error: invalid types 'int[uint8_t]' for array subscript
charlieMatrix4:7: error: invalid types 'int[uint8_t]' for array subscript
charlieMatrix4:8: error: invalid types 'int[uint8_t]' for array subscript
charlieMatrix4:10: error: invalid types 'int[uint8_t]' for array subscript
charlieMatrix4:11: error: invalid types 'int[uint8_t]' for array subscript
charlieMatrix4:13: error: invalid types 'int[uint8_t]' for array subscript
charlieMatrix4:14: error: invalid types 'int[uint8_t]' for array subscript
charlieMatrix4:16: error: expected `}' at end of input

Would it be easier to just rewire? It is just on a breadboard. Was hoping to understand code. Not sure how to go about understanding the code.

Might just rewire, I like hardware.

But if I just rewire... how does the code address each led?
Does it start at 2 and 6 and increment both equally one at a time?

void setup() {
for (uint8_t i=2;i<=6;i++) {
pinMode(i,INPUT);
digitalWrite(i,LOW);
}
}

void loop() {
uint8_t n = 0;
for (uint8_t i=2;i<=6;i++) {
pinMode(i,OUTPUT);
for (uint8_t j=i+1;j<=6;j++) {
pinMode(j,OUTPUT);
digitalWrite(i,HIGH);
delay(500);
n++;
digitalWrite(i,LOW);
digitalWrite(j,HIGH);
delay(500);
digitalWrite(j,LOW);
n++;
pinMode(j,INPUT);
if (n == 20) {
break;
}
}
pinMode(i,INPUT);
if (n == 20) {
break;
}
}
}

First, some housekeeping: Look back at the code you've posted. I'm not sure what you see, but I see unusual characters replacing important code bits. The forum's software is interpreting some of the characters as special codes that tell it to display strange stuff. Avoid this by putting your code in code tags - use the "#" button.

And 3 high and 2 low for 2? Sweet, if that is it.

I thought you had it there.

Would it be easier to just rewire?

Your call. Do you just want to make this one work, or do you want to learn how to make this kind of stuff work?

... how does the code address each led? Does it start at 2 and 6 and increment both equally one at a time?

No. Work your way through it: i starts at 2, j starts at i+1 = 3; then j goes to 4, then 5, then 6. i goes to 3; j starts at 4, goes to 5, then 6. And so on. Looks like you're not entirely clear on how a loop works.

int NLEDS[20] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
int LEDArray = sizeof(NLEDS)/sizeof(int);

I'm baffled by this. The array is nothing but a sequential list of indexes, and those indexes are really nothing more than the names you gave the LEDs - they're not connected to anything in the circuiting. The int is just 20, but you don't learn anything from it - you already told NLEDS to be 20 elements in size.

digitalWrite(LEDArray[1],HIGH);Here's what's wrong with this: LEDArray is an int, not an array. I'm thinking that this code didn't compile - is that right?

How would you like to proceed? We can post code that will give you the result you want, or we can help you work through it. My instincts say that you're unfamiliar with C, and with programming in general. Maybe we'd serve you best by getting this one working, and letting you get on with learning the language; maybe you'd prefer to work through this as a learning experience. How would you like to proceed?

Place a # before and after? I don't see and unusual characters but will add # around my code.

tmd3:
First, some housekeeping: Look back at the code you've posted. I'm not sure what you see, but I see unusual characters replacing important code bits. The forum's software is interpreting some of the characters as special codes that tell it to display strange stuff. Avoid this by putting your code in code tags - use the "#" button.

I can see how the numbers given can access the leds separately but don't know how to tell it those numbers with out it not working/verifying.

And 3 high and 2 low for 2? Sweet, if that is it.

I thought you had it there.

Would like to make it work and want to figure it out. Telling me how i and j move was helpful!

Would it be easier to just rewire?

Your call. Do you just want to make this one work, or do you want to learn how to make this kind of stuff work?

Not sure on loop, but seeing it put like this helps. Will go stare at it some more and see if I can increment or tell it individually. Trying to figure out how to write it.

... how does the code address each led? Does it start at 2 and 6 and increment both equally one at a time?

No. Work your way through it: i starts at 2, j starts at i+1 = 3; then j goes to 4, then 5, then 6. i goes to 3; j starts at 4, goes to 5, then 6. And so on. Looks like you're not entirely clear on how a loop works.

Trying to grasp how those LEDs are named. Not just the whole array.

int NLEDS[20] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

int LEDArray = sizeof(NLEDS)/sizeof(int);


I'm baffled by this. The array is nothing but a sequential list of indexes, and those indexes are really nothing more than the names you gave the LEDs - they're not connected to anything in the circuiting. The int is just 20, but you don't learn anything from it - you already told NLEDS to be 20 elements in size.

You are correct.

`digitalWrite(LEDArray[1],HIGH);`Here's what's wrong with this: LEDArray is an int, not an array. I'm thinking that this code didn't compile - is that right?

I have the time so would like to try figuring out the C monster that has found me. Will go on learning! Thanks for pointers and advice.

How would you like to proceed? We can post code that will give you the result you want, or we can help you work through it. My instincts say that you're unfamiliar with C, and with programming in general. Maybe we'd serve you best by getting this one working, and letting you get on with learning the language; maybe you'd prefer to work through this as a learning experience. How would you like to proceed?

MasterErik:
Place a # before and after?

No, not that. When you're in the reply window, there's an array of buttons marked with symbols above the text window. One of them is marked with a "#". When you push that button, "code tags" will appear in the text window at the cursor position. Paste your code between them, and your post will show it like this:< some code > ...It'll be marked as code, and everyone's browser will display it literally, and won't treat anything it finds as some sort of special character. If you want to see a special character sequence in action, try putting an "8" in parenthesis, and then push the "Preview" button.

Also, we'll be likely to get more participation if you don't quote entire messages. I'd suggest deleting the parts that you're not directly addressing from your reply. I think that viewers see long stretches of quoted text, think the post is likely to be boring, and move on without reading.

Ah, useful buttons. New concept. Gotcha. Will use the buttons.

Makes just 2 through 6 input.

(uint8_t i=2;i<=6;i++)

This increases j +1 but not over 6.

for (uint8_t j=i+1;j<=6;j++)

Makes uint8_t increase. or the i++ makes i increase and j++ makes j increase.

 n++

Makes it go to 20 integer... i and j go to 20 each. But they are only allowed to go to 6. So that is the loop.
i= 2 3 4 5 6 2 3 4 5 6 2 3 4 5 6 2 3 4 5 6
j= 3 4 5 6 ? 3 4 5 6 ? 3 4 5 6 ? 3 4 5 6 ?
But that makes it do the same over and over again. Hmm.

Ok, glad to be at work. haha

So wrote this and modified x and y to determine the values of the pins to the leds. Wow, what a pretty pattern!

void loop () {
   uint8_t n = 0;
   for (uint8_t a=x;a<=6;) {
     pinMode (a,OUTPUT);
     for (uint8_t b=y;b<=6;) {
       pinMode (b,OUTPUT);
       digitalWrite(a,HIGH);

2,3=1
3,2=2
3,4=3
4,3=4
4,5=5
5,4=6
5,6=7
6,5=8
2,4=9
4,2=10
3,5=11
5,3=12
4,6=13
6,4=14
2,5=15
5,2=16
3,6=17
6,3=18
2,6=19
6,2=20

what is the purpose of

uint8_t n = 0; n++

So it would be best to add a library to have the numbers reference the pin outputs or... I don't know

Ok.
How about can I use a reference library?
To point to turning specific pins to high,low?

Tomorrow I will look up... maybe how to state that led1=pin 2,3 and led2=pin 3,2 so I can some how reference that order of leds by stating their names.
So maybe something like led1 = pin 2 and 3 high for a set delay.

MasterErik:
what is the purpose of
uint8_t n = 0; n++

It's a relic from another application. The code that you pasted was originally intended for another post, which asked, "How to light 96 charlieplexed LEDs sequentially?" 96 LEDs requires 11 pins, and they will accommodate 110 LEDs. The variable n just counted LEDs, and reset the loop when the last one was lit. Without it, the loop would have continued, trying to illuminate LEDs that didn't exist. You don't need it, because you're using 20 LEDs, and 20 is the number of LEDs that can be served with 5 pins. When you light the 20th LED, all the loops terminate, and loop() starts over.

If you're going to continue to use the display wired exactly as it is for multiple projects, it would be handy to keep the array that maps pins to LEDs available, so you won't have to figure it out all over again. As to whether it merits creating a library, that's up to you.

Haven't been able to address them with my ideas.

void setup() {
  for (uint8_t a=2;a<=6;a++) {
    pinMode(a,INPUT);
    digitalWrite(a,LOW);
  }
}
void loop () {
   for (uint8_t a=2;a<=6;a++) {
     pinMode (a,OUTPUT);
     for (uint8_t b=3;b<=6;) {
       pinMode (b,OUTPUT);
       digitalWrite(a,HIGH);
       delay(1000);
       digitalWrite(a,LOW);
       digitalWrite(b,HIGH);
       delay(1000);
       digitalWrite(b,LOW);

     }
   }
}

But having trouble addressing other than just the first 2...

void setup() {
  for (uint8_t a=2;a<=6;a++) {
    pinMode(a,INPUT);
    digitalWrite(a,LOW);
  }
}
void loop () {
   for (uint8_t a=2;a<=6;a++) {
     pinMode (a,OUTPUT);
     for (uint8_t b=3;b<=6;) {
       pinMode (b,OUTPUT);
       digitalWrite(a,HIGH);
       delay(1000);
       digitalWrite(a,LOW);
       digitalWrite(b,HIGH);
       delay(1000);
       digitalWrite(b,LOW);

     }
   }


   for (uint8_t a=3;a<=6;a++) {
     pinMode (a,OUTPUT);
     for (uint8_t b=4;b<=6;) {
       pinMode (b,OUTPUT);
       digitalWrite(a,HIGH);
       delay(1000);
       digitalWrite(a,LOW);
       digitalWrite(b,HIGH);
       delay(1000);
       digitalWrite(b,LOW);

     }
   }
}

And this only addressed 1 and 2 still. Why not 3 and 4?

You're not restoring the pins to the INPUT state after you finish with the LED. By the time you finish with the first loop, the pins are all set to OUTPUT.

The charlieplexing rule is:

  • No more than one pin can be OUTPUT, HIGH at any time
  • No more than one pin can be OUTPUT, LOW at any time.
  • The rest of the pins must be in the high-impedance state.

Before the end of the "b" loop, you need to set the pin mode back to INPUT to restore the high-impedance state. You need to do the same thing before the end of the "a" loop. Fix that, and see what you get.

Wow, so if I set it back to input and low each time this code gets long quickly!? :astonished:

If I wanted to do POV with charlieplexing then I would need to have a library?

Sure no one cares but... my code to move leds from 1 to 20 in order based on my wiring diagram I pictured above, I think it's there.

void setup() {
  for (uint8_t a=2; a<6; ++a) {
    pinMode(a,INPUT);
    digitalWrite(a,LOW);
  }
}
void loop () {
  
   for (uint8_t a=2;a<=6;) 
   for (uint8_t b=3;b<=6;)
   for (uint8_t c=4;c<=6;)
   for (uint8_t d=5;d<=6;)
   for (uint8_t e=6;e<=6;) {
            pinMode (a, INPUT); //1
            digitalWrite (a, LOW); //1
            pinMode (b, INPUT); //1
            digitalWrite (b, LOW); //1
            pinMode (c, INPUT); //1
            digitalWrite (c, LOW); //1
            pinMode (d, INPUT); //1
            digitalWrite (d, LOW); //1
            pinMode (e, INPUT); //1
            digitalWrite (e, LOW); //1
            pinMode (a, OUTPUT); //1
            digitalWrite (a, HIGH); //1
            pinMode (b, OUTPUT); //1
            digitalWrite (b, LOW); //1
            delay (100);
            //...
            //...
            pinMode (a, INPUT); //20
            digitalWrite (a, LOW); //20
            pinMode (b, INPUT); //20
            digitalWrite (b, LOW); //20
            pinMode (c, INPUT); //20
            digitalWrite (c, LOW); //20
            pinMode (d, INPUT); //20
            digitalWrite (d, LOW); //20
            pinMode (e, INPUT); //20
            digitalWrite (e, LOW); //20
            pinMode (e, OUTPUT); //20
            digitalWrite (e, HIGH); //20
            pinMode (a, OUTPUT); //20
            digitalWrite (a, LOW); //20
            delay (100);
     }
   
}

MasterErik:
Wow, so if I set it back to input and low each time this code gets long quickly!?

No, shorter. Add two lines, an autoincrement, and a change in loop starting value; deleted a bunch of lines. Here's some more stuff that was wrong: The "b" loop didn't have anything to change the value of "b" - it was always 3; the "b" loop started with three each time, when it needs to start with a+1.

This may or may not light all your lights in sequence - I don't know what changes, if any, you've made in your wiring. But, it should light all the lights, one at a time. See if it works. Then we can work on the order of the lights.

Modifications to your code, posted July 30, 9:12 AM by the forum's clock: compiles, runs, and lights all 20 lights of my charlieplex array, one at a time:

void setup() {
  for (uint8_t a=2;a<=6;a++) {
    pinMode(a,INPUT);
    digitalWrite(a,LOW);
  }
}
void loop () {
  for (uint8_t a=2;a<=6;a++) {
    pinMode (a,OUTPUT);
    for (uint8_t b=a+1;b<=6;b++) { // CHANGED "b=3" to "b=a+1", ADDED "b++"
      pinMode (b,OUTPUT);
      digitalWrite(a,HIGH);
      delay(1000);
      digitalWrite(a,LOW);
      digitalWrite(b,HIGH);
      delay(1000);
      digitalWrite(b,LOW);
      pinMode(b,INPUT);    // ADDED LINE
    }
    pinMode(a,INPUT);    // ADDED LINE
  }
/* DELETED LINES
  for (uint8_t a=3;a<=6;a++) {
    pinMode (a,OUTPUT);
    for (uint8_t b=4;b<=6;) {
      pinMode (b,OUTPUT);
      digitalWrite(a,HIGH);
      delay(1000);
      digitalWrite(a,LOW);
      digitalWrite(b,HIGH);
      delay(1000);
      digitalWrite(b,LOW);

    }
  }
END OF DELETED LINES */
}