Picaxe28x1 and a seven segment display

Hello!
I only joined the other day so this is my first posting, in fact it’s my first involvement with any forum! So please bear with me until I get the hang of things.
I’m using a Picaxe 28x1 to control a seven segment display in conjunction with a 4511B IC, hooked up generally as described in section 3 of the Picaxe manual.
The set up is part of a model railway layout. I trigger a number of point motors through a diode matrix which gives me a particular route. I also input that trigger signal onto an input pin of the Picaxe28x1 which gives me a corresponding route number on the seven segment display and it all works very well. That is until I power down at the end of an operating session. When I power up for the next session the previous routes set are still in place, they won’t have changed, but the seven segment displays have reset themselves to zero! Therefore route indication is lost. So – the question I’m asking is – is it possible to store in the Picaxe28x1 memory the last command it sent to the seven segment driver before power down and re transmit that to the driver on the next power up to set the seven segment display to the last route selected. Or am I asking too much? The code I’m using is as below.

main: if pin0 = 1 then route1
if pin1 = 1 then route2
if pin2 = 1 then route3
if pin3 = 1 then route4
if pin4 = 1 then route5

goto main

route1: let b1 = 1
let pins=b1
goto main

route2: let b1 = 2
let pins=b1
goto main

route3: let b1 = 3
let pins=b1
goto main

route4: let b1 = 4
let pins=b1
goto main

route5: let b1 = 5
let pins=b1
goto main
 

hippy

Ex-Staff (retired)
Yes, you can use the WRITE and READ commands.

To store the last route you can change all your "goto main" to "goto saveLastRoute" ...

route5: let b1 = 5
let pins=b1
goto saveLastRoute

saveLastRoute:
Write 0,b1
goto main

You could instead put a "Write 0,b1" before every "goto main" but this is preferable ( which we will come back to later ).

Then when the program starts you can read what was last written before power-off and set the pins as required before entering your 'main:' routine ...

powerOn:
Read 0,b1
pins = b1

main: if pin0 = 1 then route1
etc

There is scope for optimising your code, and a particular case is to not write to Eeprom if you do not have to as it has a limited lifetime; 1,000,000 writes isn't however that limited if used appropriately.

Rather than -

saveLastRoute:
Write 0,b1
goto main

A better solution which checks if a changed value needs to be written is -

saveLastRoute:
Read 0,b2
If b1 <> b2 Then : Write 0,b1 : End If
goto main
 

hippy

Ex-Staff (retired)
I forgot to say, "Welcome to the PICAXE forum" !

That's nice coding for a first post and there is nothing wrong with it - and having working code is more important than any other measure. Clarity, readability and ease of following is also important and that has it.

Much less understandable but heavily optimised is the following which is given as an example of alternative ways to do things than a criticism of what you have -

Code:
Read 0,b1
Do
  LookDown 1,(0,pin0,pin1,pin2,pin3,pin4),b1
  pins = b1
  Read 0,b2
  If b1 <> b2 Then : Write 0,b1 : End If
Loop
That uses some trickery in its use of LOOKDOWN to determine which input pin is set and to turn that into a routing in b1 of 1 through 5. Note LOOKDOWN does not change 'b1' if no match to 1 is found.
 
Last edited:
Top