Need help with random function

I want only 1 of the outputs (leds) to be on for 4 seconds at a time but right now they are all on at a reduced voltage as their brightness is really dim. I also plan on adding a 4 second break between each an led going low and another one going high.

Heres my code:

const int TLLED = 12; //Top left led
const int TRLED = 11; //Top right led
const int BLLED = 10; //Bottom left led
const int BRLED = 9;  //Bottom right led
const int TLVS = 8;   //Top left vibration sensor
const int TRVS = 7;   //Top right vibration sensor
const int BLVS = 6;   //Bottom left vibration sensor
const int BRVS = 5;   //Bottom right vibration sensor
unsigned long previousMillis = 0;
unsigned long interval = 4000;
unsigned long currentMillis = 0;
const int timer = 100;
int ledON = currentMillis;
int valTL = 0 ;
int valTR = 0;
int valBL = 0;
int valBR = 0;
long randNumber;


void setup()
{
  Serial.begin(9600); 
  randomSeed(analogRead(0));
  pinMode(TLLED, OUTPUT);
  pinMode(TRLED, OUTPUT);
  pinMode(BLLED, OUTPUT);
  pinMode(BRLED, OUTPUT);
  pinMode(TLVS, INPUT);
  pinMode(TRVS, INPUT);
  pinMode(BLVS, INPUT);
  pinMode(BRVS, INPUT);
}

void loop()
{
 Serial.println(randNumber);
 int RandNumber = random(9, 13);
 digitalWrite(RandNumber, HIGH); // Turn on an LED between pin 9-12
 unsigned long previousMillis = millis ();
 if(currentMillis == previousMillis + 4000 || valTL == digitalRead(TLVS) || valTR == digitalRead(TRVS) || valBL == digitalRead(BLVS) || valBR == digitalRead(BRVS)); 
 digitalWrite(RandNumber, LOW);
  }

So how do I go from all leds on to just 1?

 Serial.println(randNumber);

Why print a value that never changes? Note that randNumber and RandNumber aren't the same variables. If you meant for this to be RandNumber, why print it before you get a value for it?

 unsigned long previousMillis = millis ();
 if(currentMillis == previousMillis + 4000 || valTL == digitalRead(TLVS) || valTR == digitalRead(TRVS) || valBL == digitalRead(BLVS) || valBR == digitalRead(BRVS));

99% of the time, an if statement with a semi colon at the end of it is pointless. I suspect you're looking for a while loop, not an if statement. The first condition, however, will be pointless, because the value of currentMillis is never updated (Why am I getting Deja Vu when I say that?). Since currentMillis is never updated from it's initial value of zero, you can rewrite that condition as

if(previousMillis == -4000 ...)

which is complete nonsense.

So my random function that I'm trying to get going will only make pin 9 go high? Any ideas why...

const int TLLED = 12; //Top left led
const int TRLED = 11; //Top left led
const int BLLED = 10; //Top left led
const int BRLED = 9; //Top left led
const int TLVS = 8;   //Top left vibration sensor
const int TRVS = 8;   //Top left vibration sensor
const int BLVS = 8;   //Top left vibration sensor
const int BRVS = 8;   //Top left vibration sensor
unsigned long previousMillis = 0;
unsigned long interval = 4000;
unsigned long currentMillis = 0;
const int timer = 100;
int ledON = currentMillis;
int valTL = 0 ;
int randNumber = 0;


void setup()
{
  Serial.begin(9600); 
  randomSeed(analogRead(0));
  pinMode(TLLED, OUTPUT);
  pinMode(TRLED, OUTPUT);
  pinMode(BLLED, OUTPUT);
  pinMode(BRLED, OUTPUT);
  pinMode(TLVS, INPUT);
  pinMode(TRVS, INPUT);
  pinMode(BLVS, INPUT);
  pinMode(BRVS, INPUT);
}

