This project is pretty simple. It is a atmega 168 with the arduino bootloader running a lottery code i wrote. The lottery code has 6 user chosen lottery numbers in it. It uses the TrueRandom library to get true random numbers. The lottery in Florida, USA. Draws 6 numbers randomly in between 1 and 53. The program does the same. It comes up with 6 random numbers in between 1 and 53. It then checks to see if these match the users lottery numbers. If it does, then it lights the LED, if it doesnt then it goes back through the loop. The program makes sure not to match the same number more than once when it is checking the 6 numbers. The odds of winning this lottery is 1 in 20,358,520. My lotto numbers i am using are 7, 14, 17, 25, 32, 44.The program runs around 100,000 times a second. I have yet to see the light come on and do not know if i ever will :) and this is why i can not really shot a video of it. I will leave a comment if it ever comes on meaning that my program "won the lottery".
This is one very elaborate way to make an LED blink! And that’s what I love about this thing.
The blink is gonna be very short though. At this rate, it will blink once every 200 seconds for .001 millisecond. At an average. You could also see this as a PWM’ed LED with a duty cycle of 1:20 million and a very variable frequency.
In other words, you might as well unplug the Arduino and your audience would see the same thing. How about an LED that lights up and dies out over the next 100000 loops, using true PWM? That would introduce some level of persistence in the system.
The LED will not blink if it wins the lottery, the LED will stay lit until the arduino is turned off. That is because i would not be around to see a blink, but if i walk passed it and see that the LED is on, then i know that sometime ago it won the lottery. The night i made this, Telefox and I did some calculations in the shout box and determined that this thing will take a few weeks on average of running time to light up. Thats because the program runs much slower than i thought, it takes a few cycles for the library to come up with the random values and on top of that it takes the program 6 cycles to come up with 6 random numbers (this is how i wrote the program). So you would need the LED to light up and die out over a period of a few weeks now try and do that :). Why would we want persistence? As a viewer watching this simulation, there is always the same odds that it will light up so you may get lucky but you need the same luck that playing that lottery has :P. Being that it is random on start up and shut down, i am able to turn it off and it continues to be random when i turn it back on. Maybe what might be more interesting would be keeping the LED on and then turning it off when it wins the lottery. So basically you would have a LED night light that most likely would stay on unless it wins the Lottery.
Excuse me for trying to learn Arduino C without owning one. I may be wrong all along, but you are capable to explain this like nobody else.
I understand you designed the code to keep the LED on until you reset the Arduino. That makes sense for the reasons we both mentioned.
But reading the code, I cannot see the LED staying on. For the LED pin to go high, variable “match” must be 6. And at the end of every six draws (line 85), that var is reset to 0. Isn’t it? And the LED pin is reset to low at the start of every loop (line 31).
If the code must remember that you won the lottery at least once since boot up, you probably need an extra var. Something like “iwoniwoniwon”. And make your LED status depend on that one.
Or stop resetting the pin altogether.
Please don’t take my nitpicking as criticism. I am a student of this stuff, just like you are.
But you must remember “match” is reset after we check to see if we have won. If we have won then it stays in the while loop and lights the LED until it is turned off because in that while loop match will always = 6 because it is not changing the value of match. It never gets to reset “match” if it has won.
Another point i might add is that i have ran the same code but only doing lottery with 3 or 4 numbers and i get it to light up and stay lit. With 3 or 4 numbers it only takes at most 30 seconds to win.
I have thought of moving digitalWrite(LED, low); to void setup because it only needs to run that once.
That while(match == 6) got me! I discarded that as a funny way of writing if(match == 6). That was my mistake here.
The code effectively stops looping when it wins the lottery and keeps looping inside the while {}. I am so used to writing some sort of exit at that point that I never saw that.
"…The program makes sure not to match the same number more than once when it is checking the 6 numbers. The odds of winning this lottery is 1 in 20,358,520. My lotto numbers i am using are 7, 14, 17, 25, 32, 44.The program runs around 100,000 times a second…"
so, if I’m right, it should take this many seconds to find a match:
probpertest=1/20358520
tests_persec=100000
prob_persec=probpertest*test_persec=4.9119483E-04
so the number of seconds required for a probability=1 is
1/prob_persec=2035.852 seconds
=33 minutes.
so if your probability estimates are correct, it should only take about half an hour till the led comes on?
I believe how our lottery works is there are 53 balls with numbers 1-53, but thats it, once you draw a ball your chances are now 1/52 and then draw another ball takes that out of play so your chances come to 1/51. You can not have lottery numbers like 7 7 6 15 15 20, you do not repeat numbers in the big lottery, i think. Can some one tell me if this is correct? i am not old enough to play the lottery so i wouldnt know :P. Oh and like i said before, the program doesnt run at 100,000 times a second, that is some what off. But even if it did, that is not how many times it “plays the lottery” it takes dozens of clock cycles for the program to run through the library and generate the 6 random numbers. So it is playing the lottery way less than you think so it will take a lot longer to light up.