Hi,
I am trying to randomly choose a value between 1 and 10 every 5 seconds without selecting any of the previous 3 values.
my idea is to save the previous values in a small ring buffer and choose random values and discard them if the values match the current values in the ring buffer.
however, this seems wasteful and could potentially get stuck. Currently the code doesn't work correctly anyway so I think its time to ask for help.
I cant think of a better way but know there must be one..
Any advice on improving my technique and achieving the desired result would be appreciated.
#include <FastLED.h>
// PATTERN SELECTION
int Pattern = 1; // Stores the current Pattern
int PatternMax = 10; // adjust this for the max number of patterns
int PatternMin = 1; // keep this 1 so patterns cycle in a loop
int PatternMaxRandomMode = 7; // stop the solid colours from apearing in the random and cycle modes. this is so patterns > PatternMaxRandomMode can be excluded from the random
int PrevPatterns[3]= {0,0,0};
int PrevPatternCount = 0;
// timer for pattern mode change
int Pattern_period = 5; // this is the time period between pattern changes
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); // start serial port
}
void loop() {
EVERY_N_SECONDS(Pattern_period){
PrevPatterns[PrevPatternCount] = Pattern; // save current patern
PrevPatternCount++; // increment count
if (PrevPatternCount >= sizeof(PrevPatterns)){
PrevPatternCount = 0; // loop array count
}
Serial.println(" ");
Serial.println("///////////////////////////////");
Serial.print("Previous Pattern 1 = ");
Serial.println(PrevPatterns[PrevPatternCount]);
Serial.print("Previous Pattern 2 = ");
Serial.println(PrevPatterns[PrevPatternCount-1]);
Serial.print("Previous Pattern 3 = ");
Serial.println(PrevPatterns[PrevPatternCount-2]);
if(Pattern == PrevPatterns[0]||Pattern == PrevPatterns[1]||Pattern == PrevPatterns[2]){
Pattern = random(PatternMin,PatternMaxRandomMode+1); // randomly choose Pattern after Pattern_period time can have same pattern more than once in a row... PatternMaxRandomMode
Serial.print("Chooseing Pattern = ");
Serial.println(Pattern);
}
Serial.print("Current Pattern = ");
Serial.println(Pattern);
}
}