Random selection of a defined group of numbers

Hello,

I am trying to modify the current code

<<
#define valve 4
#define ledPin 22
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(valve, OUTPUT);
pinMode(ledPin, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
flashLed(3000,20000);
for(int i = 1; i <= 10; i++) // repeat 10 times
{
digitalWrite(valve, HIGH); // turn the valve on
delay(200); // delay 200 ms
digitalWrite(valve, LOW); // turn the valve off
delay(3000); // delay 3000 ms
}
delay(60000); // delay 1 min

}
void flashLed(int timeOn, int timeDelay)
{
digitalWrite(ledPin, HIGH);
delay(timeOn);
digitalWrite(ledPin, LOW);
delay(timeDelay);
}

.....to get a code in which I have something like this

<<
#define valve 4
#define ledPin 22
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(valve, OUTPUT);
pinMode(ledPin, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
for(int i = 1; i <= 10; i++) // repeat 10 times
{
digitalWrite(valve, HIGH); // turn the valve on
delay(random(50,100,150,200)); // delay random in the range 50ms-100ms-150ms200ms
digitalWrite(valve, LOW); // turn the valve off
delay(random(3000,60000)); // delay random in the range of 3s-60s
}
delay(random(3000,60000)); // delay random in the range of 3s-60s

}
void flashLed(int timeOn, int timeDelay)
{
digitalWrite(ledPin, HIGH);
delay(timeOn);
digitalWrite(ledPin, LOW);
delay(timeDelay);
}

I am trying to understand:

1- what is the command to call randomly a defined series of numbers (in my case 50-100-150-200)?
2- If random(min,max) could be enough to instead call randomly numbers in a range.

I am trying to get a valve to open and close for a time variable each time ( 50-100-150-200 ms) and to do this at intervals (that go from 3s to 60s).

I hope someone could help me.

Thanks!

Put your code in code tags.

try this way:

    int temp = random(1,4);
    delay(temp *50); // delay random in the range 50ms-100ms-150ms200ms

RV mineirin

1 Like

int temp = random(1,4);
should be
int temp = random(1,5);

to get 1 to 4.

1 Like

Thanks!!!!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.