Go Down

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

#### Krzy1999

##### May 18, 2018, 02:19 pm
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.

#1
##### May 18, 2018, 02:44 pmLast 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 pmLast 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}  };

#3
##### May 18, 2018, 03:45 pm
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 pmLast 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 pmLast 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);    }  }`

#6
##### May 18, 2018, 07:44 pm
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

#7
##### May 18, 2018, 08:33 pm
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.

#8
##### May 18, 2018, 08:38 pm
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

#9
##### May 18, 2018, 10:03 pm
I will work on this, will update you tomorow. Thanks for advice.

#### Blue Eyes

#10
##### May 18, 2018, 10:08 pm
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.

#11
##### May 18, 2018, 11:36 pm
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

#12
##### May 18, 2018, 11:44 pm
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.

#### Krzy1999

#13
##### May 21, 2018, 11:30 pmLast Edit: May 22, 2018, 05:10 am by Krzy1999
So im coding some simple code. but have no idea on how to actualy begin or make this code. If you could help me with coding this shit. I'v made a multidimentional array. I want to be able to controll and update the position the the array. I'm using analog buttons. I can help you on what i've written allready and explain and awnser all of you'r questions over Teamspeak/Discord/Skype etc.

#### PaulS

#14
##### May 21, 2018, 11:36 pm
Quote
if you could help me with coding this shit.
I'm great at coding shit. I'm not too bad at coding stuff that actually works.

This part of the forum is for discussing the code you've already mangled. Click the report to moderator link, and ask that your post be moved to where it belongs - Gigs and Collaboration.