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);
}
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?
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
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);
}
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.
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:
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 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.