Having problems with 64x16 LED Matrix and randomSeed

Hi, I purchases a 64x16 LED matrix off Ebay awhile ago. After several weeks of program altering and trial and error, I was able to get it to do everything I hoped for. Now I would really like to program the sketch with a randomSeed. I have used the randomSeed function in several other sketches including two LCD displays, but no matter how I approach this one, it will not work. I could really use a point in the right direction. My programming abilities are very limited, but usually I can solve my problems with time, research and perseverance.

I had to cut out some of the actual matrix coding in the center to upload the file, but the first and last are still there. (There are 21 Bmp unsigned char in the full sketch)

Thank you for any assistance anyone can offer.
Bryan

#define COL_PIXEL     64
#define ROW_PIXEL     16

int latchPin=8; //LT
int clockPin=12;//SK
int dataPin=11; //R1

int en_74138 = 2;
int la_74138 = 3;
int lb_74138 = 4;
int lc_74138 = 5;
int ld_74138 = 6;

int sensorPin1=A1;
int sensorValue1=1;
long randomNumber;



 
unsigned int ROW_xPixel;
unsigned int ROW_num;
unsigned char Col_num_1;
unsigned char Col_num_2;
unsigned char Col_num_3;
unsigned char Col_num_4;
unsigned char Col_num_5;
unsigned char Col_num_6;
unsigned char Col_num_7;
unsigned char Col_num_8;


//Data code: Horizontal modulus ,Bytes reverse order
unsigned char  Bmp1[]=
{
/*------------------------------------------------------------------------------
;  Width X height (pixels): 64X16
;CHECKSTEP 1
------------------------------------------------------------------------------*/
 0x0, 0x3c, 0xc, 0xf3, 0xf, 0xf, 0xc3, 0xf0, 0xf0, 0x3f, 0xff, 0xfc, 0x0, 0x0, 0xc, 0x0, 0x0, 
 0xc3, 0xc, 0x33, 0xc0, 0x30, 0xc3, 0xc, 0x3, 0x3, 0x3, 0xc, 0x3, 0x0, 0xf, 0x0, 0x0, 0x3, 0xc,
 0x33, 0xc0, 0x0, 0x33, 0xc, 0x0, 0x3, 0x3, 0xc, 0x3, 0x0, 0xc, 0x0, 0x0, 0x3, 0xfc, 0xf3, 0xc0,
 0x0, 0xf, 0xf0, 0x0, 0x3, 0xf, 0xfc, 0x0, 0x0, 0xc, 0x0, 0x0, 0x3, 0xc, 0x33, 0xc0, 0x0, 0x33, 
 0x0, 0x3, 0x3, 0x3, 0xc, 0x0, 0x0, 0xc, 0x0, 0x0, 0xc3, 0xc, 0x33, 0xc0, 0x30, 0xc3, 0xc, 0x3,
 0x3, 0x3, 0xc, 0x0, 0x0, 0xc, 0x0, 0x0, 0x3c, 0xc, 0xf3, 0xf, 0xf, 0xc3, 0xf0, 0x0, 0x3, 0xff, 
 0xc, 0x0, 0xc0, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 
  
};

