How to switch on 4 output pins randomly ?

Hi im working on a project where I need to switch 4 output pins (one at a time) randomly for 1 second each with a 1 second delay between each.
I have been looking at the random function , but that dosnt seem to give me what I want ?
Isuppose I could set max and min with a 4 point spread and then associate each number to an output pin ?
any help would be appreciated.
regards
Tony

You can, for example, put the numbers of the output pins to the array and than choose a pin by array index with random function.

Please show your efforts to write the code.

1 Like

That should work.
So what, exactly, did you do?
In what way(s), exactly, was it not "what you want"

BTW: remember that the random() function gives a pseudo random result ...

Hi,
What Arduino are you using?

If you use consective numbered pins, then you could set the random function to only work between the lowest and highest numbered pins.

This maay help;

If you can't use consecutive pins then @b707 suggestion of an array would be the best.

Thanks... Tom... :smiley: :+1: :coffee: :australia:

consider

#define ProjectName "How to switch on 4 output pins randomly"
//variables 
constexpr uint8_t OutputPins [] {9,10,11,12};
void setup()
{
  Serial.begin(115200);
  Serial.println(ProjectName);
  for (auto &OutputPin:OutputPins) pinMode(OutputPin,OUTPUT); 
}
void loop() 
{
  for (auto &OutputPin:OutputPins) digitalWrite(OutputPin,LOW); 
  digitalWrite(OutputPins[random(0,5)],HIGH);
  delay(100); 
}

Hi thanks for that I havent written any code as yet just trying to work out the best way.as you can probably tell im at only a beginners level !

will do when ive written it cheers

Simulate it... for free... on WOKWI.com

Here is an example of randomly selected pins running stepper motors.

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