Using write and read to store a pointer after switching off

vernon

New Member
I have very little experience in writing code except for a few simple programs and have been asked by a friend to produces a circuit to give visual indication of a condition of a point solenoid on a model train track. on powering off the requirement is to retain the indication of the last point condition when powering up as the solenoid retains its position. The program i have so far using an 08m2 works but i cannot get the variable to store using the write command. i do not know if the simulator does not show the command or it does not perform the action, but the syntax does not say it is wrong. these are the 2 subroutines.

pointA:
b1 = 1
write b2,b1
if point = 1 then high outA endif
if point = 1 then low outB endif
return
pointB:
b1 = 0
write b2, b1
if point = 0 then low outA endif
if point = 0 then high outB endif
return

I obviously have done something wrong and would appreciate some guidance

vernon
 
I have been asked by a friend to produces a circuit to give visual indication of a condition of a point solenoid on a model train track. on powering off the requirement is to retain the indication of the last point condition when powering up as the solenoid retains its position.

Here is some code that seems to fit the bill. (You don't say whether the Picaxe is providing the visual indication, or it's a simple LED connected to the Points).

One thing to be aware of with EEPROM, is that it has a finite life. A hard loop re-writing it every few milliseconds would not be kind to it. The following code writes it 'on change'.

Rich (BB code):
#picaxe 08m2
#no_data

symbol Point = PinC.3               ;pin used to sense state of points.
                                    ;(May need a pull-down resistor)
                                    ;or is it also connected to an LED?
                                    
symbol Indicator = PinC.2           ;Indicator LED (if required)

      output C.2                    ;need to set Indicator to be an output
                                    ;and can't use PinC.2 format :-(
      
      b2 = 0                        ;seems to be EEPROM position to use

do
      read b2,b0                    ;get current EEPROM contents
      if b0 <> Point then           ;if it's different to reality
            write b2,Point          ;store current state of Points
      endif

      Indicator = Point             ;Mirror state of Points via Indicator LED

loop

After I posted this, it did strike me that the storage of the state in EEPROM is kind-of redundant :unsure:

Is the signal that changes the points a momentary one, by any chance ❓
 
Last edited:
Hi,

The Simulator cannot store data over a power-down, or probably even a Reset/ReRun. Beware also that a new Program Download will Clear any EEPROM data unless a #NO_DATA directive is put at the start of the program. Also you haven't specified what variable you have declared for "point".

if point = 1 then high outA endif
if point = 1 then low outB endif

However, as Phil says, you're not actually reading the EEPROM, and the above is unnecessarily verbose (and could occasionally lead to inconsistent outputs). A normal structure could be:
Code:
READ 0 , point
IF point = 1 THEN
   HIGH outA
   LOW outB
ELSE
   LOW outA
   HIGH outB
ENDIF
Or Phil's version can be even more compact, provided that you remember to include a DIRS (or OUTPUTs) instruction, or initial HIGH and LOW Output commands. ;)

Cheers, Alan.
 
Back
Top