3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

LED Cube 8x8x8

Step 52Software: Mode selection and random seed

Software: Mode selection and random seed

When we first started writing effects and debugging them, we noticed that the functions using random numbers displayed the exact same animations every time. It was random alright, but the same random sequence every time. Turns out the random number generator in the ATmega needs to be seeded with a random number to create true random numbers.

We wrote a small function called bootwait(). This function serves two purposes.

1) Create a random seed. 2) Listen for button presses to select mode of operation.

It does the following:

1) Set counter x to 0.

2) Start an infinite loop, while(1).

3) Increment counter x by one.

4) Use x as a random seed.

5) Delay for a while and set red status led on.

6) Check for button presses. If the main button is pressed, the function returns 1. If the PGM button is pressed it returnes 2. The return statements exits the function thus ending the infinite loop.

7) Delay again and set green led on.

8) Check for button presses again.

9) Loop forever until a button is pressed.

The loop loops very fast, so the probability that you will stop it at the same value of x two times in a row is very remote. This is a very simple but effective way to get a good random seed.

Bootwait() is called from the main() function and its return value assigned to the variable i.

If i == 1, the main loop starts a loop that displays effects generated by the ATmega. If i == 2, it enters into RS232 mode and waits for data from a computer.

« Previous StepDownload PDFView All StepsNext Step »
1 comment
Jan 9, 2011. 11:53 AMsbrown7792 says:
Is the pushbutton a momentary pushbutton?

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
651
Followers
7
Author:chr
I like microcontrollers and LEDs :D