[SOLVED]generating a random pattern only once per interval

Hi,

I'm trying to use random() to generate a random pattern on an LED display. But I am either getting one random pattern that never changes, or it will be constantly changing. I am trying to only generate one random pattern every time display4 advances one digit. Hopefully if you look at this code it will make sense. In this version it is generating constantly changing random patterns.

Any hints or help would be greatly appreciated.

Thanks, Nick

#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
//#include <TinyWireM.h> // Enable this line if using Adafruit Trinket, Gemma, etc.

#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

Adafruit_BicolorMatrix matrix3 = Adafruit_BicolorMatrix();



long previousMillis = 0; // last time a change happened
long interval = 200;
int display4 = 1;   // 2nd bi color display

void setup()
{
#ifndef __AVR_ATtiny85__
  Serial.begin(9600);
  Serial.println("7 Segment Backpack Test");
#endif
  
  matrix3.begin(0x73);
}


void loop()
{
  unsigned long currentMillis = millis();
//int rP = 50;


  if ( (currentMillis - previousMillis > interval) && (display4 < 5) )
  {
    previousMillis = currentMillis;   
    display4 = display4 + 1;
   
  }

  if ( (currentMillis - previousMillis > interval)  && (display4 > 5) )
  {
    previousMillis = currentMillis;   
  //  display1 = 1;
 // rP == random(128);  // I need this random number to change
  }

if (display4 >= 4) {
  display4 = 1;
}


 if (display4 == 1)
   {
      matrix3.clear();      // clear display
  matrix3.drawPixel(random(8), random(8), LED_GREEN);  // I don't want constant random numbers, just one per count
  matrix3.writeDisplay();  // write the changes we just made to the display
    
}

if (display4 == 2)
   {
      matrix3.clear();      // clear display
  matrix3.drawPixel(random(8), random(8), LED_YELLOW);  
  matrix3.writeDisplay();  // write the changes we just made to the display
    
}

if (display4 == 3)
   {
      matrix3.clear();      // clear display
  matrix3.drawPixel(random(8), random(8), LED_RED);  
  matrix3.writeDisplay();  // write the changes we just made to the display
    
}
if (display4 == 4)
   {
      matrix3.clear();      // clear display
  matrix3.drawPixel(random(8), random(8), LED_YELLOW);  
  matrix3.writeDisplay();  // write the changes we just made to the display
    
}

}

Why not put the random calculation immediately after the line that changes display4

display4 = display4 + 1;
// do the random stuff here

...R

First you must auto format your code! It hepl us (and you).

When you do layout your code you will see why it is changing one LED every time it gores through loop().

Mark

Robin2,

Wow that worked. I could have sworn I tried your suggestion, but I guess I didn't. But that works so thanks for the help. I'm putting the code below. I ended up using two variables so I could get a different x and y axis number.

What up Holmes4. I thought I formatted my code correctly. Is there something else I have to do?

Thanks, Nick

#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
//#include <TinyWireM.h> // Enable this line if using Adafruit Trinket, Gemma, etc.

#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

Adafruit_BicolorMatrix matrix3 = Adafruit_BicolorMatrix();



long previousMillis = 0; // last time a change happened
long interval = 200;
int display4 = 1;   // 2nd bi color display
int rP1 = 1;  // this will be a randomly generated number
int rP2 = 1; 

void setup()
{
#ifndef __AVR_ATtiny85__
  Serial.begin(9600);
  Serial.println("7 Segment Backpack Test");
#endif
  
  matrix3.begin(0x73);
}


void loop()
{
  unsigned long currentMillis = millis();
//int rP = 50;


  if ( (currentMillis - previousMillis > interval) && (display4 < 5) )
  {
    previousMillis = currentMillis;   
    display4 = display4 + 1;
    rP1 = random(8);
    rP2 = random(8);
    
   
  }

  if ( (currentMillis - previousMillis > interval)  && (display4 > 5) )
  {
    previousMillis = currentMillis;   
  //  display1 = 1;
 // rP == random(128);  // I need this random number to change
  }

if (display4 >= 4) {
  display4 = 1;
   rP1 = random(8);
}


 if (display4 == 1)
   {
      matrix3.clear();      // clear display
  matrix3.drawPixel(rP1, rP2, LED_GREEN);  // I don't want constant random numbers, just one per count
  matrix3.writeDisplay();  // write the changes we just made to the display
    
}

if (display4 == 2)
   {
      matrix3.clear();      // clear display
  matrix3.drawPixel(rP1, rP2, LED_YELLOW);  
  matrix3.writeDisplay();  // write the changes we just made to the display
    
}

if (display4 == 3)
   {
      matrix3.clear();      // clear display
  matrix3.drawPixel(rP1, rP2, LED_RED);  
  matrix3.writeDisplay();  // write the changes we just made to the display
    
}
if (display4 == 4)
   {
      matrix3.clear();      // clear display
  matrix3.drawPixel(rP1, rP2, LED_YELLOW);  
  matrix3.writeDisplay();  // write the changes we just made to the display
    
}

}

Ok,

under tools, I select auto format. Yes, i see it looks a little neater.

Thanks, Nick

void loop()
{
  unsigned long currentMillis = millis();
  //int rP = 50;


  if ( (currentMillis - previousMillis > interval) && (display4 < 5) )
  {
    previousMillis = currentMillis;   
    display4 = display4 + 1;

  }

  if ( (currentMillis - previousMillis > interval)  && (display4 > 5) )
  {
    previousMillis = currentMillis;   
    //  display1 = 1;
    // rP == random(128);  // I need this random number to change
  }

  if (display4 >= 4) {
    display4 = 1;
  }
  changePixel();
}

void changePixel(){

  if (display4 == 1)
  {
    matrix3.clear();      // clear display
    matrix3.drawPixel(random(8), random(8), LED_GREEN);  // I don't want constant random numbers, just one per count
    matrix3.writeDisplay();  // write the changes we just made to the display

  }

  if (display4 == 2)
  {
    matrix3.clear();      // clear display
    matrix3.drawPixel(random(8), random(8), LED_YELLOW);  
    matrix3.writeDisplay();  // write the changes we just made to the display

  }

  if (display4 == 3)
  {
    matrix3.clear();      // clear display
    matrix3.drawPixel(random(8), random(8), LED_RED);  
    matrix3.writeDisplay();  // write the changes we just made to the display

  }
  if (display4 == 4)
  {
    matrix3.clear();      // clear display
    matrix3.drawPixel(random(8), random(8), LED_YELLOW);  
    matrix3.writeDisplay();  // write the changes we just made to the display

  }

}

I put your code to change a pixel in a function of it's own (a very good way to use functions by the way) can you see why your display was changing so fast?

Mark