I have a project where I want to use the random function for a time interval (millis)
My line: randTime = random(1000, 5000);
So it should theoretically give me a range between 1000 and 4999 but it only gives me something between 1000 and 1050.
I wrote a seperate, small code just to try the function seperatly and it worked perfect.
I also already tried just using randTime = random(1, 5); and multiplying it by 1000 later but that just gives me 0.
Is there anything that might affect the random function if its embedded in a fairly complex code?
Ok! I removed all the unrelated parts of the code and codetags.
The issue still persists so my fault has to be somewhere within this code.
You should be able to upload it to an UNO directly and get numbers between 1000 and 5000 on the serial monitor in the given time interval (between 1 and 5 secs)
I know it's a complete mess of a code i'm only a beginner.. sorry about that.
//GENERAL INTS
const int LED = 3;
const int brightpot = A0;
const int speedpot = A2;
const int button = 7;
const int maxout = 255; // max pwm wert für LED's
const int ledmin = 20; //min pwm wert für LED's
int brightness = 0;
int counter = 1;
long spd;
long speedmin = 5;
long speedmax = 500;
int flashspd;
int flashmin = 1;
int flashmax = 20;
const float pulsemin = 1;
const float pulsemax = 100;
float pulsespd;
float pulsespd2 = 0;
float in;
float out;
float pulsebrt1;
float pulsebrt;
int mode = 5;
int flashstate = 0;
int buttonState = 0;
int lastButtonState = 0;
unsigned long previousMillis = 0;
const long interval = 5000;
long randSpd;
long randMode;
long randTime; // Time that I need random between 1 and 5 seconds
long randBrt;
void setup() {
pinMode(button, INPUT);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
int speedin = analogRead(speedpot);
buttonState = digitalRead(button);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
mode++;
}
}
lastButtonState = buttonState;
if (mode >= 6) {
mode = 1;
}
if (mode == 5) {
rndm();
}
else {
int brightnessin = analogRead(brightpot);
brightness = map(brightnessin, 0, 1023, ledmin, maxout);
spd = map(speedin, 0, 1023, speedmin, speedmax);
flashspd = map(speedin, 0, 1023, flashmin, flashmax);
/* if (mode == 1) {
on();
}
if (mode == 2) {
blnk();
}
if (mode == 3) {
flash();
}
if (mode == 4) {
pulse();
}
*/
}
}
void rndm () {
int speedin = analogRead(speedpot);
int brightnessin = analogRead(brightpot);
randTime = random(1000, 5000); //random function that gives me only a range between ca. 1000 and 1050
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= randTime) { // Function that needs to be called in between 1 and 5 seconds
previousMillis = currentMillis;
randSpd = map(speedin, 0, 1023, speedmin, speedmax);
randBrt = map(brightnessin, 0, 1023, ledmin, maxout);
spd = random(speedmin, randSpd);
brightness = random(ledmin, randBrt);
randMode = random(1, 5);
// following is unrelated code that i removed for simplification
/* if (randMode == 1) {
on();
}
if (randMode == 2) {
blnk();
}
if (randMode == 3) {
flash();
}
if (randMode == 4) {
pulse();
}
*/
Serial.print("Random time:");
Serial.println(randTime);
/* Serial.print(" brighness:");
Serial.print(brightness);
Serial.print(" speedin:");
Serial.print(speedin);
Serial.print(" spd:");
Serial.print(spd);
Serial.print(" randmode:");
Serial.print(randMode);
Serial.print(" ");
Serial.print("mode: ");
Serial.println(mode);
*/
}
}