unsigned char  Bmp21[]=
{
/*------------------------------------------------------------------------------
;  Width X height (pixels): 16X16
;checkstep7_fs
------------------------------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f, 0xfc, 0x30, 0xcc, 0x3c, 0x30, 0x0, 0xf0, 0xc0, 0x30, 0xcc, 0x3, 0x0, 0x0, 0x0, 0x0, 0x3, 0x3c, 0x30, 0xcc, 0x3c, 0x30, 0x0, 0x0, 0xc3, 0x30, 0xcc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0xcc, 0x30, 0xcc, 0x30, 0x30, 0x0, 0xc, 0xc3, 0x30, 0xcc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0xc, 0xc3, 0xc3, 0x30, 0x30, 0x0, 0xf0, 0xf0, 0xf3, 0xc3, 0x3f, 0x0, 0x0 

};


unsigned char *Bmp_tab[]={Bmp1, Bmp2, Bmp3, Bmp4, Bmp5, Bmp6, Bmp7, Bmp8, Bmp9, Bmp10, Bmp11, Bmp12, Bmp13, Bmp14, Bmp15, Bmp16, Bmp17, Bmp18, Bmp19, Bmp20, Bmp21}; 
unsigned int Bmp=21;


void shiftOut(unsigned char dataOut)
{
  
    for(int i=0;i<=7;i++)
   {
      PORTB &=~(1<<(6));//PB6 = 0 equate digitalWrite(clockPin,LOW);
       
     if(dataOut & (0x01<<i))  PORTB |=1<<(5); //PB5=1 equate digitalWrite(dataPin,HIGH);
     else  PORTB &=~(1<<(5));//PB5 = 0 equate digitalWrite(clockPin,LOW);
     
     PORTB |=1<<(6);//PB6=1 equate digitalWrite(clockPin,HIGH);
     
   }
}

//Combine 2 bits/pixel to 1 bits/pixel 
unsigned char Combine_2BitsTo1Bit(unsigned char num,unsigned char *BMP)
{
  
  unsigned char Col_num_tem_1;
  unsigned char Col_num_tem_2;
  unsigned int Col_num_tem = 0;
  unsigned char i=0;
  unsigned char Col_num_1bit = 0x00;
  
  Col_num_tem_1 = *(BMP+num);
  Col_num_tem_2 = *(BMP+num+1);
  
  Col_num_tem = Col_num_tem_1;
  Col_num_tem |= (Col_num_tem_2 << 8);
  
 for(i=0;i<8;i++)
  {   
    if(Col_num_tem&(0x0003<<i*2)) Col_num_1bit |= (0x01<<i);
  }
  return ~Col_num_1bit;
}

//display one picture 
void display_martix(unsigned char *BMP)
{
  //Display count
  unsigned int dis_cnt=analogRead(sensorPin1)/1.5;  //POT adjustment
  unsigned int i;
  
   for(i=0;i<dis_cnt*16;i++)
   {



     
     digitalWrite(en_74138, HIGH);//Turn off display
  
     //Col scanning
    shiftOut(Col_num_1);
    shiftOut(Col_num_2);
    shiftOut(Col_num_3);
    shiftOut(Col_num_4);
    shiftOut(Col_num_5);
    shiftOut(Col_num_6);
    shiftOut(Col_num_7);
    shiftOut(Col_num_8);
  
    digitalWrite(latchPin, LOW);
    digitalWrite(latchPin, HIGH);
  
    //Row scanning
    // AVR Port Operation 
    //PORTD = ((ROW_xPixel << 3 ) & 0X78) | (PORTD & 0X87);//Write PIN 3 4 5 6 la_74138 lb_74138 lc_74138 ld_74138
    PORTE = ((ROW_xPixel << (5-0) ) & 0X20) | (PORTE & 0XDF);////Write PIN 3(PE5) la_74138
    PORTG = ((ROW_xPixel << (5-1) ) & 0X20) | (PORTG & 0XDF);////Write PIN 4(PG5) lb_74138
    PORTE = ((ROW_xPixel << (3-2) ) & 0X08) | (PORTE & 0XF7);////Write PIN 5(PE3) lc_74138
    PORTH = ((ROW_xPixel << (3-3) ) & 0X08) | (PORTH & 0XF7);////Write PIN 5(PH3) ld_74138
     
    digitalWrite(en_74138, LOW);//Turn on display
  

     
    if(ROW_xPixel==15) ROW_xPixel=0; else ROW_xPixel++;
  
   /*
   // Single color,1 bits/pixel
   Col_num_1=~BMP[(COL_PIXEL/8)*ROW_xPixel];
   Col_num_2=~BMP[(COL_PIXEL/8)*ROW_xPixel+1];
   Col_num_3=~BMP[(COL_PIXEL/8)*ROW_xPixel+2];
   Col_num_4=~BMP[(COL_PIXEL/8)*ROW_xPixel+3];
   Col_num_5=~BMP[(COL_PIXEL/8)*ROW_xPixel+4];
   Col_num_6=~BMP[(COL_PIXEL/8)*ROW_xPixel+5];
   Col_num_7=~BMP[(COL_PIXEL/8)*ROW_xPixel+6];
   Col_num_8=~BMP[(COL_PIXEL/8)*ROW_xPixel+7];
 */
   
   //Single color,2 bits/pixel 
   Col_num_1 = Combine_2BitsTo1Bit((COL_PIXEL/8)*ROW_xPixel*2,BMP);
   Col_num_2 = Combine_2BitsTo1Bit((COL_PIXEL/8)*ROW_xPixel*2+2,BMP);
   Col_num_3 = Combine_2BitsTo1Bit((COL_PIXEL/8)*ROW_xPixel*2+4,BMP);
   Col_num_4 = Combine_2BitsTo1Bit((COL_PIXEL/8)*ROW_xPixel*2+6,BMP);
   Col_num_5 = Combine_2BitsTo1Bit((COL_PIXEL/8)*ROW_xPixel*2+8,BMP);
   Col_num_6 = Combine_2BitsTo1Bit((COL_PIXEL/8)*ROW_xPixel*2+10,BMP);
   Col_num_7 = Combine_2BitsTo1Bit((COL_PIXEL/8)*ROW_xPixel*2+12,BMP);
   Col_num_8 = Combine_2BitsTo1Bit((COL_PIXEL/8)*ROW_xPixel*2+14,BMP);
   
  }
  
}

void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));
  
  
  pinMode(latchPin,OUTPUT);
  pinMode(clockPin,OUTPUT);
  pinMode(dataPin,OUTPUT);
  
  pinMode(en_74138,OUTPUT);
  pinMode(la_74138,OUTPUT);
  pinMode(lb_74138,OUTPUT);
  pinMode(lc_74138,OUTPUT);
  pinMode(ld_74138,OUTPUT);
  
  
  digitalWrite(en_74138, LOW);


  
  
}




