Arduino Forum

Using Arduino => Programming Questions => Topic started by: Krzy1999 on May 18, 2018, 02:19 pm

Title: Multi-Dimensional Arrays, Retreving data.
Post by: Krzy1999 on 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.
 
Title: Re: Multi-Dimensional Arrays, Retreving data.
Post by: adwsystems on May 18, 2018, 02:44 pm
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.
Title: Re: Multi-Dimensional Arrays, Retreving data.
Post by: Krzy1999 on May 18, 2018, 03:08 pm
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.
Title: Re: Multi-Dimensional Arrays, Retreving data.
Post by: adwsystems on 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.
Title: Re: Multi-Dimensional Arrays, Retreving data.
Post by: Krzy1999 on May 18, 2018, 03:54 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?
Title: Re: Multi-Dimensional Arrays, Retreving data.
Post by: Blue Eyes on May 18, 2018, 06:30 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?

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);
    }
  }

Title: Re: Multi-Dimensional Arrays, Retreving data.
Post by: adwsystems on 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.
Title: Re: Multi-Dimensional Arrays, Retreving data.
Post by: Krzy1999 on 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.
https://drive.google.com/file/d/1Y-1d68rB3t7kaoRX7Mi5_I4C2NTDqH1_/view?usp=sharing




Title: Re: Multi-Dimensional Arrays, Retreving data.
Post by: adwsystems on 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
Title: Re: Multi-Dimensional Arrays, Retreving data.
Post by: Krzy1999 on May 18, 2018, 10:03 pm
I will work on this, will update you tomorow. Thanks for advice.
Title: Re: Multi-Dimensional Arrays, Retreving data.
Post by: Blue Eyes on 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.  

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

Title: Re: Multi-Dimensional Arrays, Retreving data.
Post by: adwsystems on May 18, 2018, 11:36 pm
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.
Title: Re: Multi-Dimensional Arrays, Retreving data.
Post by: BulldogLowell on 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 (https://www.amazon.com/Matrix-Interface-Display-Arduino-Mega2560/dp/B071FN6BDF/ref=sr_1_9?s=electronics&ie=UTF8&qid=1526679744&sr=1-9&keywords=SPI+led+matrix+display).

Title: Pls, respond
Post by: Krzy1999 on May 21, 2018, 11:30 pm
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.
Title: Re: Pls, respond
Post by: PaulS on 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.
Title: Re: Pls, respond
Post by: Krzy1999 on May 21, 2018, 11:58 pm
Sorry for posting in the wrong forum section i'm preatty new to this.

I have made a multidimentional array that look like this:

//Buttom right
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}  };

I want to be able to keep track of what position im on and update it with a press of a button. I want to explain more and awnser your questions if you can join discord would be lovely. :) thanks in advance https://discord.gg/vmCDUf

Title: Re: Pls, respond
Post by: lastchancename on May 22, 2018, 12:14 am
Change your (16-bit) ints to (8 bit) bytes - save half your memory!

Code: [Select]
byte readvalue;

// Buttom right
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}  };

// only you can find these...
byte x = firstIndex;
byte y = secondIndex;

readvalue = ledMatrixA[x][y];

// use readvalue for whatever you want...
Title: Re: Multi-Dimensional Arrays, Retreving data.
Post by: Coding Badly on May 22, 2018, 01:40 am

@Krzy1999, please do not cross-post.  Threads merged.

Title: Re: Multi-Dimensional Arrays, Retreving data.
Post by: adwsystems on May 22, 2018, 03:19 am
So im coding some simple code. but have no idea on how to actualy begin or make this code. Im willing to pay you 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.
What are analog buttons?

What you want isn't at all difficult. The wiring is far more difficult than the program will be. Someone else can write the program, but since they don't have the hardware, they can't test it. It would be still up to you to debug their program. I'm sure you can write the program, but you may need to reframe your thinking. The trick is to take it one step at a time.

1. Write a sketch that lights one LED.
2. Write a sketch to read the four buttons (don't forget to explain what analog buttons are, in the picture they look like standard on/off 0/1 push buttons)
3. Add the if statements to combine #1 and #2 (don't forget to not let the position go <0 or >15)

We can walk you through it. Let's begin.

Decide on where position x=0, y=0 is. Lower left?

Decide where you want the first LED to light.

Now I hate to say this, but start a blank sketch.

Write a sketch that turns on the one led at the start position (hint: declare two global variables posX and posY and set them to you selected start position).

Post your sketch when you have that done. Then we can walk through the next step.

This may sound kind of lame but trust me this process works. I have taught dozens of people and written hundreds of programs using it.