Go Down

Topic: Matrix problem (Read 2968 times) previous topic - next topic

PaulRB

actually, when everything is 0 the matrix is on
Oh, yeah.

The evening or this weekend, will try to get you another step forward. Maybe we can display a fixed image.

bogdan666

no problem, i appreciate it.

PaulRB

i am prepared to learn every line of the code
For the code that is working now, are there any lines you do not understand or have questions about?

bogdan666

no, it was very clear because you started with a big delay between the rows, so i saw each change with "my own eyes"

PaulRB

#64
Apr 13, 2019, 11:46 am Last Edit: Apr 13, 2019, 11:50 am by PaulRB
OK, lets add a few more lines of code:
Code: [Select]
#include <SPI.h>

#define LATCH 10
#define MULTIPLEX_PERIOD 2

SPISettings shiftRegister(10000000UL, LSBFIRST, SPI_MODE0);

unsigned int image[8] = {
  0b0000000000001000,
  0b0000000000001100,
  0b0000000000001110,
  0b1111111111111111,
  0b0000000000001110,
  0b0000000000001100,
  0b0000000000001000,
  0b0000000000000000
};

void setup() {
  SPI.begin(); //Start up the SPI hardware
  SPI.beginTransaction(shiftRegister); //Prepare to send data to shift registers
  pinMode(LATCH, OUTPUT);
}

void multiplexDisplay() {
  //"static" means these variables do not loose their value when multiplexDisplay() ends
  static byte currRow; //the row, 0 to 7, of the matrix that is currently lit
  static unsigned long lastUpdate; //time when this row was lit
  unsigned long millisNow = millis(); //Get time now
  if (millisNow - lastUpdate >= MULTIPLEX_PERIOD) { //Is it time to light next row of matrix?
    lastUpdate = millisNow; //Record time this row was lit
    SPI.transfer(1 << currRow); //Send a byte indicating which row to light
    SPI.transfer(~lowByte(image[currRow])); //Send the required pattern for this row of the matrix
    SPI.transfer(~highByte(image[currRow]));
    digitalWrite(LATCH, LOW);
    digitalWrite(LATCH, HIGH); //Latch the data into the shift register outputs
    if (currRow++ > 7) currRow = 0; //Get ready to light next row of matrix
  }
}

void loop() {
  multiplexDisplay();
}

bogdan666

The arrow pointing to the left is displaying correctly, no flickering, and also, now because you change to lsbfirst the leds light up at 1 instead of 0 ?

PaulRB

Ah. The arrow was supposed to point right, as it looks in the code. Change the LSBFIRST back to MSBFIRST and swap the highByte() and lowByte() functions.

What changed the LEDs to light up with a 1 is the "~" operator. It means "binary not" and reverses 0s & 1s.

bogdan666

#67
Apr 13, 2019, 06:20 pm Last Edit: Apr 13, 2019, 07:09 pm by bogdan666
wait, it's pointing right, i just mixed them up sorry.

LE: in my excitement of seeing an arrow on my matrix, after confusing right and left, i noticed another thing, the code starts from first row, but my first row is off, so i think that the arrow is up side down.

PaulRB

Try this change:
Code: [Select]
    SPI.transfer(128 >> currRow); //Send a byte indicating which row to light

bogdan666

yes, it works, the leds are on for every 1 in the code, but you have to explain what happened and the changes with lsb msb and the last change.

PaulRB

#70
Apr 13, 2019, 09:09 pm Last Edit: Apr 13, 2019, 09:11 pm by PaulRB
This page will tell you about the << and >> operators.

This page will tell you about MSBFIRST and LSBFIRST.

bogdan666

so in a very short way, both of these problems tell you the "direction" from where the bits are being read, right ?

PaulRB

so in a very short way, both of these problems tell you the "direction" from where the bits are being read, right ?
LSBFIRST & MSBFIRST do, yes.

The << & >> operators move bits around inside a variable by shifting them right or left by one or more places.

bogdan666


PaulRB

#74
Apr 14, 2019, 12:03 pm Last Edit: Apr 14, 2019, 12:03 pm by PaulRB
Shall we try a little scrolling next?
Code: [Select]
#include <SPI.h>

#define LATCH 10
#define MULTIPLEX_PERIOD 2
#define SCROLL_PERIOD 200

SPISettings shiftRegister(10000000UL, MSBFIRST, SPI_MODE0);

unsigned int image[8] = {
  0b0000000000001000,
  0b0000000000001100,
  0b0000000000001110,
  0b1111111111111111,
  0b0000000000001110,
  0b0000000000001100,
  0b0000000000001000,
  0b0000000000000000
};

void setup() {
  SPI.begin(); //Start up the SPI hardware
  SPI.beginTransaction(shiftRegister); //Prepare to send data to shift registers
  pinMode(LATCH, OUTPUT);
}

void multiplexDisplay() {
  //"static" means these variables does not loose their value when multiplexDisplay() ends
  static byte currRow; //the row, 0 to 7, of the matrix that is currently lit
  static unsigned long lastUpdate; //time when this row was lit
  unsigned long millisNow = millis(); //Get time now
  if (millisNow - lastUpdate >= MULTIPLEX_PERIOD) { //Is it time to light next row of matrix?
    lastUpdate = millisNow; //Record time this row was lit
    SPI.transfer(128 >> currRow); //Send a byte indicating which row to light
    SPI.transfer(~highByte(image[currRow])); //Send the required pattern for this row of the matrix
    SPI.transfer(~lowByte(image[currRow]));
    digitalWrite(LATCH, LOW);
    digitalWrite(LATCH, HIGH); //Latch the data into the shift register outputs
    if (currRow++ > 7) currRow = 0; //Get ready to light next row of matrix
  }
}

void scrollDisplay() {
  static unsigned long lastUpdate; //time when matrix was last scrolled
  unsigned long millisNow = millis(); //Get time now
  if (millisNow - lastUpdate >= SCROLL_PERIOD) { //Is it time to scroll the matrix?
    lastUpdate = millisNow; //Record time the matrix was scrolled
    for (byte row = 0; row < 8; row++) {
      byte savedBit = bitRead(image[row], 15);
      image[row] <<= 1;
      bitWrite(image[row], 0, savedBit);
    }
  }
}

void loop() {
  multiplexDisplay();
  scrollDisplay();
}

As before, please let me know what you see, I can't test this. Also not sure which direction it will scroll! And as always, please read each line and ask about anything you don't understand.

Go Up