Go Down

Topic: Multi-Dimensional Arrays, Retreving data. (Read 155 times) previous topic - next topic

Krzy1999

Hello, i'm a student of DTU in copenhagen and i'm working on a project. I'm pretty new to arduino coding. I've made a board with 4 led matrixes and i'v made a multi-dimensional array that is written up so it fits the 8x8 led matrix for truning on each light this is made possible with a few HC595 that controll the rows and colums.

I want to make a dot move around the array/ledmatrix. I don't know how to read or controll the array. I've conected buttons and want them to controll the dot by shifting through the data in the array and uploading new data to the arduino every time i press a button, basicly i'm trying to make snake.

i have linked the code im using so you can see what i mean, and a picture of the project that can give you an ide of what is going on.
 

adwsystems

#1
May 18, 2018, 02:44 pm Last Edit: May 18, 2018, 02:56 pm by adwsystems
What does your current program do? What have you been able to program so far? Can you get the leds to turn on?

In short what you will need to do is record the current position in the 16x16 grid. When a button is pressed, increment or decrement the position to move around the grid. If position 0,0 is the LED in the upper right, then increment on right and down, decrement on left or up. Then light up the new LED position.

Krzy1999

#2
May 18, 2018, 03:08 pm Last Edit: May 18, 2018, 03:33 pm by Krzy1999
Right now you can turn on every single light by itself using this code:

Chosing the byte that will be sent to the arduino.

byte heartPowerA[1] =  {B10001000};
byte heartGroundA[1] = {B11111011};


Sending the byte using shiftOut command.

  for (int i = 0; i < 1; i++)
  {
    digitalWrite(latchPinA2, LOW);
    digitalWrite(latchPinA1, LOW);

    shiftOut(dataPinA2, clockPinA2, MSBFIRST, heartGroundA);
    shiftOut(dataPinA1, clockPinA1, MSBFIRST, heartPowerA);

    digitalWrite(latchPinA1, HIGH);
    digitalWrite(latchPinA2, HIGH);

    delay(100);
  }


First off the column 0 would be used to control the up and down motion. The columns and rows 1-8 would controll where the dot goes left to right.
 
What i realy want to do is use this array and implement my buttons that would shift through the array.
Lets say that my dot was on the top lef of the led matrix the bytes i would be sending would be B11111110 and B10000000. Now if i wanted to move the dot to the right i want the button to move through the array and with the press of the right button start sending B01000000, if pressed again B00100000. As i'v wirten in the aray.

The same goes for up and down they would just be controlled by the column0 ex. the top left light B11111110 and B10000000. On the press go one down the bytes would be B11111101 etc.

int ledMatrixA[8][9] ={ 
{B11111110,B10000000,B01000000,B00100000,B00010000,B00001000,B00000100,B00000010,B00000001},
{B11111101,B10000000,B01000000,B00100000,B00010000,B00001000,B00000100,B00000010,B00000001},
{B11111011,B10000000,B01000000,B00100000,B00010000,B00001000,B00000100,B00000010,B00000001},
{B11110111,B10000000,B01000000,B00100000,B00010000,B00001000,B00000100,B00000010,B00000001},
{B11101111,B10000000,B01000000,B00100000,B00010000,B00001000,B00000100,B00000010,B00000001},
{B11011111,B10000000,B01000000,B00100000,B00010000,B00001000,B00000100,B00000010,B00000001},
{B10111111,B10000000,B01000000,B00100000,B00010000,B00001000,B00000100,B00000010,B00000001},
{B01111111,B10000000,B01000000,B00100000,B00010000,B00001000,B00000100,B00000010,B00000001}  };

If you have any idea of how this should be done please help.

adwsystems

You have put a lot of thought into this already. Using the direction you are going, just track your location and use it to send the value from the array to the LED matrix.

Krzy1999

#4
May 18, 2018, 03:54 pm Last Edit: May 18, 2018, 03:55 pm by Krzy1999
I have my thoughts but 0 arduino programing skill.
Any idea of what comands would be good to look into when programing this?
Should i make some kind of forloop for (i) (j) so i can controll how the array?

Blue Eyes

#5
May 18, 2018, 06:30 pm Last Edit: May 18, 2018, 08:22 pm by Blue Eyes
What LEDs turn on with the code that you attached?  How are the LEDs wired (ground = column or ground = row)?  What does the following do?

Code: [Select]

  for (int row = 0; row < 8; row++)
  {
    for (int col = 0; col < 8; col++)
    {
      digitalWrite(latchPinA2, LOW);
      digitalWrite(latchPinA1, LOW);

      shiftOut(dataPinA2, clockPinA2, MSBFIRST, ledMatrixA[row]);
      shiftOut(dataPinA1, clockPinA1, MSBFIRST, ledMatrixA[col + 1]);

      digitalWrite(latchPinA2, HIGH);
      digitalWrite(latchPinA1, HIGH);

      delay(100);
    }
  }


