Arduino Cookbook Sketch 7.7 modified for 3x3 matrix

Wednesday August 26 2015

Hello. I modified the above sketch for a 3x3 common anode matrix built from scratch. I've changed rowPins and columnPins and delayMicroseconds. Multiple variations have compiled but (mostly) with the same result: all the LEDs turned ON but not one by one. Can you please help me find the problem ?

`` This is where I tried to post the code.

Here is what I did: I went back to the IDE 1.01 on my VISTA Notebook > Edit > Select all > Edit > Copy for Forum. I came back to my message and clicked the "code tags" icon but no code displayed on the Preview -- only the two bracketed words. I didn't get any error messages. Can you please help me copy the code to my message ? (I haven't done this before.) Thanks.

Just "Select all" from the IDE and 'normal' copy.
Code tags will put in a HTML style and tag. You need to paste your code in between the two markers.

You can edit the first post to put in your code.

I find this easier:

  1. In the IDE source code window, enter Ctrl-A
  2. Ctrl-C
  3. In your posting window, click on the code tag icon (</>)
  4. Ctrl-V

Done.

/* modified ACKBK 7.2 for 3x3RA

  • changed microSeconds to delay(100)
    *can see seqnential blinking but not fromn upper L corner
    *to lower R corner. Starts in upper L corner
    *goes to first two LEDs then back to first, then the entire 1st column etc
  • matrixMpx sketch
  • Sequence LEDs starting from first column and row until all LEDS are lit
  • Multiplexing is used to control 64 LEDs with 16 pins
    */

const int columnPins[] = {8, 9, 10};
const int rowPins[] = {5, 6, 7};

int pixel = 0; // 0 to 63 LEDs in the matrix
int columnLevel = 0; // pixel value converted into LED column
int rowLevel = 0; // pixel value converted into LED row

void setup() {
for (int i = 0; i < 3; i++)
{
pinMode(columnPins*, OUTPUT); // make all the LED pins outputs*
_ pinMode(rowPins*, OUTPUT);_
_
}_
_
}_
void loop() {
_
pixel = pixel + 1;_
_
if(pixel > 8)_
_
pixel = 0;_
_
columnLevel = pixel / 3; // map to the number of columns*_
* rowLevel = pixel % 3; // get the fractional value*
* for (int column = 0; column < 3; column++)*
* {*
* digitalWrite(columnPins[column], LOW); // connect this column to Ground*
* for(int row = 0; row < 3; row++)*
* {*
* if (columnLevel > column)*
* {*
* digitalWrite(rowPins[row], HIGH); // connect all LEDs in row to +5 volts*
* }*
* else if (columnLevel == column && rowLevel >= row)*
* {*
* digitalWrite(rowPins[row], HIGH);*
* }*
* else*
* {*
* digitalWrite(columnPins[column], LOW); // turn off all LEDs in this row*
* }*
* delay(100);*
* //delayMicroseconds(800000); // delay gives frame time of 20ms for 64 LEDs*
* digitalWrite(rowPins[row], LOW); // turn off LED*
* }*
* digitalWrite(columnPins[column], HIGH); // disconnect this column from GND*
* }*
}

*SECOND REPLY: *
Please see the update at the bottom of my original message. The question now is: does the pattern I describe match this code ? If so, we're OK.
I also found that using the CTRL keys was easier for copying the code. The select/copy method didn't work for me -- it just produced more brackets. Thanks for getting back to me so quickly.

It appears that you did not click the code tag icon (</>) located above the smiley face :slight_smile: icon or you moved the cursor in your post from between the code tags, [ code ] and [ /code ] to some other position in your post. If you do exactly what I said, it will work.

I did upload the code with the CTRL instructions. I also explained that I went back to the original 7.7 sketch code (modified for a 3x3 common anode matrix) and changed only the delay function. This is the code which I finally was able to upload to the Forum.

I would like to know if the pattern I describe matches the uploaded code. With delay(100) I could see the first LED start blinking in the upper left corner. Ihen the top 2 LEDs in the left-hand column blinked, then the entire left column. This was repeated in the middle column until all three columns (9 LEDS) blinked. There was a pause and the pattern then repeated from the upper left corner.

Also, can someone explain how to change the code so each LED blinks one by one from the upper left corner to the lower right corner ? Thanks.

I don't understand this comment:

