November 23, 2024, 08:27:30 PM

News:

Got VSA?  Want to use your Prop-SX?  Now you can!  See the VSA section of the Library forum for Prop-SX code that works with VSA.


Saving Random Value to EEPROM

Started by JackMan, April 22, 2011, 09:14:36 AM

Previous topic - Next topic

JackMan

Would this program cause any danger of burning out two locations of the EEPROM? I believe it will only write to these locations once each time the program is run. It seems to work pretty well as I get a different random pattern everytime.




Main:
  HIGH Audio
  PAUSE 250
  LOW Audio
  PAUSE 6000
   timer = 0                                     'reset timer
  READ 0, valLo
  READ 1, valHi
  DEBUG #value

Rattle_Lid:
  RANDOM value
   delay = PULSE_MAX - PULSE_MIN + 1             ' calculate span for //
   delay = value // delay + PULSE_MIN            ' caculate delay
  TOGGLE Valve1
  PAUSE delay
   timer = timer + delay
  IF timer < RATTLE_TIME THEN Rattle_Lid
  LOW Valve1
  PAUSE 4000
   timer = 0                                     'reset timer
  READ 0, valLo
  READ 1, valHi
  DEBUG #value

Rock_It:
  RANDOM value
   valves = value & %00000110
  IF valves = %110 THEN Rock_It                 ' only one valve on at a time
  IF valves = last THEN Rock_It                 ' no repeats
   last = valves                                ' save for next cycle

  PINS = valves                                 ' update rock outputs
  RANDOM value                                  ' restir
   delay = PULSE_MAX - PULSE_MIN + 1            ' calculate span for //
   delay = value // delay + PULSE_MIN           ' caculate delay
  PAUSE delay
   timer = timer + delay                        ' update timer
  IF timer < ROCK_TIME THEN Rock_It             ' rock crate for 4s
  PINS = %00000000
  WRITE 0, valLo
  WRITE 1, valHi
  PAUSE 20000                                   ' window for Beast Attack

  HIGH Reset

JonnyMac

April 22, 2011, 11:40:49 AM #1 Last Edit: April 22, 2011, 11:49:51 AM by JonnyMac
Here's what I'd do: Create a section above reset called Power_Up which you know will run only on a hard reset (power up or reset button). This is where you read the value from EEPROM (to you random variable), use RANDOM on it, then write it back for next time.

This process pre-initialzes lottery to a new value each cycle; no need for READ and WRITE after the Power_Up section (FWIW, only WRITE wears out the EEPROM, so you only want to use it once.  In your program you're using it after every cycle).

Power_Up:
 READ 0, lottoLo                       ' low byte of lottery
 READ 1, lottoHi                       ' high byte of lottery
 RANDOM lottery                        ' randomize 1st value for next run
 WRITE 0, lottoLo                      ' save for next run
 WRITE 1, lottoHi

Reset:
 PINS = %00000000
 DIRS = %00111111



BTW, if your controller is interfacing with an AP-16+ then you should add PAUSE 2500 to the end of the Power_Up section; this gives the AP-16+ time to power up and configure itself.  Again, you only need this for power up.
Jon McPhalen
EFX-TEK Hollywood Office

JonnyMac

On the "wears out" comment above...

The EEPROM we use on the Prop-1 has a guaranteed life of 1,000,000 write cycles.  Let's be conservative and use 10% of that: 100,000 write cycles.  This means that if the EE lasted only 10% of what the manufacturer says it will, you can power up your prop once a day for about 274 years.

Yes, I tend to be very conservative in my design approach; I have found that this is what helps circuits last.
Jon McPhalen
EFX-TEK Hollywood Office

JackMan

Yep, I see what you're saying. The way I have things setup the Prop-1 actually powers down after each cycle of the prop so it would write to EEPROM once either way. The AP-16+ is on and ready constantly when the power supply is live. Thanks for the info.

JonnyMac

Why are you powering down? The Prop-1 only consumes less than 50mA and you invite troubles power cycling.  If the AP-16+ can stay powered up, why can't the Prop-1?
Jon McPhalen
EFX-TEK Hollywood Office

JackMan

My reason is kinda complicated. The way I have everything wired I have more control over what I want to take place via the 4 CH remote. I can always re-do my wiring and programming but for now I'm leaving it as is.