每次arduino启动时如何防止颜色数组成为相同的随机数组
how to prevent an array of colors from being the same random array every time the arduino starts
我有一个存储一些 uint32_t neopxiel 颜色的数组:红色、绿色、蓝色和黄色:
uint32_t colorRed = pixels.Color(255, 0, 0);
uint32_t colorGreen = pixels.Color(0, 150, 0);
uint32_t colorBlue = pixels.Color(0, 255, 255);
uint32_t colorYellow = pixels.Color(255, 255, 0);
uint32_t colorArray[4] = {colorRed, colorGreen, colorBlue, colorYellow};
然后我想制作一个数组,每次 arduino 启动时以随机顺序保存这些颜色。
uint32_t patternArray[4] = {colorArray[random(4)], colorArray[random(4)], colorArray[random(4)], colorArray[random(4)]};
这似乎对这 4 种颜色进行了随机排序,但每次 arduino 启动时都是相同的 "random" 顺序(黄色、绿色、绿色、蓝色)。
每次arduino启动时如何使随机顺序不同?
我已经尝试声明一个占位符数组并在大多数论坛帖子建议的 randomSeed(0) 之后初始化它,但我似乎无法获得正确的语法来声明数组,然后再将值赋给它:
//uint32_t patternArray; //nope
//uint32_t patternArray[4]; //nope
setup(){
randomSeed(analogRead(0));
//patternArray = {colorArray[random(4)], colorArray[random(4)], colorArray[random(4)], colorArray[random(4)]}; //nope
patternArray[4] = {colorArray[random(4)], colorArray[random(4)], colorArray[random(4)], colorArray[random(4)]}; //nope
}
Reads the value from the specified analog pin. Arduino boards contain a multichannel, 10-bit analog to digital converter. This means that it will map input voltages between 0 and the operating voltage(5V or 3.3V) into integer values between 0 and 1023.
因此无论引脚 0 的电压电平如何,它始终相同,因此随机种子始终相同。
使用时间作为种子值可确保每次启动程序时都有不同的值,例如millis()
Returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.
或者,查看 randomSeed()
中的示例
Example Code
The code generates a pseudo-random number and sends the generated number to the serial port.
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
}
这几乎就是你所拥有的。也许做 Serial.begin()
是需要的技巧。
最终奏效的解决方案与@NathanOliver- Reinstate Monica 建议的类似。我以错误的方式分配数组值。此外,randomSeed(millis())
无效,但 randomSeed(analogRead(1))
有效,但是,random()
调用不能在设置方法中 - 它必须在循环中。
void setup() {
Serial.begin(115200);
randomSeed(analogRead(1));
}
void loop(){
for (int i=-0; i<totalPatternLength;i++){
int randIndex = random(totalNumColors);
patternArray[i] = colorArray[randIndex];
}
}
我有一个存储一些 uint32_t neopxiel 颜色的数组:红色、绿色、蓝色和黄色:
uint32_t colorRed = pixels.Color(255, 0, 0);
uint32_t colorGreen = pixels.Color(0, 150, 0);
uint32_t colorBlue = pixels.Color(0, 255, 255);
uint32_t colorYellow = pixels.Color(255, 255, 0);
uint32_t colorArray[4] = {colorRed, colorGreen, colorBlue, colorYellow};
然后我想制作一个数组,每次 arduino 启动时以随机顺序保存这些颜色。
uint32_t patternArray[4] = {colorArray[random(4)], colorArray[random(4)], colorArray[random(4)], colorArray[random(4)]};
这似乎对这 4 种颜色进行了随机排序,但每次 arduino 启动时都是相同的 "random" 顺序(黄色、绿色、绿色、蓝色)。
每次arduino启动时如何使随机顺序不同?
我已经尝试声明一个占位符数组并在大多数论坛帖子建议的 randomSeed(0) 之后初始化它,但我似乎无法获得正确的语法来声明数组,然后再将值赋给它:
//uint32_t patternArray; //nope
//uint32_t patternArray[4]; //nope
setup(){
randomSeed(analogRead(0));
//patternArray = {colorArray[random(4)], colorArray[random(4)], colorArray[random(4)], colorArray[random(4)]}; //nope
patternArray[4] = {colorArray[random(4)], colorArray[random(4)], colorArray[random(4)], colorArray[random(4)]}; //nope
}
Reads the value from the specified analog pin. Arduino boards contain a multichannel, 10-bit analog to digital converter. This means that it will map input voltages between 0 and the operating voltage(5V or 3.3V) into integer values between 0 and 1023.
因此无论引脚 0 的电压电平如何,它始终相同,因此随机种子始终相同。
使用时间作为种子值可确保每次启动程序时都有不同的值,例如millis()
Returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.
或者,查看 randomSeed()
Example Code
The code generates a pseudo-random number and sends the generated number to the serial port.
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
}
这几乎就是你所拥有的。也许做 Serial.begin()
是需要的技巧。
最终奏效的解决方案与@NathanOliver- Reinstate Monica 建议的类似。我以错误的方式分配数组值。此外,randomSeed(millis())
无效,但 randomSeed(analogRead(1))
有效,但是,random()
调用不能在设置方法中 - 它必须在循环中。
void setup() {
Serial.begin(115200);
randomSeed(analogRead(1));
}
void loop(){
for (int i=-0; i<totalPatternLength;i++){
int randIndex = random(totalNumColors);
patternArray[i] = colorArray[randIndex];
}
}