Go Down

Topic: Random() (Read 1 time) previous topic - next topic

iluvplanes


PaulS

Quote
I used random(2,5) because those were the values I need.

Keep in mind that the random() function will never return the upper value of the range that you specify. If you want it to return 2, 3, 4, and 5, the upper limit needs to be 6.

iluvplanes


iluvplanes

Thanks for your help everyone, I got it to work now!
Here's the code if anyone else wants to know...
Code: [Select]

// Random LED lights code...

long randNumber1;
int val = 2;
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;


void setup() {               

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);     
  pinMode(led3, OUTPUT);     
  pinMode(led4, OUTPUT);     
  Serial.begin(9600);
  randomSeed(analogRead(1));
  //make sure pins are on low.
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);


}

// the loop routine runs over and over again forever:
void loop() {
  delay(3000);
 
  val = 1; 
  randNumber1 = random(2,6);
   

//First LED.
  digitalWrite(randNumber1, HIGH);
  delay(700);
  val = randNumber1;
  digitalWrite(randNumber1, LOW);
  //Serial.println(randNumber1);
  //Serial.println(val);
 
  //Check to see if randNum is same. If so, change it.
  randNumber1 = random(2,6);
  while (randNumber1 == val) {
    randNumber1 = random(2,6);
  }
  //write to 2nd LED
  digitalWrite(randNumber1, HIGH);
  delay(700);
  digitalWrite(randNumber1, LOW);
  val = randNumber1;


  //Check 
randNumber1 = random(2,6); 
  while (randNumber1 == val) {
    randNumber1 = random(2,6);
  }
  //Write to 3rd LED
  digitalWrite(randNumber1, HIGH);
  delay(700);
  digitalWrite(randNumber1, LOW);
    val = randNumber1;

 
  //Check
  randNumber1 = random(2,6);
  while (randNumber1 == val) {
    randNumber1 = random(2,6);
  }

  //Write to 4th LED.
  digitalWrite(randNumber1, HIGH);
  delay(700);
  digitalWrite(randNumber1, LOW);
    val = randNumber1;

 
delay(1000);
}

patduino

Hi iluvplanes,

Just a few coding observations that can make your code easier to read and easier to understand:

You have 2 delays, 3 sec at the beginning of your loop and 1 sec at the bottom of the loop, that can be combined into a single delay.

// Comments help to explain what you are doing. I had to read through the code to determine the following:

It appears that you have a loop cycle time of 6.8 seconds when you add all of the delays.  Within this time you would like to randomly flash some LEDs for 0.7 seconds.

From your code, it appears that you can flash an LED multiple times in the cycle--just not twice in a row, and that within a cycle it is ok if an LED doesn't flash at all. If not, then Nick's shuffle approach is the way to go.

That said... This was probably just supposed to be a fun activity and now that you've gotten it to work, you can move on to other things!  FUN, FUN, FUN.  If you're looking for something to tweak (and something to learn), maybe use potentiometers to vary the loop delay and on/off delays?
There are 10 types of people in the world, those that understand binary, and those that don't.

Go Up