void loop()
{
 Serial.println(randNumber);
 int randNumber = random(1, 4);
 Serial.println(randNumber);
 // digitalWrite(randNumber, HIGH); // Turn on an LED between pin 9-12
 {
  if (randNumber = 1) 
  digitalWrite (TLLED, HIGH);
  digitalWrite (TRLED, LOW);
  digitalWrite (BLLED, LOW);
  digitalWrite (BRLED, LOW);
 }
 {
  if (randNumber = 2) 
  digitalWrite (TRLED, HIGH);
  digitalWrite (TLLED, LOW);
  digitalWrite (BLLED, LOW);
  digitalWrite (BRLED, LOW);
 }
 {
  if (randNumber = 3) 
  digitalWrite (BLLED, HIGH);
  digitalWrite (TRLED, LOW);
  digitalWrite (TLLED, LOW);
  digitalWrite (BRLED, LOW);
 }
 {
  if (randNumber = 4) 
  digitalWrite (BRLED, HIGH);
  digitalWrite (TRLED, LOW);
  digitalWrite (BLLED, LOW);
  digitalWrite (TLLED, LOW);
 }
 //unsigned long previousMillis = millis ();
// if(currentMillis == previousMillis + 4000 || valTL == digitalRead(TLVS) || valTR == digitalRead(TRVS) || valBL == digitalRead(BLVS) || valBR == digitalRead(BRVS)); 
// digitalWrite(randNumber, LOW);
  }

Jus two = in an if statement.
How is it wired, what resistor values are you using with your LEDs?

Ok thanks and the leds are in parallel with the pins. Not sure resistor size as they were prewired, they're rated for 3-6V

I tried the == in the if statement but that made the leds on pin 12-10 really dim and the led on pin 9 was right off

Weaver14:
Ok thanks and the leds are in parallel with the pins. Not sure resistor size as they were prewired, they're rated for 3-6V

No they are not in parallel. Either one end is connected to +5V and the other to the pin or one end is connected to ground and the other to the pin.
Anyway they might look like they are off when you get the if statement right.

So keep the == in you need it. But you need some sort of delay because you are turning LEDs on at random very rapidly.

Ya my bad the + side of the leds are connected to the pin and the other side to ground.

Here's where I'm at now, the leds come on randomly which is great and stay on for 4 seconds which is what I wanted but how do I speed up the time between random numbers being generated/ leds going ?

const int TLLED = 12; //Top left led
const int TRLED = 11; //Top right led
const int BLLED = 10; //Bottom left led
const int BRLED = 9; //Bottom right led
const int TLVS = 8;   //Top left vibration sensor
const int TRVS = 8;   //Top right vibration sensor
const int BLVS = 8;   //Bottom left vibration sensor
const int BRVS = 8;   //Bottom right vibration sensor
unsigned long previousMillis = 0;
unsigned long interval = 4000;
unsigned long currentMillis = 0;
const int timer = 100;
int ledON = currentMillis;
int valTL = 0 ;
int randNumber = 0;
int then; 
 


void setup()
{
  Serial.begin(9600); 
  randomSeed(analogRead(0));
  pinMode(TLLED, OUTPUT);
  pinMode(TRLED, OUTPUT);
  pinMode(BLLED, OUTPUT);
  pinMode(BRLED, OUTPUT);
  pinMode(TLVS, INPUT);
  pinMode(TRVS, INPUT);
  pinMode(BLVS, INPUT);
  pinMode(BRVS, INPUT);
}

void loop()
{
 Serial.println(randNumber);
 int randNumber = random(1, 4);
 Serial.println(randNumber);
 // digitalWrite(randNumber, HIGH); // Turn on an LED between pin 9-12
 {
  if (randNumber == 1) 
  digitalWrite (TLLED, HIGH);
  digitalWrite (TRLED, LOW);
  digitalWrite (BLLED, LOW);
  digitalWrite (BRLED, LOW);
  delay(4000);
  then; digitalWrite (TLLED, LOW);
 }
 {
  if (randNumber == 2) 
  digitalWrite (TRLED, HIGH);
  digitalWrite (TLLED, LOW);
  digitalWrite (BLLED, LOW);
  digitalWrite (BRLED, LOW);
  delay(4000);
  then; digitalWrite (TRLED, LOW);
 }
 {
  if (randNumber == 3) 
  digitalWrite (BLLED, HIGH);
  digitalWrite (TRLED, LOW);
  digitalWrite (TLLED, LOW);
  digitalWrite (BRLED, LOW);
  delay(4000);
  then; digitalWrite (BLLED, LOW);
 }
 {
  if (randNumber == 4) 
  digitalWrite (BRLED, HIGH);
  digitalWrite (TRLED, LOW);
  digitalWrite (BLLED, LOW);
  digitalWrite (TLLED, LOW);
  delay(4000);
  then; digitalWrite (BRLED, LOW);
 }
 //unsigned long previousMillis = millis ();
// if(currentMillis == previousMillis + 4000 || valTL == digitalRead(TLVS) || valTR == digitalRead(TRVS) || valBL == digitalRead(BLVS) || valBR == digitalRead(BRVS)); 
// digitalWrite(randNumber, LOW);
  }

