newbie post - help with RND Generator

Hi guys,

First post! Recently started using picaxe and getting to grips with the basics. Used to use chip factory to teach the kids but not enough scope. Loving the picaxe!
Anyhow one kid is tryin to do a simple fruit machine type thing - I thought of three random number generators broken down into 6 chunks (ie 3 dice) with outputs to LEDS. Three switches could be pressed in sequence and if each of the dice show the same number, output high to activate a buzzer, open a door etc.

Used manuals to get two dice working, but no idea how to say if 3 dice show the same number then sound alarm.

Not sure if this is the right approach - any help would be much appreciated, thanks in advance.
 

lbenson

Senior Member
What is your setup and code for getting two dice working? What is the difficulty in adding a third?

If the results with values of 1-6 are in variables, say, b1, b2, and b3, then "if b1 = b2 and b1 = b3 then: high 1: endif" tests for identity and (say) lights an LED. You can run it in the simulator.
 

lbenson

Senior Member
Ok, first you need another 4 outputs, so you need either to go to a 28X1 or use I/O expansion, for example, with the 74HC595, as shown in Dr_Acula's post, "16 digital outputs from an 08 picaxe using two HC595 chips", here

http://www.picaxeforum.co.uk/showthread.php?t=9116

(You would only need 1 74HC595 with an 18X.)

In order to make the value returned from the randomization a little clearer, you might want to reduce it to the values you're looking for, 1-6. You can do this using the remainder operation, "//":

Code:
random w0
b10 = b0 // 6 + 1 ' make b10 have a value 1-6
random w0
b11 = b0 // 6 + 1 ' make b11 have a value 1-6
random w0
b12 = b0 // 6 + 1 ' make b12 have a value 1-6
Now you have three values to display, in b10, b11, and b12

You can use one of several newer program control constructs to display your values--SELECT/CASE or IF/ELSEIF/ELSE/ENDIF. Supposing you wanted to output the third die, you could use the following.

Code:
SELECT b12 ' b13 is used to hold the bit settings for turning on the LEDs
  CASE 1: b13 = %00000010 
'...  CASE 2-5
  ELSE: b13 = %00001101 ' this is case 6 
ENDSELECT
Then (on a 28X1), after you have used DIRSC (once, in initialization) to specify which port C pins you want to use as outputs (0-3), you can flash the die value as before

pinsc = b13

Then you can test to see if b10=b11=b12 to ring the gong.

(Note--this code is untested. Something similar should work.)
 
Last edited:
wow
this should keep me busy for a while. Originally trying to use
if b0 < 210
if bo < 168....etc etc - this seems much better!

I can get a 28x from school

Thanks for all the help Ibenson
 

lbenson

Senior Member
Here's a challenge for students. If the three 7-led patterns which requires 4 control lines per pattern are replaced with three RGB LEDs, which require only 3 control lines per pattern, then the whole thing can be done with a 14M, which can have 9 outputs (see Manual 1, Appendix C). You might think that you need an additional output to make the happy sound, but when you have determined a win, then you can reuse one of the pins for a buzzer, and can flash all or some of the lights. It can be done using less than half the programming space on a 14M.
 
Hello,

I will look into this - dont have any 14M to hand but sure I will be able to order them. In the mean time I got as far as this. Take a glance if you wish, (would appreciate it). I did three pairs of LEDS (one red one green) ie. If all red OR all green then sound alarm. Had syntax errors on b10 = b11 = b12 ?? Still two LEDS aint much fun (not sure how random either), prefer your recent idea.
....Basically crash course in PICAXE, usually wood/metal teacher with basic electronics, but think it would be better for kids to learn to program too.

Thanks for your time Ibenson - (not sure if it is acceptable to post code like this?)

'FOR THE TIME BEING 3 PAIRS OF 2 LEDS (ONE RED ONE GREEN)
'IF ALL RED OR ALL GREEN THEN SOUND SIREN


main:

random w0
b10 = b0 // 2 + 1 ' make b10 have a value 1-2
random w0
b11 = b0 // 2 + 1 ' make b11 have a value 1-2
random w0
b12 = b0 // 2 + 1 ' make b12 have a value 1-2

' b13 is used to hold the bit settings for turning on the LEDs

SELECT b10
CASE 1: b13 = %00000001 'this is option 1
CASE 2: b13 = %00000010 'this is option 2
ENDSELECT

SELECT b11
CASE 1: b13 = %00000100 'this is option 1
CASE 2: b13 = %00001000 'this is option 2
ENDSELECT

