Random Colour Code

Hi everyone,

I've currently got an 8x8 RGB matrix running off an Rainbowduino Atmel 328 hooked up to an Arduino Uno Atmel 328. All is working well and it runs the demo codes fine. I've currently got it running the MeggyJr patch and playing the Random Colours code. Currently is initiates a random board of coloured cells, and then changes them randomly for a period of time whilst speeding up the frame rate. It's meant to have interactive controls as its a MeggJR code, but I'm not interested in this aspect so ignore them. I have altered it so that it doesn't speed up and stays at a constant frame rate. What I was hoping was if anyone could help me alter the code slightly. I can't see where you alter the frame rate speed. Secondly, I would like to run the code for a set period of time/frames, pause it, hold that pause for another set time, then re-run the code starting with a new random board etc. etc.

If anyone is able to help that would be fantastic. I have copied the code below, thanks in advance!

Dan


updated code below in response to Grumpy_Mike's comment

Look at your code,
is:-
while (i < smiley-cool {
arduino code. No it has been mangled by the forum. That is why there is the # icon.
Go back and edit your post, select the code part and hit the # icon. You might have to paste it in again. Then we can see what it actually is.

Sorry about that, didn’t notice it when I posted it the first time. Here it is again, but without the random smiley faces;

#include <MeggyJrSimple.h>    // Required code, line 1 of 2.
 
 
 

unsigned long lasttime;
int delayTime;
byte interactive;
byte ButtonsLast;
byte pause;
byte ColorRange; 
 
byte AuxLEDValue; 
 
 
 
 
void setup()                    // run once, when the sketch starts
{

  MeggyJrSimpleSetup();      // Required code, line 2 of 2.
 
  lasttime = millis();
  delayTime = 300;
  interactive = 0;   // No human contact yet.
  pause = 0;

  ColorRange = 14;
  
  
}  // End setup()


void loop()                     // run over and over again
{   
   
  unsigned long TempTime;

  byte i;
  byte j;
  int delayTemp;
 
 CheckButtonsPress(); 
 
 
    if (Button_B)   //"B" button            // Resume automatic speed ramps
      interactive = 1;       

    if (Button_A)   //"A" button            // Pause/unpause
    {interactive = 0; 
      pause = !pause;       
    }
    
   // if ((Button_Up) && (delayTime > 0))      // up button: Speed up
     {  delayTime -= 10;
      interactive = 1; 
     }
     
   // if ((Button_Down) &&  (delayTime < 1000))  // down button: Slow down
     { delayTime += 10; 
       interactive = 1; 
     }
     
    if ((Button_Left) && (ColorRange > 1))    // left button: reduce # of colors
     {interactive = 1; 
     ColorRange--;
     }
    if ((Button_Right) &&  (ColorRange < 14))  // right button: increase # of colors
      {interactive = 1; 
      ColorRange++; 
      }
      
 
  TempTime = millis();

  delayTemp = delayTime - 50;
  if (delayTemp < 0)
    delayTemp = 0;

  if  ((TempTime - lasttime) > delayTemp)
  {

    i = 0;
    while (i < 8) { 
      j = 0;
      while ( j < 8)
      {
        DrawPx(i,j, rand() % ColorRange);
 
        // Randomly pick colors from the color look up table!
        j++;
      }
      i++;
    }

    if (interactive == 0)
    { 
      if (--delayTime == 0)
          delayTime = 250;
    }

    AuxLEDValue++;
    
 SetAuxLEDs(AuxLEDValue);

    lasttime = TempTime;

    if (pause == 0)
       DisplaySlate();  
  }
  
}   // End loop()

It is hard to say anything because half the code is missing. The line:-
#include <MeggyJrSimple.h>
imports in another file with code in it and without seeing that and any other files that are included from that there is no way we can get the full picture.

code for <MeggyJrSimple.h>

#ifndef MeggyJrSimple_h
#define MeggyJrSimple_h
 
#include <MeggyJr.h>

MeggyJr Meg;
byte GameSlate[8][8];       
byte lastButtonState=0;

byte Button_A=0;		 
byte Button_B=0;
byte Button_Up=0;
byte Button_Down=0;
byte Button_Left=0;
byte Button_Right=0;
 
#define MeggyCursorColor   15,15,15				// You can define color constants like this.

//Color lookup Table
byte ColorTable[26][3] = 
{
  { MeggyDark  },  
  { MeggyRed  }  ,
  { MeggyOrange  },
  { MeggyYellow  },
  { MeggyGreen  },
  { MeggyBlue  } ,
  { MeggyViolet  },
  { MeggyWhite  },  
  { MeggyDimRed  },  
  { MeggyDimOrange  },  
  { MeggydimYellow  },  
  { MeggyDimGreen  },  
  { MeggydimAqua  },  
  { MeggyDimBlue  },  
  { MeggydimViolet  },   
  { MeggyCursorColor},      //Extra bright cursor position color (not white).
  {0,0,0},					//CustomColor0 (dark, by default)
  {0,0,0},			        //CustomColor1 (dark, by default)
  {0,0,0},					//CustomColor2 (dark, by default)
  {0,0,0},					//CustomColor3 (dark, by default)	
  {0,0,0},					//CustomColor4 (dark, by default)
  {0,0,0},					//CustomColor5 (dark, by default)
  {0,0,0},					//CustomColor6 (dark, by default)
  {0,0,0},					//CustomColor7 (dark, by default)
  {0,0,0},					//CustomColor8 (dark, by default)
  {0,0,0}					//CustomColor9 (dark, by default)
}; 

// Assign those colors names that we can use:
enum colors {
  Dark, Red, Orange, Yellow, Green, Blue, Violet, White, 
  DimRed,DimOrange,DimYellow,DimGreen,DimAqua,DimBlue,DimViolet,FullOn,
    CustomColor0, CustomColor1, CustomColor2, CustomColor3, CustomColor4,  
	CustomColor5, CustomColor6, CustomColor7, CustomColor8, CustomColor9
	};     


  
void CheckButtonsDown()
	{ 
	//NOP
	}
	 
void CheckButtonsPress()
	{
	//NOP
	}
 
 
// Write a byte to the Auxiliary LED set at the top of the LED matrix display.  
void SetAuxLEDs(byte InputLEDs)
	{
	//NOP
	}

 

// Write a byte to the Auxiliary LED set at the top of the LED matrix display.  
// This version reverses bit order, so you can call it with an explicit binary number
void SetAuxLEDsBinary(byte n)
{
	//NOP
}

 


// Simple function to color in a pixel at position (x,y,color):
void DrawPx(byte xin, byte yin, byte color)
{
		GameSlate[xin][yin] = color;
}



// function to read color of pixel at position (x,y):
byte ReadPx(byte xin, byte yin)
{   
	return	GameSlate[xin][yin];
}


//Empty the Game Slate:
void ClearSlate(void)
{
  byte i;
  byte j;
  i = 0;
  while (i < 8) { 
    j = 0;
    while ( j < 8)
    {
      GameSlate[i][j] = 0;
      j++;
    }
    i++;
  }
}


// DisplaySlate() :: Write the Game Slate to the Display Memory it.
// This function looks up each color number (name) stored in the Game Slate,
// retreives its R,G,B components from the color table, and writes them to the
// R,G,B parts of the display memory.

void DisplaySlate (void) {	
  byte  j = 0; 
  while (j < 8) 
  {
    Meg.SetPxClr(j, 7, ColorTable[ GameSlate[j][7] ]);   
    Meg.SetPxClr(j, 6, ColorTable[ GameSlate[j][6] ]);  
    Meg.SetPxClr(j, 5, ColorTable[ GameSlate[j][5] ]);   
    Meg.SetPxClr(j, 4, ColorTable[ GameSlate[j][4] ]);  
    Meg.SetPxClr(j, 3, ColorTable[ GameSlate[j][3] ]);   
    Meg.SetPxClr(j, 2, ColorTable[ GameSlate[j][2] ]);  
    Meg.SetPxClr(j, 1, ColorTable[ GameSlate[j][1] ]);   
    Meg.SetPxClr(j, 0, ColorTable[ GameSlate[j][0] ]);  
    j++; 
  }  	 
}  
 #endif
  
 
  // Pre-defined sound divisors:
 
#define ToneB2		64783 

#define ToneC3		61157	
#define ToneCs3		57724
#define ToneD3		54485	
#define ToneDs3		51427
#define ToneE3		48541	
#define ToneF3		45816	
#define ToneFs3		43243
#define ToneG3		40816	
#define ToneGs3		38526
#define ToneA3		36363
#define ToneAs3		34323
#define ToneB3		32397
 
#define ToneC4		30578
#define ToneCs4		28862
#define ToneD4		27242
#define ToneDs4		25713
#define ToneE4		24270
#define ToneF4		22908
#define ToneFs4		21622
#define ToneG4		20408
#define ToneGs4		19263
#define ToneA4		18182 
#define ToneAs4		17161
#define ToneB4		16198 

#define ToneC5		15289
#define ToneCs5		14431
#define ToneD5		13626
#define ToneDs5		12857
#define ToneE5		12135
#define ToneF5		11454
#define ToneFs5		10811
#define ToneG5		10204
#define ToneGs5		9631
#define ToneA5		9091 
#define ToneAs5		8581
#define ToneB5		8099 

#define ToneC6		7645
#define ToneCs6		7215
#define ToneD6		6810
#define ToneDs6		6428
#define ToneE6		6067
#define ToneF6		5727
#define ToneFs6		5405
#define ToneG6		5102
#define ToneGs6		4816
#define ToneA6		4545 
#define ToneAs6		4290
#define ToneB6		4050 

#define ToneC7		3822
#define ToneCs7		3608
#define ToneD7		3406
#define ToneDs7		3214
#define ToneE7		3034
#define ToneF7		2863
#define ToneFs7		2703
#define ToneG7		2551
#define ToneGs7		2408
#define ToneA7		2273 
#define ToneAs7		2145
#define ToneB7		2025 

#define ToneC8		1911
#define ToneCs8		1804
#define ToneD8		1703
#define ToneDs8		1607
#define ToneE8		1517
#define ToneF8		1432
#define ToneFs8		1351
#define ToneG8		1276
#define ToneGs8		1204
#define ToneA8		1136 
#define ToneAs8		1073
#define ToneB8		1012 

#define ToneC9		956
#define ToneCs9		902
#define ToneD9		851
#define ToneDs9		803

  /*
 Sound output:
 
 Frequency is given by the following formula:
 
 f= 8 MHz/divisor, so divisor = 8 MHz/f.   (Round to nearest.)
 Maximum divisor: 65535, so min. frequency is 122 Hz
 
 Example: for 440 Hz, divisor = 18182
  
 */
  
   // "Cheater" functions
 // #define SoundOn()    Meg.SoundState(1)
 // #define SoundOff()   Meg.SoundState(0) 
 // #define MakingSound  (TCCR1B > 0)		
 
 #define SoundOn();    {}
 #define SoundOff();   {}
 #define MakingSound  false
   
 
 
 
 // other sound functions:
void Tone_Start(unsigned int divisor, unsigned int duration_ms)
{
	//NOP
}



#define Tone_Update(); {}			// For backwards compatibility.

/*
  // Stop the note if its completion time has come.
void Tone_Update(void)
{
 // Obsolete with current version of library; sounds stop automatically.
 // please remove this function from your program if it is there.
}
*/


void EditColor(byte WhichColor, byte RedComponent, byte GreenComponent, byte BlueComponent)
{
   ColorTable[WhichColor][0] = RedComponent;
   ColorTable[WhichColor][1] = GreenComponent;
   ColorTable[WhichColor][2] = BlueComponent;
 } 




void MeggyJrSimpleSetup(void) 
  {
		Meg = MeggyJr();  
		
	    lastButtonState = Meg.GetButtons();
		
  //		 Meg.StartTone(0, 0);
	//	 Tone_Update();
		 SoundOff();
  }

and code for <MeggyJr.h>

*/


#ifndef byte
#define byte uint8_t
#endif

#ifndef MeggyJr_h
#define MeggyJr_h

#include <inttypes.h>
#include <avr/interrupt.h>
#include <avr/io.h>

#define FPS 120						// Frames per second; value can be changed somewhat.
#define DISP_BUFFER_SIZE 192		// 8 rows * 24 bytes per row == 192. Don't mess with it. :)
#define MAX_BRIGHTNESS 15			// 16 steps of brightness per pixel

// Predefined Colors: 
#define MeggyDark      0,  0,   0
#define MeggyRed       6,  0,   0
#define MeggyOrange   12,  5,   0
#define MeggyYellow    7,  10,  0
#define MeggyGreen     0,  15,  0
#define MeggyBlue      0,   0,  5
#define MeggyViolet    8,   0,  4
#define MeggyWhite     3,  15,  2 
 
#define MeggyDimRed    1,  0,   0
#define MeggyDimGreen  0,  1,   0
#define MeggyDimBlue   0,  0,   1
#define MeggyDimOrange 1,  1,   0 
#define MeggydimYellow 1,  1,   0
#define MeggydimAqua   0,  3,   1
#define MeggydimViolet 2,  0,   1

//#define SoundEnabled (TCCR1B > 0)

// Optional timing test routine; can be used to measure
// duration of interrupt refresh routine.
//#define  timingtest 1


class MeggyJr
{
	public:
		MeggyJr(void);										// Constructor
		
		static byte MeggyFrame[DISP_BUFFER_SIZE];
		static byte AuxLEDs;
		
		static byte SoundEnabled;		static byte SoundAllowed;	 
		
	//	 static volatile byte ToneInProgress;		// Removed, v 1.3		 
	//	static unsigned long SoundStopTime;			// Removed, v 1.3
		
		void ClearMeggy (void);	
		void ClearPixel(byte x, byte y);
		byte GetButtons(void);

		byte GetPixelR(byte x, byte y);
		byte GetPixelG(byte x, byte y);
		byte GetPixelB(byte x, byte y);
		 
		void SetPxClr(byte x, byte y, byte rgb[3]);
		static byte *currentColPtr;
		static byte  currentCol;
		static byte  currentBrightness;

		void SoundState(byte t);
        void StartTone(unsigned int Tone, unsigned int duration)  ;  
//		void SoundCheck(void) ;  // End sound if it's time to do so.   // Removed, v 1.3

 		static unsigned int ToneTimeRemaining;			// new, v 1.3

#ifdef timingtest
	static unsigned int testTime;
#endif
	    
	private: 
		
};

#endif

I couldn’t find the files for the others included in this code above. Is this enough to get a rough idea?

It looks like:- delayTime = 300; is the thing that controls the frame rate, have you tried changing this?

Secondly, I would like to run the code for a set period of time/frames, pause it, hold that pause for another set time, then re-run the code starting with a new random board

It would be best if you gathered the stuff in the loop() into a function or two. Then in loop() keep calling those functions until another timer is up, then don't call them again until you press the button at which point you generate random numbers in your controlling parameters.

I tried out the delayTime value as it does indeed change the frame speed. Thanks for that.

In terms of writing functions in the loop cycle, I'm not entirely sure where I would start with that, or how it would fit in etc. Any ideas or samples you might be able to provide me with? Thanks for the info on this.

On a side note, I've noticed your from Manchester, where abouts? I'm living in Manchester at the moment for uni.

Cheers,

Dan

It is just what you did for the timing only another layer up. That is relegate everything in the loop() into a function, then call that function from the loop, the results are then no different. Then control the calling of that function for as long as the current time ( millis() timer) is less than a certain variable. When it is stop calling that function and chaneg some parameters. It is just like the blink without delay sketch but you are not blinking an LED but going through everything that is currently in the loop().

I live just north of Manchester beyond Ramsbottom. I will be down in the city tonight at Sound Control for the Mono gig.