Need help Random Timer

#ironman

New Member
08M2 Random problem please help!

Hi
I'm a first time user of picaxe and programming.
I would love some help with programming an 08M2 to produce a random time delay before output, for a reaction game circuit.
I have this code so far from blockly but it produces a value for variable a of either 0 or 65535 and nothing else.
I would appreciate any support to solve this problem, Thanks guys


Code:

symbol varA = w0

main:
do
do
random varA
loop until pinC.1 = 1
do
pause 10
loop until pinC.1 = 1
do
let varA = varA - 5000
loop until varA < 10000
if varA < 10000 then
high C.0
endif
do
pause 10
loop until pinC.1 = 0
if pinC.1 = 1 then
low C.0
else
do
pause 10
loop until pinC.1 = 1
low C.0
endif
loop
stop



project1.1.png
 
Last edited:

lbenson

Senior Member
If you start with a non-zero number, you will get better (but still repeating) pseudo-random results, e.g., before main:

varA = 48611 ' a prime number

Others have suggested that if you prime the random seed with readadc10 on an unconnected pin, you will get a more random start. If you continue from time to time to reseed from the readadc10 you will get a non-repeating sequence. If you don't feel you get enough variation in the readadc10, you could multiply the seed by that number rather than use the adc result as the new seed.
 

#ironman

New Member
Thanks for the reply, but unfortunately the first method caused a syntax error and due to it being my first time programming anything I don't have a clue on how to carry out the second method. :( Could someone edit my code so that I can see what is going on, please? The only way I even got this far was by blockly
 

lbenson

Senior Member
I'm not sure how a syntax error could be caused by

symbol varA = w0
varA = 48611


Code:
#picaxe 08M2

symbol varA = w0
symbol varB = w1
symbol varC = w2
varA = 48611

main:
  for b25 = 1 to 20
    readadc10 C.4,varB
    varC = varA * varB
    random varC
    sertxd(#varA," ",#varB," ",#varC,cr,lf)
    varA = varC
  next b25
This won't simulate reasonably, because the readadc10 doesn't vary, but may give you fairly good response on a real 08M2. You might need to attach a longish wire to C.4 and wave your hand over it to increase alternation in the reading ("handwavium" at work).

[Untested--based on prior speculation that I have read.]
 
Last edited:
Wich range of time delay do you want ?

A random time delay is the simplest of the world.
Your basic mistake , you use blockly :D :D :D
Nice Joke - thats all - , but not a serious way to programming.
 

inglewoodpete

Senior Member
Thanks for the reply, but unfortunately the first method caused a syntax error and due to it being my first time programming anything I don't have a clue on how to carry out the second method. :( Could someone edit my code so that I can see what is going on, please? The only way I even got this far was by blockly
This piece of your original code will distort the random number that you are trying to generate:
Code:
         do
            let varA = varA - 5000
         loop until varA < 10000
You would be better off preserving your random number (VarA) and manipulating a copy of it in each loop. The number passed into the Random function is used to seed the next random number.
Code:
[color=Blue]Symbol [/color][color=Purple]varA [/color][color=DarkCyan]= [/color][color=Purple]w0[/color]
[color=Blue]Symbol [/color][color=Purple]varB [/color][color=DarkCyan]= [/color][color=Purple]w1[/color]

[color=Black]Main:
      [/color][color=Blue]Do
         Do
            Random [/color][color=Purple]varA
         [/color][color=Blue]Loop Until [/color][color=Purple]pinC.1 [/color][color=DarkCyan]= [/color][color=Navy]1
         [/color][color=Blue]Let [/color][color=Purple]varB [/color][color=DarkCyan]= [/color][color=Purple]varA
         [/color][color=Blue]Do
            Pause [/color][color=Navy]10
         [/color][color=Blue]Loop Until [/color][color=Purple]pinC.1 [/color][color=DarkCyan]= [/color][color=Navy]1
         [/color][color=Blue]Do
            Let [/color][color=Purple]varB [/color][color=DarkCyan]= [/color][color=Purple]varB [/color][color=DarkCyan]- [/color][color=Navy]5000
         [/color][color=Blue]Loop Until [/color][color=Purple]varB [/color][color=DarkCyan]< [/color][color=Navy]10000
         [/color][color=Blue]If [/color][color=Purple]varB [/color][color=DarkCyan]< [/color][color=Navy]10000 [/color][color=Blue]Then
            High C.0
         End If
         Do
            Pause [/color][color=Navy]10
         [/color][color=Blue]Loop Until [/color][color=Purple]pinC.1 [/color][color=DarkCyan]= [/color][color=Navy]0
         [/color][color=Blue]If [/color][color=Purple]pinC.1 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]Then
            Low C.0
         Else
            Do
               Pause [/color][color=Navy]10
            [/color][color=Blue]Loop Until [/color][color=Purple]pinC.1 [/color][color=DarkCyan]= [/color][color=Navy]1
            [/color][color=Blue]Low C.0
         End If
      Loop[/color]
 

