0

I planed a 6x6 matrix with a Arduino Nano as controller. Its only possible to let one LED in the matrix lighten up. Any idea how I get the Code smaller? (Im a C Noobie ^^)

~Nico

Circuit diagram: enter image description here

Code:

void setup() {
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, INPUT);
  pinMode(15, INPUT);
  pinMode(16, INPUT);
  pinMode(17, INPUT);
}

void loop() {
  int value1 = digitalRead(14);
  int value2 = digitalRead(15);
  int value3 = digitalRead(16);
  int test = digitalRead(17);
  int x1 = 2;
  int x2 = 3;
  int x3 = 4;
  int x4 = 5;
  int x5 = 6;
  int x6 = 7;
  int y1 = 8;
  int y2 = 9;
  int y3 = 10;
  int y4 = 11;
  int y5 = 12;
  int y6 = 13;


  // put your main code here, to run repeatedly:


  if (test == HIGH) {
    digitalWrite(x1, HIGH);
    digitalWrite(y1, HIGH);
    delay(15);
    digitalWrite(y1, LOW);
    digitalWrite(y2, HIGH);
    delay(15);
    digitalWrite(y2, LOW);
    digitalWrite(y3, HIGH);
    delay(15);
    digitalWrite(y3, LOW);
    digitalWrite(y4, HIGH);
    delay(15);
    digitalWrite(y4, LOW);
    digitalWrite(y5, HIGH);
    delay(15);
    digitalWrite(y5, LOW);
    digitalWrite(y6, HIGH);
    delay(15);
    digitalWrite(y6, LOW);
    digitalWrite(x1, LOW);
    digitalWrite(x2, HIGH);
    digitalWrite(y1, HIGH);
    delay(15);
    digitalWrite(y1, LOW);
    digitalWrite(y2, HIGH);
    delay(15);
    digitalWrite(y2, LOW);
    digitalWrite(y3, HIGH);
    delay(15);
    digitalWrite(y3, LOW);
    digitalWrite(y4, HIGH);
    delay(15);
    digitalWrite(y4, LOW);
    digitalWrite(y5, HIGH);
    delay(15);
    digitalWrite(y5, LOW);
    digitalWrite(y6, HIGH);
    delay(15);
    digitalWrite(y6, LOW);
    digitalWrite(x2, LOW);
    digitalWrite(x3, HIGH);
    digitalWrite(y1, HIGH);
    delay(15);
    digitalWrite(y1, LOW);
    digitalWrite(y2, HIGH);
    delay(15);
    digitalWrite(y2, LOW);
    digitalWrite(y3, HIGH);
    delay(15);
    digitalWrite(y3, LOW);
    digitalWrite(y4, HIGH);
    delay(15);
    digitalWrite(y4, LOW);
    digitalWrite(y5, HIGH);
    delay(15);
    digitalWrite(y5, LOW);
    digitalWrite(y6, HIGH);
    delay(15);
    digitalWrite(y6, LOW);
    digitalWrite(x3, LOW);
    digitalWrite(x4, HIGH);
    digitalWrite(y1, HIGH);
    delay(15);
    digitalWrite(y1, LOW);
    digitalWrite(y2, HIGH);
    delay(15);
    digitalWrite(y2, LOW);
    digitalWrite(y3, HIGH);
    delay(15);
    digitalWrite(y3, LOW);
    digitalWrite(y4, HIGH);
    delay(15);
    digitalWrite(y4, LOW);
    digitalWrite(y5, HIGH);
    delay(15);
    digitalWrite(y5, LOW);
    digitalWrite(y6, HIGH);
    delay(15);
    digitalWrite(y6, LOW);
    digitalWrite(x4, LOW);
    digitalWrite(x5, HIGH);
    digitalWrite(y1, HIGH);
    delay(15);
    digitalWrite(y1, LOW);
    digitalWrite(y2, HIGH);
    delay(15);
    digitalWrite(y2, LOW);
    digitalWrite(y3, HIGH);
    delay(15);
    digitalWrite(y3, LOW);
    digitalWrite(y4, HIGH);
    delay(15);
    digitalWrite(y4, LOW);
    digitalWrite(y5, HIGH);
    delay(15);
    digitalWrite(y5, LOW);
    digitalWrite(y6, HIGH);
    delay(15);
    digitalWrite(y6, LOW);
    digitalWrite(x5, LOW);
    digitalWrite(x6, HIGH);
    digitalWrite(y1, HIGH);
    delay(15);
    digitalWrite(y1, LOW);
    digitalWrite(y2, HIGH);
    delay(15);
    digitalWrite(y2, LOW);
    digitalWrite(y3, HIGH);
    delay(15);
    digitalWrite(y3, LOW);
    digitalWrite(y4, HIGH);
    delay(15);
    digitalWrite(y4, LOW);
    digitalWrite(y5, HIGH);
    delay(15);
    digitalWrite(y5, LOW);
    digitalWrite(y6, HIGH);
    delay(15);
    digitalWrite(y6, LOW);
    digitalWrite(x6, LOW);



  }
}
  • 1
    The concepts which will help you are for loops and possibly arrays. – Mark Smith Mar 17 '17 at 19:07
  • Definitely arrays. Any idea how I get the Code smaller? - why does it need to be smaller? It fits into available memory, or it doesn't. – Nick Gammon Mar 18 '17 at 7:23
