can you help a programming beginner

eddie.rodgers

New Member
Hi Guys,
For an absolute 76 year old beginner I need some programming help please.
I have made the axe105 dice and it works well except when it is in standby and running the main loop waiting for pushbutton input 3 to be turned on, it draws 500 ua which would be a bit touch on long term AAA battery drain.
I envisage adding to the main loop a command that allows the main program to loop maybe 50 times as is (waiting for pushbutton as dice is moved around the table) but if this does not happen (as when game is stopped) then put the picaxe to sleep or nap.
I would like it to restart the main loop when the pushbutton is pushed for input 3 and restart the main loop for a few cycles before doing the “make number”routine.
I am afraid I have never programmed before so not sure where to go.
Any ideas please - it would make me (and my grandson) very happy.
Thanks
Eddie
 

inglewoodpete

Senior Member
Welcome to the PICAXE forum. We have beginners of all ages contribute to the forum. Also many very experienced career electronic engineers and programmers.

I am not familiar with the AXE105 but a quick look at AXE105.PDF indicates that it uses and 8-pin PICAXE - probably the 08M2 these days. There are different ways of programming discussed in the data sheet. Are you using flowcharting or coding in basic, command-by-command?

As to a possible solution to your power consumption issue: the PICAXE chip can be placed in a number of different power-saving modes. My thoughts are that when the dice is running in an "idle" state waiting for a button-press, you could slow the clock right down from the normal 4MHz to 32kHz. This will dramatically reduce power consumption when it is idle. First of all, have a look at the SetFreq command in Manual 2 (Commands).
 
Last edited:

hippy

Technical Support
Staff member
The Dice Kit was never designed for low power operation but consumption can usually be reduced by adding NAP commands between checks for a button push, by using DISABLEBOD OFF, disabling ADC and other on-chip peripherals, or by dropping the operating speed.
 

stan74

Senior Member
A simple answer would be a push button switch on the bottom so it only powers up if it's picked up. You could add a large capacitor to the circuit to keep the circuit going for a while. I always try to find simple answers.
 

eddie.rodgers

New Member
Hi Guys and thanks for the tips – these were what I was looking for !
I will probably program in basic
The problem with just adding a nap before pushbutton would be that it would then not run as many loops which are recommended when using the random command. I would like it to do quite a few loops when it is being moved from player to player but after a few minutes when it would appear it was not going to be used again in the immediate future, it could be reduced in speed (I had not thought of that) or put to sleep or long term nap.
I will also look at disabling ADC etc
Is there any easy way to allow it to do say 100-1000 loops (when no button is pressed) and then go slow or nap or sleep until button is again pressed to wake up and continue normal operation ?
Thanks also for the idea of the mechanical button on the bottom. Unfortunately with a 6 year old, it may not be always placed on its bottom !
I have been through the basic commands but as I say, having never programmed before so needed a few suggestions, again thanks
Eddie
 

inglewoodpete