void loop()
{

  display_martix(Bmp1);
 display_martix(Bmp2);
 display_martix(Bmp3);
 display_martix(Bmp4);
 display_martix(Bmp5);
 display_martix(Bmp6);
 display_martix(Bmp7);
 display_martix(Bmp16);
 display_martix(Bmp17);
 display_martix(Bmp18);
 display_martix(Bmp19);
 display_martix(Bmp20);
 display_martix(Bmp21); 
 

 
}

I don't know what are you trying to achieve nor what the sketch does (you didn't tell it) and I don't know what you mean by "it will not work" (you didn't tell what you expect and what it does instead), and the sketch it's not compiling too...

...nonetheless I'll go for a guess : D

If what are you looking for is to draw stuff on lcd, and if the stuff is based on a random number selection you believe to get but don't (getting always the same pattern...?), then the problem could be the seed you're setting here:

randomSeed(analogRead(0));

I guess you know, but to get an almost real random number, analogRead function has to read the value of an unconnected pin. Maybe the pin number 0 isn't. Ref

Sorry, about that. The current sketch is a message board. It will display 21 different messages that were drawn using matrix design software. The speed the messages show are adjustable using a potentiometer. The goal, is to make the messages display random. Also, this is an LED matrix.

Sorry, for some reason I read LCD instead of LED.
By the way, what does your program do instead of randomly display your messages? It displays always the same? It doesn’t display messages at all? And have you tried to change pin to read?

You need to place all your BmpX in a two dimensional array. Next you can use a random number as an index to select one of them and "play" that.

Looking a bit more at your code, you already seem to have that in place in a slightly different way using the below line

unsigned char *Bmp_tab[] = {Bmp1, Bmp2, Bmp3, Bmp4, Bmp5, Bmp6, Bmp7, Bmp8, Bmp9, Bmp10, Bmp11, Bmp12, Bmp13, Bmp14, Bmp15, Bmp16, Bmp17, Bmp18, Bmp19, Bmp20, Bmp21};

I can not test but below should do what your current loop() code does

void loop()
{
  static int counter = 0;

  // display current message
  display_martix(Bmp_tab[counter]);

  // increment counter for next time
  counter++;
  // reset counter when we reach end of Bmp_tab array
  if(counter >= sizeof(Bmp_tab] / sizeof(Bmp_tab[0]))
  {
    counter = 0;
  }
}

Once that works, you can simply replace counter in the call to display_martix by a random number.

Thank you very much Sterretje, your piece of code slipped right in there, and worked, but I don't think that's the direction I need to go to display everything in a random sequence. I tried several different modifications to implement a randomSeed and they all affected the outcome negatively. It really amazes me, that you glanced at my code and were able to write that small piece, and it worked without having the board and display in front of you.

Bryan

No, it was not the solution for your problem but it was showing you how you can use your array of pointers to display a specific message; that specific in the above example was based on a counter.

You should post your attempt so we can look at it. You should also make clear what “they all affected the outcome negatively” means.

Anyway, this should be a loop that displays random messages.

void loop()
{
  // display random message
  display_martix(Bmp_tab[random(sizeof(Bmp_tab]) / sizeof(Bmp_tab[0]))];
}

Not compiled, not tested.

You might want to read up how to use randomSeed in e.g. setup() to make the start value a little less predictable.

However, randomSeed will not give a perfect initialisation. The The reliable but not very sexy way to seed random thread contains some example code to get a better randomSeed. Please read and understand the complete thread before using any of it.

The code in reply #6 also includes a way to make the seeding depending on your code. So different programs will result in different seeding.

reply #52 contains the latest version.

Thanks again sterretje for the help. I took a few minutes to plug in your line of code, unfortunately it came back with an error and I was short on time.

Matrix_LED_6416_MEGA_CHECKSTEPS:478: error: expected ‘]’ before ‘)’ token

display_martix(Bmp_tab[random)sizeof(Bmp_tab)] / sizeof(Bmp_tab[0]))];

Matrix_LED_6416_MEGA_CHECKSTEPS:478: error: invalid types ‘unsigned char* [21][]’ for array subscript

exit status 1
expected ‘]’ before ‘)’ token

I didn’t get much time to play with it since I have been working longer hours. My job gets in the way of my hobbies a lot lately. Hopefully this weekend I will be able to work on this headache, I mean project.

I also want to thank you for the article you sent on the better randomSeed. After lots of searches, I never came across that one, I am getting very excited to experiment using that on my last project. In fact, that may end up being my weekend project!!!

Please post your updated code that is based on my random example.

// Edit Never mind

Matrix_LED_6416_MEGA_CHECKSTEPS:478: error: expected ']' before ')' token

   display_martix(Bmp_tab[random[b][color=red])[/color][/b]sizeof(Bmp_tab)] / sizeof(Bmp_tab[0]))];

That is definitely wrong; my example also seemed to have a problem; below updated version

  display_martix(Bmp_tab[random(sizeof(Bmp_tab) / sizeof(Bmp_tab[0]))]);

Does compile ;)