2

Any idea how I get the Code smaller?

don't try to get the code smaller -> try to get it working.

for things like this, you should think about

1) how to structure the display data: typically it is done in a matrix format. I would structure it as an array of 6 chars / columns, with the rows in the lowest 6 bits;

2) how to track the column being displayed and updated: using a static variable typically;

3) how to update the display periodically: via a timer interrupt usually.

Each update would go something like this:

  turn off all columns;
  present row information;
  update the column index and turn it on;

what's left is to implement that in a way that the code is flexible and portable to a different application.

once that's done, all your application needs to do is to stuff the right information into the display buffer and the display routine, invoked via a timer isr, will take care of the updating all by itself, without user involvement.

if you still need help, let us know.

edit: i put this together quickly to demonstrate the concept.

it is a display routine, led_display(), being invoked via a timer2 interrupt, displaying data in an 8-char buffer, 0x11, 0x22, ..., 0x88.

the scanning is done by colomn (active high), and the rows are connected to D0..D7, and the cols D8..D13, A0, A1, all user configurable.

//global variables
uint8_t lRAM[COL_NUM]={         //display buffer
    0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
};

const uint8_t row_pins[ROW_NUM]={   //pins tied to matrix' rows
    0, 1, 2, 3, 4, 5, 6, 7,
};

const uint8_t col_pins[COL_NUM]={   //pins tied to columns
    8, 9, 10, 11, 12, 13, A0, A1, 
};

once the initialial set-up is done, there is nothing the user needs to do, other than filling the display buffer with information s/he wishes to be displayed.

void setup() {
    // put your setup code here, to run once:
    //pinMode(led, OUTPUT);
    led_init();                         //reset the led module

    //set up timer2
    tmr2_init(TMR2PS_32x);              //timer2 with 32:1 prescaler -> 16Mhz / 256 / 32 = 2Khz per col. 
    tmr2_act(led_display);              //install user handler to update the display periodically
}

void loop() {
    // put your main code here, to run repeatedly:
    //digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    //delay(100);               // wait for a second
    //digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    //delay(100);               // wait for a second
}

and here is the execution.enter image description here

ps: the code was originally written to drive 8x7-segment displays but it works with 8x8 matrix as well -> they are essentially the same thing.

the display routine, led_display(), follows the general sketch laid out earlier.