Just stick in a delay for a test.
Then doing it for real look at the blink without delay example and put your random code in place of the blink bit.

Blink without delay can edit the time between random numbers generated?

The rate at which the LEDs are turned on/off depends on the value in delay(). If you want it to be faster, reduce the number passed to it. You could also use the Blink Without Delay, to only run that code every X seconds.

I have the delay on turning the leds off at 4000 so they are on for 4 second which is what I'm after but the time from when one turns off to when another turns on seems to be random? And I'm not very familiar with the blink without delay function.

  then; digitalWrite (TLLED, LOW);

Whatever you think this "then" is doing, it's not.

If you want there to be a delay between when the LED is turned off until the next one is turned on, put a delay after all of the if statements, before loop() ends.

Weaver14:
Blink without delay can edit the time between random numbers generated?

In the BlinkWithoutDelay example there is a variable called interval. This can, of course, be changed by the program if you want/need.

Weaver14:
I have the delay on turning the leds off at 4000 so they are on for 4 second which is what I'm after but the time from when one turns off to when another turns on seems to be random? And I'm not very familiar with the blink without delay function.

If two successive calls to the random number generator produce the same number it will look like there has been no change.

I added the delays after the if statements but the break between two leds was still random. Should I look into blink without delay then?

const int TLLED = 12; //Top left led
const int TRLED = 11; //Top right led
const int BLLED = 10; //Bottom left led
const int BRLED = 9; //Bottom right led
const int TLVS = 8;   //Top left vibration sensor
const int TRVS = 8;   //Top right vibration sensor
const int BLVS = 8;   //Bottom left vibration sensor
const int BRVS = 8;   //Bottom right vibration sensor
unsigned long previousMillis = 0;
unsigned long interval = 4000;
unsigned long currentMillis = 0;
const int timer = 100;
int ledON = currentMillis;
int valTL = 0 ;
int randNumber = 0;
int then; 
 
void setup()
{
  Serial.begin(9600); 
  randomSeed(analogRead(0));
  pinMode(TLLED, OUTPUT);
  pinMode(TRLED, OUTPUT);
  pinMode(BLLED, OUTPUT);
  pinMode(BRLED, OUTPUT);
  pinMode(TLVS, INPUT);
  pinMode(TRVS, INPUT);
  pinMode(BLVS, INPUT);
  pinMode(BRVS, INPUT);
}

void loop()
{
 Serial.println(randNumber);
 int randNumber = random(1, 4);
 Serial.println(randNumber);
  {
  if (randNumber == 1) 
  digitalWrite (TLLED, HIGH);
  digitalWrite (TRLED, LOW);
  digitalWrite (BLLED, LOW);
  digitalWrite (BRLED, LOW);
  delay(4000);
   }
   {
  if (randNumber == 2) 
  digitalWrite (TRLED, HIGH);
  digitalWrite (TLLED, LOW);
  digitalWrite (BLLED, LOW);
  digitalWrite (BRLED, LOW);
  delay(4000);
  }
  {
  if (randNumber == 3) 
  digitalWrite (BLLED, HIGH);
  digitalWrite (TRLED, LOW);
  digitalWrite (TLLED, LOW);
  digitalWrite (BRLED, LOW);
  delay(4000);
  }
 
  {
  if (randNumber == 4) 
  digitalWrite (BRLED, HIGH);
  digitalWrite (TRLED, LOW);
  digitalWrite (BLLED, LOW);
  digitalWrite (TLLED, LOW);
  delay(4000);
  }
  }

I bet it's not random. I bet the delay will depend on which LED is lighting up. They delay between 4 going off and 1 going on, for example, will be much less than 1 and 2. Why are your delay's outside of your if statements now? Try spending a bit more time debugging, and less time taking shots in the dark without understanding what you're doing.

I posted the wrong code, but I edited it. That was one I was messing around with. Alright I'll look into troubleshooting it more.

  digitalWrite (TRLED, HIGH);
  digitalWrite (TLLED, LOW);
  digitalWrite (BLLED, LOW);
  digitalWrite (BRLED, LOW);
  digitalWrite (TRLED, LOW);

Turn the LED on, and then microseconds later, turn it back off. Hardly seems like it's worth it to turn it on.

edited it again, sorry for all the edits arrch. Having a tough time with this right now. The one up now should be the correct one that I have so far.