Hi, I´m trying to do a Formula 1 Grid Light to my RC cars.
I wanted to start it with a RF(433) controller purchased on ebay, to not inure racers, I try to descover a code to variate the turn off light, but i didn´t find anything like.
To turn on light in sequence is a little bit easy, but the rest....kkk
please help-me, look till I reach:
/*
Race Track grid light
From HugoYH
*/
void setup() {
// initialize the digital pin as an output.
pinMode(2, OUTPUT); //red Led
pinMode(4, OUTPUT); //red Led
pinMode(6, OUTPUT); //red Led
pinMode(8, OUTPUT); //red Led
pinMode(10, OUTPUT); //red Led
pinMode(11, INPUT); // RF controller button
}
void loop() //starts sequnce when the press the controller button
{
digitalWrite(2, HIGH); // red Led on for 1,5 second
delay(1500);
digitalWrite(4, HIGH); // red led on 1,5 second
delay(1500);
digitalWrite(6, HIGH); // red Led on for 1,5 second
delay(1500);
digitalWrite(8, HIGH); // red Led on for 1,5 sec
delay(1500);
digitalWrite(10, HIGH); // red Led on for 1,5 seconds
delay(1500);
//I want to turn all ligth, in variable time( 0,001 milisec to 2 sec)
digitalWrite(2, LOW); // LED turns off
delay(10);
digitalWrite(4, LOW); // LED turns off
delay(10);
digitalWrite(6, LOW); // LED turns off
delay(10);
digitalWrite(8, LOW); // LED turns off
delay(10);
digitalWrite(10, LOW); // LED turns off
delay(1000);
//stop sequence, start just when press the rf controller button
}
What is the desired outcome? I don't know what an F1 grid light does.
The lights should turn on one by one, and then all turn off really quickly?
And this should be repeated?
And nothing should happen until a button is pressed?
setup pins as inputs and outputs
set pins to initial values to turn off LEDs
initialise started variable to false
seed random number generator (http://arduino.cc/en/Reference/RandomSeed)
start of loop
if started is false
if input from RF module
turn on LEDs in sequence
generate a random number (http://arduino.cc/en/Reference/Random)
scale the random number to desired range (is the min time really 0.001 milliseconds ?)
delay for random time
set started variable to true to prevent the sequence happening again
end of if
end of if
end of loop
(1) Start by reading the RF module input and printing its value to the Serial monitor.
Now you know what to test for.
(2) Add an if test so that code is executed when the input is received and get it to generate a random number in the range that you want and output that to the Serial monitor.
Now you know that a suitable random number is being generated and that it changes each time the RF input is received.
(3) Add the started variable and its if test so that the RF input only triggers once.
Now you know that the code will only be triggered once.
(4) Add the code to turn on the LEDs in order (you already have this)
Now you know that the LEDs only come on when the first RF input is received.
(5) Add the code to turn off the LEDs after the random delay.
Nearly there !
(6) Test what happens if RF input is sent whilst the LEDs are being turned on
It shouldn't happen, but you never know.
(7) Take out, or comment out the Serial.print commands that you used for debugging.
Keep a copy of the code at each stage so that you can go back to it if you mess up whilst making the next change.
Once it works pat yourself on the back and, as suggested by AWOL, see if you can tidy up the code to make it more efficient. Even if this code does not need to use arrays you will need them in the future and you will also need to avoid using the delay() function at some time because it stops anything else happening. Think what would happen if the start had to be aborted whilst the LEDs were being turned on in sequence. You would need to react to a button press, but the current code would not let you read another button because of the blocking caused by using delay().
Think what would happen if the start had to be aborted whilst the LEDs were being turned on in sequence. You would need to react to a button press, but the current code would not let you read another button because of the blocking caused by using delay().
Again, and Again!
Thanks
Abort sequence.....GOOD thought!!, I have 4 button, I can't use one of for abort?
I will study each part of you scheme and write scripts!
but now thinking, instead of all led turned off untill press the button, I will have 5 green light turned on all time, then when i press the button, it will start the sequence, but its a second version of it!!!kk(thinking too on a buzzer, no??)
Abort sequence.....GOOD thought!!, I have 4 button, I can't use one of for abort?
Yes you can but if you use delay() to do the timing then while it is happening you cannot read the state of the abort button. The normal way round this is to use the millis() function to calculate the number of milliseconds that have passed since an action was started, such as turning on an LED, to determine whether enough time has passed before something else needs to be done, such as turning on the next LED, and if not go and do something else, such as reading the abort button, then go back and check the elapsed time again.
Look at the example BlinkWithoutDelay program in the IDE to see how to do it in practice but I strongly suggest that you get the version using delay() working first otherwise you may not see the wood for the trees if you have problems.