Senior Member
Is there any easy way to allow it to do say 100-1000 loops (when no button is pressed) and then go slow or nap or sleep until button is again pressed to wake up and continue normal operation ?
The "pause-before-sleep" option brings another thought to me. If you are using an 08M2 PICAXE in the kit, then you can make use of the background timer. There is a system variable called Time which increments every second. You will need to use the EnableTime command in your startup code. Then, within your main loop code, reset the Time variable every time a button is pressed (I presume that's how the dice is normally used) Eg Time=0

Add the following code segment ( see if you can get an understanding of what it does, too!)
Code:
[color=Navy]#PICAXE [/color][color=Black]08M2[/color]
[color=Green]'Include the following code segment in your main loop. All of your low power
' and wake-up code would be enclosed here, although you would need to add a
' timer reset (Time = 0) command just after the (normal) keypress in the code[/color]
[color=Blue]If [/color][color=Purple]Time [/color][color=DarkCyan]> [/color][color=Navy]60 [/color][color=Blue]Then [/color][color=Green]'I'm assuming a 60-second time out but you can change this
   [/color][color=Blue]SetFreq k31    [/color][color=Green]'Slow the clock down
   '(you may want to add other power saving commands here)
   [/color][color=Blue]DisableTime
   Do             [/color][color=Green]'Wait in low-power state for the next key-press
       '(you could add a short Nap command period here)
   [/color][color=Blue]Loop While [/color][color=Purple]Pin3 [/color][color=DarkCyan]= [/color][color=Navy]0
   [/color][color=Blue]SetFreq m4     [/color][color=Green]'Back to full throttle
   [/color][color=Blue]EnableTime
   [/color][color=Purple]Time [/color][color=DarkCyan]= [/color][color=Navy]0[/color]
[color=Blue]EndIf[/color]
 

manuka

Senior Member
eddie.rodgers: Welcome from a fellow Kiwi ! I even may be able to help locally, but best you specify where you & grandson are located. Stan. (Eastbourne, Wellington).
 

AllyCat

Senior Member
Hi,

I would like it to do quite a few loops when it is being moved from player to player....Is there any easy way to allow it to do say 100-1000 loops
Actually there's no need for that. For a die (6 possible values) you need only loop through RANDOM three times (or 6 times for a two dice display) for an effectively random result. More may not even do better, because anything controlled by the program itself is "deterministic" i.e. it will do the same every time. For true randomness you need an independent "seed" such as a temperature or a "touch" measurement. But the time it takes to pass the box from one user to the next should be more than sufficient.

The usual method for obtaining the lowest possible "off" current consumption is for the/a pushbutton to apply power to the chip. The chip then closes its own (electronic) power supply switch to "keep itself alive" and then it "kills itself" after the required timeout. But if you have ready-made hardware then a simple sleep/nap method should be quite satisfactory. Generally, completely sleeping (or napping) for a short time (perhaps one second) and then waking up for the minimum possible time (just to check for a button press) is more efficient that running at a slow clock rate (even k32).

Cheers, Alan.
 

eddie.rodgers

New Member
Thanks to all the help – in particular inglewoodpete for his suggestions.
I now know considerably more about picaxe and basic programming than when I started.
I found the simulator to be a tremendous help even though it appears as if it’s not perfect.
I was lucky to find a serial terminal emulator that allowed me to see the time count as it happened (sertxd (#time) ; *******************)
. Before this when simulating it would sometimes work but usually not. With this addition I found the count went 0000011111 etc roughly once per second so getting to 60 count took forever. This does not happen on the real 08m2 that I have set the time to 45 seconds.
This now draws 0.55ma during the loop of getting the random number and then goes down to 20 ua during loop waiting for key press. The only minor disadvantage is when it is in the loop mode the keypress starts the random loop again and you need to press again to get a dice number. I see this as no problem as handing around the table should normally keep this out of sleep mode.
Hi Manuka, I am in Auckland and retired after 50+ years of electronics but 0 minutes of computer programming.
Again thanks
Eddie

' modified for time slowdown current 0.5ma on loop then 25ua on slowdown
low 0 ' make LED pins outputs
low 1
low 2
low 4
enabletime







main:
SetFreq m4 'Back to full throttle
let pins=0 ' switch all LEDs off
random w0 ' get a random number
if input3 is on then makenumber ' test switch
;sertxd (#time) ; ******************* ' shows count during simulate but either 4 or 5 times LONGER
If Time > 45 Then ' 45-second time out
SetFreq k31 'Slow the clock down
DisableTime
Do 'Wait in low-power state for the next key-press
Loop While Pin3 = off
EnableTime
Time = 0
EndIf
goto main

makenumber:
if b1>210 then showsix ' test random number and jump
if b1>168 then showfive
if b1>126 then showfour
if b1>84 then showthree
if b1>42 then showtwo

showone:
let pins=4 ' show LEDs for 1
wait 3
goto main

showtwo:
let pins=16 ' show LEDs for 2
wait 3
goto main

showthree:
let pins=20 ' show LEDs for 3
wait 3
goto main

showfour:
let pins=18 ' show LEDs for 4
wait 3
goto main

showfive:
let pins=22 ' show LEDs for 5
wait 3
goto main

showsix:
let pins=19 ' show LEDs for 6
wait 3
goto main
 

BESQUEUT

Senior Member
1) Please use "copy for forum" or
Code:
 ... [ /code]  tags.
2) Please avoid using GOTO command :
[code]
[color=Green]' modified for time slowdown current 0.5ma on loop then 25ua on slowdown[/color]
[color=Blue]low [/color][color=Navy]0 [/color][color=Green]' make LED pins outputs[/color]
[color=Blue]low [/color][color=Navy]1[/color]
[color=Blue]low [/color][color=Navy]2[/color]
[color=Blue]low [/color][color=Navy]4[/color]
[color=Blue]enabletime

Do
      SetFreq m4        [/color][color=Green]'Back to full throttle
      [/color][color=Purple]pins[/color][color=DarkCyan]=[/color][color=Navy]0            [/color][color=Green]' switch all LEDs off
      [/color][color=Blue]random [/color][color=Purple]w0         [/color][color=Green]' get a random number
      [/color][color=Blue]if [/color][color=Purple]input3 [/color][color=DarkCyan]is [/color][color=Blue]on then gosub [/color][color=Black]Makenumber
      [/color][color=Green];sertxd (#time) ; ******************* ' shows count during simulate but either 4 or 5 times LONGER
      
      [/color][color=Blue]If [/color][color=Purple]Time [/color][color=DarkCyan]> [/color][color=Navy]45 [/color][color=Blue]Then       [/color][color=Green]' 45-second time out 
            [/color][color=Blue]SetFreq k31       [/color][color=Green]'Slow the clock down
            [/color][color=Blue]DisableTime
            Do [/color][color=Green]'Wait in low-power state for the next key-press
            [/color][color=Blue]Loop While [/color][color=Purple]Pin3 [/color][color=DarkCyan]= [/color][color=Blue]off
            EnableTime
            [/color][color=Purple]Time [/color][color=DarkCyan]= [/color][color=Navy]0
      [/color][color=Blue]EndIf
Loop[/color]

[color=Black]makenumber:

      [/color][color=Blue]select  [/color][color=Purple]b1
            [/color][color=Blue]case [/color][color=DarkCyan]>[/color][color=Navy]210 [/color][color=Black]: [/color][color=Purple]pins[/color][color=DarkCyan]=[/color][color=Navy]19     [/color][color=Green]' six
            [/color][color=Blue]case [/color][color=DarkCyan]>[/color][color=Navy]168 [/color][color=Black]: [/color][color=Purple]pins[/color][color=DarkCyan]=[/color][color=Navy]22     [/color][color=Green]' five
            [/color][color=Blue]case [/color][color=DarkCyan]>[/color][color=Navy]126 [/color][color=Black]: [/color][color=Purple]pins[/color][color=DarkCyan]=[/color][color=Navy]18     [/color][color=Green]' four
            [/color][color=Blue]case [/color][color=DarkCyan]>[/color][color=Navy]84 [/color][color=Black]: [/color][color=Purple]pins[/color][color=DarkCyan]=[/color][color=Navy]20      [/color][color=Green]' three
            [/color][color=Blue]case [/color][color=DarkCyan]>[/color][color=Navy]42 [/color][color=Black]: [/color][color=Purple]pins[/color][color=DarkCyan]=[/color][color=Navy]16      [/color][color=Green]' two
            [/color][color=Blue]else [/color][color=Black]: [/color][color=Purple]pins[/color][color=DarkCyan]=[/color][color=Navy]4           [/color][color=Green]' one
      [/color][color=Blue]end select
      wait [/color][color=Navy]3[/color]
[color=Blue]RETURN[/color]
 

BESQUEUT

Senior Member
Lets please keep in mind that:
1. the OP is a beginner
2. the code is taken directly from the AXE105 Dice Kit datasheet (page 33): http://www.picaxe.com/docs/AXE105.pdf
with just a few changes, as per other posts in this thread, in the Main: loop.
YES of course.
That being said, IHMO it's a good practice to start avoiding goto.
He said :
I will probably program in basic... Eddie
So IHMO AXE105 example, translated from Logicator, have to be improved to serve as a BASIC programming example.

Eddie, is my suggest really more complicated to understand ?
Feel free to ask for any explanation you need.

Also, note that this code is slightly different than yours in that when returning from
Makenumber subroutine, it continue with
If Time > 45 Then ...
Your code start a new loop.
 
Last edited:

eddie.rodgers

New Member
Thanks guys,
I didn’t even know there was a “case” command so there is still lots to learn.
Why is the “goto” so frowned upon ?
I have converted to the new program and it runs well so will probably use it for the dice.
Again, appreciate the help
Eddie
 

premelec

Senior Member
There are long discussions and opinions on GOTO - check the archives... or wikipedia - the horrors of 'spagetti code' - jumping all over... bottom line is avoid GOTO but leave it in the command set ;-0
 

eddie.rodgers

New Member
Hi Guys,
Confusion – I’m not sure if I am cut out to be a programmer !
After loading the new software onto the dice board it works very well and I can understand how the simplified program works.
HOWEVER
Every so often – maybe every 10 or so button pushes the LEDs come on and STAY on (on any set of numbers).
So, I thought maybe a key bounce but holding the button down produces a new random number each 3 ish seconds. I then thought the program may not be going from return back to the start where the LEDs are turned off. However, if I push the button the program responds, turns off the LEDs and shows a new number so it must be running.
I cannot get it to fail on the simulation. By the way this never happened with the original program
Where am I going wrong ?
 

BESQUEUT

Senior Member
Hi Guys,
Confusion – I’m not sure if I am cut out to be a programmer !
After loading the new software onto the dice board it works very well and I can understand how the simplified program works.
HOWEVER
Every so often – maybe every 10 or so button pushes the LEDs come on and STAY on (on any set of numbers).
So, I thought maybe a key bounce but holding the button down produces a new random number each 3 ish seconds. I then thought the program may not be going from return back to the start where the LEDs are turned off. However, if I push the button the program responds, turns off the LEDs and shows a new number so it must be running.
I cannot get it to fail on the simulation. By the way this never happened with the original program
Where am I going wrong ?
IHMO, a beginning programmer IS a programmer (and needs even better advices...):rolleyes:
Not sure to understand what is the problem, but if you want the program to go back to "start" when input3 is ON, simply do not execute the
If Time test.
To do that, simply add an Else Command :
Code:
[color=Blue]Do
      SetFreq m4        [/color][color=Green]'Back to full throttle
      [/color][color=Purple]pins[/color][color=DarkCyan]=[/color][color=Navy]0            [/color][color=Green]' switch all LEDs off
      [/color][color=Blue]random [/color][color=Purple]w0         [/color][color=Green]' get a random number
      [/color][color=Blue]if [/color][color=Purple]input3 [/color][color=DarkCyan]is [/color][color=Blue]on then
            gosub [/color][color=Black]Makenumber
      
      [/color][color=Blue]else
            if [/color][color=Purple]Time [/color][color=DarkCyan]> [/color][color=Navy]45 [/color][color=Blue]Then       [/color][color=Green]' 45-second time out 
            [/color][color=Blue]SetFreq k31       [/color][color=Green]'Slow the clock down
                  [/color][color=Blue]DisableTime
                  Do [/color][color=Green]'Wait in low-power state for the next key-press
                  [/color][color=Blue]Loop While [/color][color=Purple]Pin3 [/color][color=DarkCyan]= [/color][color=Blue]off
                  EnableTime
                  [/color][color=Purple]Time [/color][color=DarkCyan]= [/color][color=Navy]0
            [/color][color=Blue]EndIf
      endif
Loop[/color]
This can also be written simplier :
Code:
[color=Blue]Do
      SetFreq m4        [/color][color=Green]'Back to full throttle
      [/color][color=Purple]pins[/color][color=DarkCyan]=[/color][color=Navy]0            [/color][color=Green]' switch all LEDs off
      [/color][color=Blue]random [/color][color=Purple]w0         [/color][color=Green]' get a random number
      [/color][color=Blue]if [/color][color=Purple]input3 [/color][color=DarkCyan]is [/color][color=Blue]on then
            gosub [/color][color=Black]Makenumber
      
      [/color][color=Blue]elseIf [/color][color=Purple]Time [/color][color=DarkCyan]> [/color][color=Navy]45 [/color][color=Blue]Then   [/color][color=Green]' 45-second time out 
            [/color][color=Blue]SetFreq k31       [/color][color=Green]'Slow the clock down
            [/color][color=Blue]DisableTime
            Do [/color][color=Green]'Wait in low-power state for the next key-press
            [/color][color=Blue]Loop While [/color][color=Purple]Pin3 [/color][color=DarkCyan]= [/color][color=Blue]off
            EnableTime
            [/color][color=Purple]Time [/color][color=DarkCyan]= [/color][color=Navy]0
      [/color][color=Blue]EndIf
Loop[/color]
This code is now equivalent to #11 code and my Nota Bene become obsolete :
Also, note that this code is slightly different than yours in that when returning from
Makenumber subroutine, it continue with
If Time > 45 Then ...
Your code start a new loop.
 
Last edited:

eddie.rodgers

New Member
Hi, Thanks again for excellent (and prompt) advise.
I was able to solve the problem but do not understand the reason for LEDs remaining on.
I moved the command "pins = 0" to straight after the "wait 3 seconds" and before the return.
This has totally solved the problem and after almost 100 dice rolls there has been no further problem.
Thanks to your help I now have a program that will withstand a 6 year old !!
I will look at your newest suggestions as I am sure they will assist me as well
 
Top