adwsystems

I have my thoughts but 0 arduino programing skill.
Any idea of what comands would be good to look into when programing this?
Should i make some kind of forloop for (i) (j) so i can controll how the array?
The loop turns on one led at a time. You want to turn on a specific LED. Make two variables posX, and posY. Incement and decrement based on the button switches (b esure to make sure posX and posY stay =>0 and <= 15). Then use posX and posY in place of row and col in the loop and get rid of the loop. Also best to only send the data to the LED when the button is pressed.

Krzy1999

What LEDs turn on with the code that you attached?  How are the LEDs wired (ground = column or ground = row)?  What does the following do?


  for (int row = 0; row < 8; row++)
  {
    for (int col = 0; col < 8; col++)
    {
      digitalWrite(latchPinA2, LOW);
      digitalWrite(latchPinA1, LOW);

      shiftOut(dataPinA2, clockPinA2, MSBFIRST, ledMatrixA[row]);
      shiftOut(dataPinA1, clockPinA1, MSBFIRST, ledMatrixA[col + 1]);

      digitalWrite(latchPinA2, HIGH);
      digitalWrite(latchPinA1, HIGH);

      delay(100);
    }
  }e]


The code i showed in my reply turns on 2 leds on row 3 one in position 1 and the other at 5 on the led matrix

The columns are controlled by the power also (the command heartPower),
The rows are controlled by the ground also (the command heartGround).

The code runs through a wierd and satesfying pattern i have conected a google drive link where you can watch the pattern.
https://drive.google.com/file/d/1Y-1d68rB3t7kaoRX7Mi5_I4C2NTDqH1_/view?usp=sharing





adwsystems

KISS, man, KISS. Keep It Stupidly Simple.

I would setup an unsigned int array of length 16. That will give you 16 bits wide and 16 rows tall.

On button press:
1. turn off the old bit
2. update posX and posY
3. turn on bit at new location
4. send array to shift registers

Krzy1999

I will work on this, will update you tomorow. Thanks for advice.

Blue Eyes

It appears the code that I posted had power and grounds reversed, since you say that columns are controlled by the power and the code says that A2 is grounds.  Maybe dataPinA_row and dataPinA_col would be better names?

Your code has three arrays of ints named ledMatrixA, ledMatrixB, ledMatrixC and ledMatrixD.  It looks like the A and D are identical and the B and C are different.

I don't think it's necessary to store all those values in arrays, since they can be calculated based on row and column.  For example, the following reproduces the ledMatrixA array just by bit shifting.

Code: [Select]

int ledMatrixA[8][9] = {
  {B11111110, B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001},
  {B11111101, B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001},
  {B11111011, B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001},
  {B11110111, B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001},
  {B11101111, B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001},
  {B11011111, B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001},
  {B10111111, B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001},
  {B01111111, B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001}
};

void setup() {
  Serial.begin(9600);

  byte row, col;

  for (byte r = 0; r < 8; r++)
  {

    row = (1 << r) ^ B11111111;;
    Serial.print(row, BIN); Serial.print(", ");

    for (byte c = 0; c < 8; c++)
    {

      col = 1 << (7 - c);
      Serial.print(col, BIN); Serial.print(", ");

    }

  Serial.println();

  }
}

void loop() { }



I think if I were working on this I would make an array of the shift register pins like this:

Code: [Select]

typedef struct  {
  int rowData;
  int rowLatch;
  int rowClock;
  int colDate;
  int colLatch;
  int colClock;

} shiftreg;

shiftreg shiftRegPins[] = {{13, 12, 11, 10, 9, 8}, {7, 6, 5, 4, 3, 2}};


 

Then work out how to calculate a row and column based on the LED array index and write a function to turn on a specific LED given a matrix #, row and column.  Multiple columns in a row could be achieved via bit operations.

After that was done I would write the code to handle the patterns associated with the buttons.  

BTW, the wiring on your breadboards is a work of art.


adwsystems

BTW, the wiring on your breadboards is a work of art.
Yes, kudos on the wiring. I know for me, and I'm sure for a few others, people are more willing to help when they see neat work and an attempt to be organized. It will help in debugging and discussion.

@BlueEyes, that is similar to what I am suggesting. Use the array to store the LED(s) to be lit, and the current position (posX and posY) to modify the array.I look forward to seeing (code and video, if possible) what Krzy1999 comes back with.

BulldogLowell

Yes, kudos on the wiring. I know for me, and I'm sure for a few others, people are more willing to help when they see neat work and an attempt to be organized. It will help in debugging and discussion.
yes, but maybe it is time to look at an I2C or SPI led matrix display.


Go Up