#ironman

New Member
This piece of your original code will distort the random number that you are trying to generate:
Code:
         do
            let varA = varA - 5000
         loop until varA < 10000
You would be better off preserving your random number (VarA) and manipulating a copy of it in each loop. The number passed into the Random function is used to seed the next random number.
Code:
[color=Blue]Symbol [/color][color=Purple]varA [/color][color=DarkCyan]= [/color][color=Purple]w0[/color]
[color=Blue]Symbol [/color][color=Purple]varB [/color][color=DarkCyan]= [/color][color=Purple]w1[/color]

[color=Black]Main:
      [/color][color=Blue]Do
         Do
            Random [/color][color=Purple]varA
         [/color][color=Blue]Loop Until [/color][color=Purple]pinC.1 [/color][color=DarkCyan]= [/color][color=Navy]1
         [/color][color=Blue]Let [/color][color=Purple]varB [/color][color=DarkCyan]= [/color][color=Purple]varA
         [/color][color=Blue]Do
            Pause [/color][color=Navy]10
         [/color][color=Blue]Loop Until [/color][color=Purple]pinC.1 [/color][color=DarkCyan]= [/color][color=Navy]1
         [/color][color=Blue]Do
            Let [/color][color=Purple]varB [/color][color=DarkCyan]= [/color][color=Purple]varB [/color][color=DarkCyan]- [/color][color=Navy]5000
         [/color][color=Blue]Loop Until [/color][color=Purple]varB [/color][color=DarkCyan]< [/color][color=Navy]10000
         [/color][color=Blue]If [/color][color=Purple]varB [/color][color=DarkCyan]< [/color][color=Navy]10000 [/color][color=Blue]Then
            High C.0
         End If
         Do
            Pause [/color][color=Navy]10
         [/color][color=Blue]Loop Until [/color][color=Purple]pinC.1 [/color][color=DarkCyan]= [/color][color=Navy]0
         [/color][color=Blue]If [/color][color=Purple]pinC.1 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]Then
            Low C.0
         Else
            Do
               Pause [/color][color=Navy]10
            [/color][color=Blue]Loop Until [/color][color=Purple]pinC.1 [/color][color=DarkCyan]= [/color][color=Navy]1
            [/color][color=Blue]Low C.0
         End If
      Loop[/color]
Thanks inglewoodpete your suggestion worked perfectly
Anyway, I found that the original code worked sufficiently on PE6, it must be just be a problem with blockly
Thanks to Ibenson too for helping me to make the time delay more random
Doubt I'll ever use blockly again now! :D
 

lbenson

Senior Member
And when it's time to programming in the real world and creating reasonable code ,
maybe you visit the German Picaxe-Forum for learning using Picaxe in the right way, ok ;-)
Here is an example to use the random command in a right way - no guess , playing , subtracting , or any other jokes :)

http://www.zierath-software.de/phpBB3/viewtopic.php?f=7&t=176
Instead of posting a link which might become invalid in the future, why not post the code on how "to use the random command in a right way"?
 

westaust55

Moderator
The link is into an entire German website full of PICAXE related information.
Rather than having a small bit posted here (which even I often suggest) worth a look:
http://www.zierath-software.de/picaxe/
http://www.zierath-software.de/phpBB3/

My German is pretty poor (mainly learnt word for food with good reason when there 20 years ago) so a bit of translation help is necessary to make use of the information there. Some appears to duplicate data/information on the PICAXE website.
 

JimPerry

Senior Member
If you use Chrome there is an option to automatically translate available - right click and select translate :cool:
 
ok folks ,

the German Picaxe-Kompendium side exists since 2006.
And any years the Forum.
Most of the german picaxe user don't speak english to have a look here for any questions.
And ..... sorry , but ..... not always you can find here the best answer of simple code questions.

Until the Picaxe is interesting for microcontroller beginners , all links and the Forum will persist. Don't worry :)

Why I don't post a code ?
well, I have ask which range of time delay ist desired , but till know , no answer.
So , I can't post a code without conditions.
Programming is not a guessing game - ok, maybe for anyone yet :)

Greetings
Your German Picaxe-Kompendium Team

P.S.
On the Forum side AND on the Kompendium side ( under Examples) you can find many many many many Codes for a lot of projects .
 

lbenson