SELECT b12
CASE 1: b13 = %00010000 'this is option 1
CASE 2: b13 = %00100000 'this is option 2
ENDSELECT

if b10 = b11 = b12 then buzz


buzz:
for b14 = 1 to 5
sound 7,(119,20)
sound 7,(200,20)
next b14

end
 

lbenson

Senior Member
To post code, put the word "code" in square brackets before your code, and "/code" in square brackets after. This will separate the code, put it in a smaller scrollable box to make the thread flow better for those who don't want to look at the code, and retain your formatting--indents, etc.--e.g., &#91;code&#93;...&#91;/code&#93;.

The good thing about using LEDs of different colors instead of the LED array is that your values are already coded so that they can be used to directly light the LEDs. In your code following main, b10 will have a value of 1 (%00000001) or 2 (%00000010). Note that these are the values which you assign to b13--so forget the SELECTs and just use b10 directly. Note also that there is no reason not to have 3 non-zero values with two LEDs--red on, green on, or both on, represented by %00000001, %00000010, %00000011. Just change your code so that the values are in the range of 1-3. The same will be true if you add a third LED, e.g., a blue one, which would have you emulating an RGB LED.

"b10 = b11 = b12" was pseudocode on my part. The picaxe way to do it would be:
Code:
if b10 = b11 and b10 = b12 then
  gosub buzz
endif
Remove the "end" and put a "return" in its place--this makes "buzz:" a subroutine.

You'll want your main code to loop forever. To do this you can put a "do" command immediately after main, and "loop" before buzz. Then you need to add code to read your pushbutton. You only need one of them, not three--after all, it isn't called a "three-armed bandit". And you need to put in the code to turn the LEDs on based on the values of b10, b11, and b12. And that's it--short and sweet.

I think someone showed how to get a ninth output on an 18X, on serout, by poking a register somewhere, which would let you do 3 LEDs on an 18X--but I don't have a note about that.

You can run all this in the simulator and watch the outputs change.
 
Last edited:

westaust55

Moderator
To post code, put the word "code" in square brackets before your code, and "/code" in square brackets after. This will separate the code, put it in a smaller scrollable box to make the thread flow better for those who don't want to look at the code, and retain your formatting--indents, etc.--e.g., [code]...[/code].

This is very true and is covered in the sticky post at the top of the Active forum if only people would read these posts:
http://www.picaxeforum.co.uk/showthread.php?t=7679
http://www.picaxeforum.co.uk/misc.php?do=bbcode

For all new members, I suggest that you have a read at the content of post 4 in this thread as well. It is generic but helps people understand what is required to give full information up front:
http://www.picaxeforum.co.uk/showthread.php?t=10982
 
hi there ibenson,
got the code to work it simulates pretty much what i had in mind - i'm sure it pretty 'messy'. Just one problem, I'd like to hold all the selected LEDs on until the result is true or false. What i have now is first pair on, then off. second pair on then off, third pair on then off. In essence its like spinning one 'fruit' wheel three times and having to remember whether you won or not! posted the code if you can be bothered to take a look - all your help has been much appreciated.:)

AM79

Code:
'FOR THE TIME BEING 3 PAIRS OF 2 LEDS (ONE RED ONE GRN)
'IF ALL RED OR ALL GRN or ALL RED AND GRN THEN SOUND ALARM.


main:
	do
	if pin1 = 1 then prog
	loop
	
prog:
	
	random w0
	b10 = b0 // 3 + 1 ' make b10 have a value 1-3
	random w0
	b11 = b0 // 3 + 1 ' make b11 have a value 1-3
	random w0
	b12 = b0 // 3 + 1 ' make b12 have a value 1-3

	
	'1ST PAIR OF LEDS
	
	IF b10 = %00000001 then outpins = %00000001 ' RED ON
	endif
	IF b10 = %00000010 then outpins = %00000010 ' GRN ON
	endif
	IF b10 = %00000011 then outpins = %00000011 ' RED+GRN ON
	endif
	
	pause 2000
	
	
	'2ND PAIR OF LEDS

	IF b11 = %00000001 then outpins = %00000100 ' RED ON
	endif
	IF b11 = %00000010 then outpins = %00001000 ' GRN ON
	endif
	IF b11 = %00000011 then outpins = %00001100 ' RED+GRN ON
	endif
	
	pause 2000
	
	
	'3RD PAIR OF LEDS

	IF b12 = %00000001 then outpins = %00010000 ' RED ON
	endif
	IF b12 = %00000010 then outpins = %00100000 ' GRN ON
	endif
	IF b12 = %00000011 then outpins = %00110000 ' RED+GRN ON
	endif
	
	pause 2000


	if b10 = b11 and b10 = b12 then   'IF ALL 3 LED SETS MATCH SOUND ALARM 
  	gosub buzz
	endif
	
	goto main


	