//update the display
void led_display(void) {
    static uint8_t col_index=0;         //current col
    //turn off current col -> the only active col now
    COL_OFF(col_pins[col_index]);

    //update the column index
    col_index=(col_index==COL_NUM-1)?0:(col_index+1);

    //present row data
    if (lRAM[col_index] & (1<<0)) ROW_ON(row_pins[0]); else ROW_OFF(row_pins[0]);
    if (lRAM[col_index] & (1<<1)) ROW_ON(row_pins[1]); else ROW_OFF(row_pins[1]);
    if (lRAM[col_index] & (1<<2)) ROW_ON(row_pins[2]); else ROW_OFF(row_pins[2]);
    if (lRAM[col_index] & (1<<3)) ROW_ON(row_pins[3]); else ROW_OFF(row_pins[3]);
    if (lRAM[col_index] & (1<<4)) ROW_ON(row_pins[4]); else ROW_OFF(row_pins[4]);
    if (lRAM[col_index] & (1<<5)) ROW_ON(row_pins[5]); else ROW_OFF(row_pins[5]);
    if (lRAM[col_index] & (1<<6)) ROW_ON(row_pins[6]); else ROW_OFF(row_pins[6]);
    if (lRAM[col_index] & (1<<7)) ROW_ON(row_pins[7]); else ROW_OFF(row_pins[7]);

    //turn on the display
    COL_ON(col_pins[col_index]);
}
  • The problem is, that im not allowed to change something in the circuit. I am only coding it for a good Friend how allready solder it. The Circuit works and i have to programm some Programms for it :) Something like waves or circles. – Nico L. Mar 17 '17 at 20:33
  • if I understand something wrong correct me :D Im not so good in English – Nico L. Mar 17 '17 at 20:38
  • It is maybe possible to make a routine wich is going in the background and you can putt some pattern in it? if yes can you help me programm it :) I dont know much about c & c++ – Nico L. Mar 17 '17 at 20:51
  • obviously people will be willing to help you but you have to get it started somewhere. no one is going to write the code for you. – dannyf Mar 17 '17 at 21:48
3

You can use arrays to hold sets of row pin numbers or column pin numbers, which then allows use of for loops for pinMode() settings and for testing LEDs one after another, as shown in code below.

Note, using enum { ... }; for integer-constant declarations is slightly more compact than a series of const int statements. When a constant name in an enum does not have an = and value after it, it is given a value one larger than the previous constant. So, for example, the first enum in the following code sets x2 to 3, x3 to 4, etc.

enum { x1=2, x2, x3, x4, x5, x6};
enum { y1=8, y2, y3, y4, y5, y6};
const byte rowPins[] = { x1, x2, x3, x4, x5, x6};
const byte colPins[] = { y1, y2, y3, y4, y5, y6};
enum { nRow = sizeof rowPins / sizeof rowPins[0] };
enum { nCol = sizeof colPins / sizeof colPins[0] };

void setup() {
  for (byte i=0; i<nRow; ++i) pinMode(rowPins[i], OUTPUT);
  for (byte i=0; i<nCol; ++i) pinMode(colPins[i], OUTPUT);
  for (byte i=14; i<18; ++i)  pinMode(i, INPUT);
}

void loop() {
  byte test = digitalRead(17);
  if (test) {
    for (byte x=0; x<nRow; ++x) {
      digitalWrite(rowPins[x], HIGH);
      for (byte y=0; y<nCol; ++y) {
        digitalWrite(colPins[y], HIGH);
        delay(15);
        digitalWrite(colPins[y], LOW);
      }
      digitalWrite(rowPins[x], LOW);
    }
  }
}

Note, subroutines and functions can be used to modularize code and (typically) reduce its volume. See code sample below. In the code as is, the subroutine doesn't particularly reduce volume, but definitely adds modularity. You may find it useful to look up refactoring.

enum { x1=2, x2, x3, x4, x5, x6};
enum { y1=8, y2, y3, y4, y5, y6};
const byte rowPins[] = { x1, x2, x3, x4, x5, x6};
const byte colPins[] = { y1, y2, y3, y4, y5, y6};
enum { nRow = sizeof rowPins / sizeof rowPins[0] };
enum { nCol = sizeof colPins / sizeof colPins[0] };

void setup() {
  for (byte i=0; i<nRow; ++i) pinMode(rowPins[i], OUTPUT);
  for (byte i=0; i<nCol; ++i) pinMode(colPins[i], OUTPUT);
  for (byte i=14; i<18; ++i)  pinMode(i, INPUT);
}

void lightup(byte x, byte y, unsigned int howLong) {
  digitalWrite(rowPins[x], HIGH);
  digitalWrite(colPins[y], HIGH);
  delay(howLong);
  digitalWrite(colPins[y], LOW);
  digitalWrite(rowPins[x], LOW);
}

void loop() {
  byte test = digitalRead(17);
  if (test) {
    for (byte x=0; x<nRow; ++x)
      for (byte y=0; y<nCol; ++y)
        lightup(x, y, 15);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.