In this tutorial, we are going to add 3 more LED's for a total of 4, then make them blink randomly. For added joy, we will draw matching circles on the screen and depending on how annoying you want to be, we will add sounds to go along with the random blinking. Before I go into the program, you will need to wire up 3 more LED's, if you made it through the last tutorial, it should not be too much of a stretch to wire up these other three. Just remember the Ground needs to connect to the short wire on the LED and the the Data Pins (13,14 and 15) hook up to the long wire on the LED through a resister. Here is mine, the green wires are Ground, the Orange wires are the Data Pins.
Blinking LED's: I went over this in my first tutorial, if you have not worked through that, I suggest you do. We need to configure each Pin for data output, so we can turn the LED's off and on.
Random Numbers: The point of this exercise is to make the 4 LED's blink on and off randomly. We do this by setting the variables G, Y, R and B to either 1 or 2 and we want to assign this randomly. MMBasic has its own random number generator built in, to use it the first thing we must do is seed it off the system clock, this ensures the numbers will be as random as possible. We then use the RND function to generate a random number between 0 and 1, in order to get a number between 0 and 2, we need to multiply the number generated by the RND function by 2, this will generate numbers like .0005432 or 1.0274591, what we want to do is round those numbers to the nearest whole number, being 0 or 1 by using the Int function, then we add 1 to it, so we get a 1 or 2. This is done with the following code; "B = Int(Rnd * 2) + 1". If we wanted to produce a number between 1 and 6 (think 6 sided dice) we would use "Dice = Int(Rnd * 6) + 1".
Circles: When I was originally messing around with my Maximite, I wanted to put matching colored circles on the screen that would flash along with the LED's, that way if something went wrong, I would have some sort of indicator of what might be happening, plus it kind of adds to the show. The circile function is used something like this; "Circle (x pos,y pos), radius, color, aspect, fill/nofill". In the parenthesis is the X and Y positions on the screen, (50,100) would mean the circle is centered 50 pixels from the left and 100 pixels from the top. Radius is exactly that 25 would set the radius of the circle to 25 pixels. The aspect will define the aspect ratio. Because the Maximite's pixels are rectangular an aspect ratio of 0.833 will result in a perfect circle (more or less) on most monitors. Other ratios can be specified for a variety of ovals. If nothing is specified the default is 1.0. So, "Circle (50,100), 25, 2, .833" will produce an empty green circle. Adding an "f" to the end as such; "Circle (50,100), 25, 2, .833, f" will produce a solid green circle.The following shows the numbers assigned for each of the basic 8 colors, 0 is Black.
Sound: This is really just an annoying add on, which I actually did not find use for until later on. But I am putting it here, because, well it is annoying. The Sound function is an easy way to play crude musical notes. The format of the function is fairly simple, "Sound Frequency, Duration". For instance "Sound 440, 250" will play the A note for 250 milliseconds ( one quarter of a second). For this program I am going to use 4 notes and they are as follows; 440 - A, 454 - B, 523 - C and 587 - D. If these are not correct, blame this site, it is where i got these numbers from. I determine the note to play by generating a random number between 1 and 4 with "S = Int(Rnd * 4) + 1", set the S1 variable to the frequency depending on the value of S and then play the note with "Sound S1,250".
Now lets tie this altogether, we start the program with seeding the random number generator, initialize the Data Pin configurations for Data Output and draw out initial circles on the screen.
We then move into the main program, where we randomly determine which LED's will be on and what key we will play and plays it. Then you will see for each of the variables G, Y, R, and B, we check to see if they are a 1 or a 2, using If-Then-EndIf statements. If the variable is set to one, it does not light the LED and draws an empty circle. If the variable is 2, it turns the LED on and draws the corresponding circle filled in. The program then pauses for half a second and goes back to the Main label and starts over again. The program will cycle through this until Ctrl-C is pressed.
' rndblink.bas
Randomize Timer
SetPin 12, DOUT
SetPin 13, DOUT
SetPin 14, DOUT
SetPin 15, DOUT
Circle (50,100), 25, 2, .833, f
Circle (101,100), 25, 6, .833, f
Circle (151,100), 25, 4, .833, f
Circle (201,100), 25, 1, .833, f
Main:
Cls
G = Int(Rnd * 2) + 1
Y = Int(Rnd * 2) + 1
R = Int(Rnd * 2) + 1
B = Int(Rnd * 2) + 1
S = Int(Rnd * 4) + 1
If S = 1 Then S1 = 440
If S = 2 Then S1 = 454
If S = 3 Then S1 = 523
If S = 4 Then S1 = 587
Sound S1,250
If G = 1 Then
G1 = 7
Pin(12) = 0
Circle (50,100), 25, G1, .833
Endif
If G = 2 Then
G1 = 2
Pin(12) = 1
Circle (50,100), 25, G1, .833, f
Endif
If Y = 1 Then
Y1 = 7
Pin(13) = 0
Circle (101,100), 25, Y1, .833
Endif
If Y = 2 Then
Y1 = 6
Pin(13) = 1
Circle (101,100), 25, Y1, .833, f
Endif
If R = 1 Then
R1 = 7
Pin(14) = 0
Circle (151,100), 25, R1, .833
Endif
If R = 2 Then
R1 = 4
Pin(14) = 1
Circle (151,100), 25, R1, .833, f
Endif
If B = 1 Then
B1 = 7
Pin(15) = 0
Circle (201,100), 25, B1, .833
Endif
If B = 2 Then
B1 = 1
Pin(15) = 1
Circle (201,100), 25, B1, .833, f
Endif
Pause 500
GoTo Main
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.