buzz:
	for b2 = 1 to 5
	sound 7,(57,20)
	sound 7,(65,20)
	next b2

	return
 

lbenson

Senior Member
First, you don't need to do your tests, since the value that you are sending to outpins is the same as the value you are testing for, so you could just do

outpins = b10

For b11, it is the value shifted over by two bits, which you can achieve by multiplying it by 4 (%00000011 * 4 = %00001100).

For b12, it is the value shifted over by four bits, which you can achieve by multiplying it by 16 (%00000011 * 16 = %00110000).

This doesn't solve your problem of cycling through different outpins statements. To do it with one outpins statement, you simply add your shifted values together, and set outpins equal to that. Two statements can accomplish this:

b13 = b12*4+b11*4+b10
outpins = b13

Note that this has the effect of multiplying b12 by 16 and b11 by 4. You can check this in the simulator.

Not much code now. Ever shorter and sweeter.

There is a problem with your code. You jump out of your "do" loop by going to prog, and then from prog "goto main". This is not considered good practice--it is less likely to cause future problems if you treat "prog" the way you do "buzz", as a subroutine, and then you use a "return" from "prog" as you did from "buzz". So your pushbutton test would be

Code:
if pin1 = 1 then 
  gosub prog
endif
and "goto main" would be replaced by "return".
 
thanks again Ibenson,

Yeah your last post did mention that - i did try outpins = b13 but didnt know about the multiplication. I did try variations of boolean to combine b10, b11 and b12 but i guess not the right way. Should all work now and code much smaller.
Gonna try the 'hippy' tactic too, just for fun - our program good enough for now. Might look into the 7 seg too just for practice, I'll get back to you.

cheers again for the help :) AM79
 
Hi again, all working as planned, and played around a little with the above. In the end, the lad wanted to 'dispense some sweets' with the winning combinations, so needed one more output for the mofset (solenoid) circuit, so stuck with the original LED arrangement.

Other kids getting to like the picaxe now - keypad burglar alarms all round??? I suppose this should be a new thread. Hope to see you there!

Cheers Ibenson
 

lbenson

Senior Member
3-input AND gate

Glad to hear that the kids are gaining picaxe experience. It could be addictive.

You have another option for getting an extra output. If you are still using only six non-zero possibilities there is another value for three bits which you are not using: 7, or %111. You could flash those bits (e.g., bits 0-2) on when a "win" was signified, and use a 3-transistor AND circuit to trigger the release of the prize, as in the attachment below.

From Jameco in the U.S. here, the PN2222 transistors only cost one and a half cents. Any similar inexpensive transistor would work.

If you want to step up to robotics (always popular), you might want to look at the "Autonomous Hexapod" thread by BeanieBots, here: http://www.picaxeforum.co.uk/showthread.php?t=7760
 

Attachments

Last edited:

lbenson

Senior Member
Corrected schematic in above post to include necessary 4K7 resistor from output to ground. Also showed LEDs which are attached to same outputs as transistors.
 
Thats a nice liitle bit of circuitry to have in ones arsenal, I'll try and get the time to implement that one! That lad is putting it down on the breadboard as we speak.

Steep learning curve, probably only a few days ahead of my pupils!! Gotta sort the next one out now - keypad arrangement, going well. The keypad activates the circuit, the sensor picks up the intruder, but then you have to deactivate with a switch. A bit naff - no point in having the keypad really, I'd like to deactivate with the same pin number on the keypad, but hey. Next kid wants a bicycle speedometer. Phew pressure! Your right though its getting quite addictive - doing it for myself rather than the kids.
Thanks again Ibenson that schematic will help a lot, for future stuff too AM79
 

eclectic

Moderator
@Amateurman.

Edited quote.
Steep learning curve, probably only a few days ahead of my pupils!!
Next kid wants a bicycle speedometer. Phew pressure! Your right though its getting quite addictive - doing it for myself rather than the kids.
There are quite a few postings re. Speedo / Odometer circuits.

If you ever get a few minutes, try
Search >> Advanced Search
using some of the the terms

Bicy* / Bike / speedo* / odom*

e
 
Top