int pixel       = 0;        // 0 to 63 LEDs in the matrix

If it is a 3x3x3, why are there 64 LED's? Seems to me it should be 27.

Also, can someone explain how to change the code so each LED blinks one by one from the upper left corner to the lower right corner ?

Wouldn't this require row 0, col 0, then row 1, col 1, then row 2, col 3? Seems this is a natural for a nested set of for loops.

Why is columnLevel = pixel / 3? Also, when you see something like this:

if(pixel > 8) )

it tells you that you don't have your posted code within code tags. Indeed, that's why code tags are there: so your code doesn't get mangled.

Never mind smiling faces or wrong comments , it does not compile ( with at least one error message ) because of this problem:

const int columnPins[] = {8, 9, 10};
const int rowPins[]   = {5, 6, 7};

int pixel       = 0;        // 0 to 63 LEDs in the matrix
int columnLevel = 0;        // pixel value converted into LED column
int rowLevel    = 0;        // pixel value converted into LED row

void setup() {
  for (int i = 0; i < 3; i++)
  {
    pinMode(columnPins, OUTPUT);  // make all the LED pins outputs
    pinMode(rowPins, OUTPUT);
  }
}

Never mind smiling faces or wrong comments...

Really? So dismissing comments that are misleading and posting code to this Forum improperly should be dismissed because you deem them unimportant? Who died and made you King?

Excuse my boldness, I guess YOUR opinion and YOUR SOLUTION to the problem ( correct comments) is more important than mine.
How silly of me.

It was silly of you and you are excused, but it wasn't just my comments. You were implying that your comment was the most important...and we're just supposed to brush everything else off the table. Also, if you really want to be helpful with your post, don't just quote the offending source lines...at least give the poster a hint of what to do next. Many posters here don't know what to do with "because of this problem:" followed by a dozen lines of code. I'm all for the OP figuring things out for themselves, but a little direction might be warranted.

@SkySong: Please edit your post with the code to add code tags to the source, so that Vaclav won't be confused by the absence of indices.
(We thought that after nearly 1k posts, he might have got the hang of what the absence of tags would do, but it seems a particularly difficult lesson for him to learn)

Here's a spare set you can have [code][/code]

Hello again. Thanks for your replies. I hope I haven't confused anyone.

  1. I don't understand what's the problem with the code I uploaded or how you would like me to fix it.

I had difficulty both with "Copy to Forum" and "Copy" (neither worked for me) and I uploaded the code with the suggested CTRL shortcuts.

  1. It looks like I missed the "63" in that comment -- sorry. I do proofread stuff.

  2. I'm working with a 2D 3x3 matrix -- not a cube.

  3. I'm working on my Arduino programming but I don't understand the reply about "row0, col1" etc. Where should that code go ?

  4. "columnLevel=pixel/3" is a modification of a line in the 7.8 sketch in Arduino Cookbook. The original statement is: "columnLevel = pixel/8;" I think "columnLevel" represents a calculated column value which is compared with "column" (the actual column in the display.)

  5. As you probably have noticed, pots are not used in this sketch. Can anyone point me to other RCS code which doesn't use pots ?

  6. Can we re-focus on whether the described pattern matches the uploaded code and how to get the LEDS to blink one-by-one from first to ninth ?

  7. I do have more programming questions (lots) but I know that I can't ask them all now. I hope my future posts will improve.

[
1) I don't understand what's the problem with the code I uploaded or how you would like me to fix it.

This one  - missed array index.

Than it compiles, so what is next on you list ? 

Suggestions: 
Use longer delay so you can see the sequence ( if any ) better.
If you add Serial.print liberally you can see the code progress too.
I have added a silly sample in this snippet as an example

And since we started with commenting on wrong comments - lets be persistent.
The line pinMode(columnPins[i], OUTPUT);  // make all the LED pins outputs 
does not make ALL pins output - just columnPins[i], so the comment belongs in front of the for loop.

code]void setup() {

  // make all the LED pins outputs
  for (int i = 0; i < 3; i++)
  {
    pinMode(columnPins[i], OUTPUT);  // make all the LED pins outputs
    Serial.print(" LED  @ pin  ");
    Serial.print(columnPins[i]);
    Serial.println( " set to OUTPUT "); 
    pinMode(rowPins[i] , OUTPUT);
  }
}

Here are some more mods :slight_smile:

[code]
/* modified ACKBK 7.2 for 3x3RA
* changed microSeconds to delay(100)
*can see seqnential blinking but not fromn upper L corner
*to lower R corner.  Starts in upper L corner
*goes to first two LEDs then back to first, then the entire 1st column etc
* matrixMpx sketch
* Sequence LEDs starting from first column and row until all LEDS are lit
* Multiplexing is used to control 64 LEDs with 16 pins
*/

const int columnPins[] = {8, 9, 10};
const int rowPins[]   = {5, 6, 7};

int pixel       = 0;        // 0 to 63 LEDs in the matrix
int columnLevel = 0;        // pixel value converted into LED column
int rowLevel    = 0;        // pixel value converted into LED row

void setup() {
  Serial.begin(115200);
  Serial.print(__FILE__);

  for (int i = 0; i < 3; i++)
  {
    pinMode(columnPins[i], OUTPUT);  // make all the LED pins outputs
    pinMode(rowPins[i], OUTPUT);
  }
}

void loop() {
  pixel = pixel + 1;
  if (pixel > 8)
    pixel = 0;

  columnLevel = pixel / 3;                     // map to the number of columns
  Serial.println(columnLevel);
  //for(;;);

  rowLevel = pixel % 3;                        // get the fractional value
  for (int column = 0; column < 3; column++)
  {
    digitalWrite(columnPins[column], LOW);     // connect this column to Ground
    Serial.print("connect pin ");
    Serial.print(columnPins[column]);
    Serial.println(" column ");
    for (int row = 0; row < 3; row++)
    {
      if (columnLevel > column)
      {
        Serial.println("connect all LEDs in row to +5 volts");
        digitalWrite(rowPins[row], HIGH);  // connect all LEDs in row to +5 volts
      }
      else if (columnLevel == column && rowLevel >= row)
      {
        digitalWrite(rowPins[row], HIGH);
      }
      else
      {
        digitalWrite(columnPins[column], LOW); // turn off all LEDs in this row
      }

      //delayMicroseconds(800000);     // delay gives frame time of 20ms for 64 LEDs
      digitalWrite(rowPins[row], LOW);         // turn off LED
      Serial.print("turn off pin ");
      Serial.print(rowPins[row]);
      Serial.println(" row ??");
      delay(1000);


    }

    digitalWrite(columnPins[column], HIGH);   // disconnect this column from GND
  }
}

[/code]

Hello again.

I don't know how that smiley face got into the pixel section of the code I uploaded. The statement should read: "if (pixel>8)" go back and start over.

Some of the earlier mods I made did not compile but the code I uploaded to the Forum did compile and run with the pattern I have described elsewhere (e.g. see top of uploaded code.) I could see the sequencing after I switched from microseconds to milliseconds. I think the pattern matches the code but I wanted to double-check. (I have a good quality video on my IPAD but I don't know how to send it to the Forum. Is there a way to do that ?)

I understand what you mean about the setup() comment being in the wrong place. Thanks for pointing that out but that's where the comment is in the book and I probably downloaded the code from a website without ever changing it.

My other question is: does anyone know of other RCS code (that doesn't use pots) which will get the LEDs to blink one by one across each row (or down each column) without going back to the first LED until all 9 LEDs have blinked ?

Thank you for exolaining how to add serial code to a sketch. I haven't tried it yet but will do so soon.

I don't know how that smiley face got into the pixel section of the code I uploaded.

It's because you didn't use CODE TAGS

Hello again.

I don't see my last post which I uploaded this morning so I'll try to summarize very briefly:

The code I uploaded did compile and run -- as I described (e.g. in my comment at the very top of the code.). I have a good quality video on my IPAD but don't know how to send it to the Forum. Any suggestions ?

  1. I still would like to double check if my description of the sequence matches the code I uploaded. (I expected something more like Knight Rider except on a Matrix.)

  2. I also still wonder if anyone knows of any other RCS code (which does not use pots) where the LEDs light up in sequence from the first to the last without going back to the first LED until the repeat (like the basic Knight Rider pattern.)

The comment in setup() is from the book so I just left it there -- but I understand why it is wrong. The smiley face in the pixel section covers an "8" so the line read: "if (pixel > 8" go back and start counting pixels again. I appreciate the explanation of where to put the serial code in a program and will try it in another matrix sketch.

I hope to speak better Arduino in the future. Thanks for your continuing help.