Senior Member
Why I don't post a code ?
well, I have ask which range of time delay ist desired , but till know , no answer.
Leaving aside the question of what to do with the number once you have it, if you have a superior way to generate random or pseudo-random numbers on a picaxe, please post it.
 

edmunds

Senior Member
Dear all,

While finding the German forum was all new to me and it looks like a great resource (with google translate being your best friend :)) I did not have energy to read through the longish discussion on random numbers there right now. However, I have code at hand that I have got satisfactory random results with small numbers with the two procedures below and maybe it is helpful:

Code:
Random3sub:
  random helper1
  RandomOf3 = helper1//3+1 'pick random route out of three
return

Random2sub:
  random helper2
  RandomOf2 = helper2//2+1 'pick random route out of two
return
RandomOfx is a word variable that contains the random number I'm looking for. helperx is just another word variable. Replace the numbers 3 and 2 respectively with the number of choice=a range of numbers where you are looking for the random choice.


Good luck,

Edmunds
 
Hello Edmunds ,

It gives me pleasure that at least one people has enough interest to learn some thing.
The key is the command or rather the function : MOD
In Picaxe-Code you use the signs ==> // <== for this function.

German Picaxe-User don't feel like it too, to translate each english word into german when they have to read any post here.
Here is no translation in German , WHY should I do a translation in english ??
Same work for everyone.

How does Edmunds just say :
have a look , below on each end , mostly you will find any examples and /or procedures
 

lbenson

Senior Member
Here is no translation in German , WHY should I do a translation in english ??
I didn't see anyone ask for a translation--just code that produced a better random number sequence.

Hippy's post entitled Random Numbers in code snippets from 2007 starts with an example using the MOD ("//") function: http://www.picaxeforum.co.uk/showthread.php?7799-Random-Numbers

I would suspect that most threads about the RANDOM function mention the use of MOD, since a raw number between 0 and 65,535 is not likely to be what a poster is looking for.

But glad to know that there is an active German picaxe forum.
 

JimPerry

Senior Member
English is a very difficult language for most programmers - I speak some French, German, English, American and fluent Gibberish :confused:

Englisch ist eine sehr schwierige Sprache für die meisten Programmierer - Ich spreche ein wenig Französisch , Deutsch, Englisch , amerikanische und fließend Gibberish


L'anglais est une langue très difficile pour la plupart des programmeurs - Je parle un peu , allemand, anglais , Gibberish américain et français couramment


English is a language very difficulty for programmers MOST - I speak some French , German, English , American and fluent Gibberish

Just shows that BASIC / Picaxe BASIC is very unforgiving about translation/.syntax (all translations mangled by Google) :rolleyes:
 
Last edited:
Basic is already a very simple language, even if it is using English words - that's not really the problem.
User who does not realy speak English, learn basic rather quickly.
On the other side, make questions or read answer in English is already a huge problem

I didn't see anyone ask for a translation
Nice joke my dear
How should they ask , if they can not speak english ? lool
Language differences will be unfortunately remain difficult

If the function mod but already is supposedly so long known, i wonder why so much user did not know to use the function random properly?
 

inglewoodpete

Senior Member
If the function mod but already is supposedly so long known, i wonder why so much user did not know to use the function random properly?
When used in a microcontroller, the Random function has more rules to observe than in a computer with many processes running. Many forum members have very little background knowledge of programming - that's why they ask questions (and sometimes they don't ask the right questions:rolleyes:).

I have known about the MOD function for a long time but I've had to use it many times. If I did not need to use it then I probably would not know about how I could use it.
 

hippy

Technical Support
Staff member
If the function mod but already is supposedly so long known, i wonder why so much user did not know to use the function random properly?
I imagine it is much like any other 'software trick' or technique; one has either discovered it or not. What may be well known to some may be completely unknown to others.

As we have been discussing language, an analogy is that it is like a dictionary. Nearly all the words are there, are usually well defined, but most people will not know all those words, will only discover new ones when they first encounter them being used.
 

Dartmoor

Member
Just some 'lateral thinking' (that will probably confuse Google translate?) . . .
With most Picaxe M2 chips there is an option for more than one "start" (ie loop).
Have a separate loop that simply increments the value of a variable, between the limits that are required, and then returns to the start. This can run at high speed.
In the main loop just look for the value of the variable when needed and you should get a pretty random answer?
Most of our common uses only require quite a small range?

Provided the test is not made the instant the power is swithed on, it should work?
I am still a newbie (using flowchart) & so expect to get shot down in flames (again!).
 
Your idea is nice.
what you mean is multitasking.
only, not every code can be executed in multitasking,
especially when functions are used is where multitasking is not allowed.
So, possible but not very flexible.
The best way is still on the function mod, I think :)
 
Top