Shop OBEX P1 Docs P2 Docs Learn Events
How to - Toggle Any Program on/off with RESET — Parallax Forums

How to - Toggle Any Program on/off with RESET

edited 2004-12-02 21:31 in Learn with BlocklyProp
Run and End Control with the BOE’s RESET Button
Here is a simple way to make the RESET button on your Board of Education a toggle switch for your program.· Try TestRestart.bs2.· When you download the program, you will have to press and release the RESET button once to make it run.· The next time you press and release RESET, the program ends.· The third time, the program runs again, the fourth time, the program ends again, and so-on.·

[color=#000033]' TestRestart.bs2[/color]
 
[color=#000033]' {$STAMP BS2}[/color]
[color=#000033]' {$PBASIC 2.5}[/color]
 
[color=#000033]' -- RESET init --[/color]
 
[color=#000033]  DATA     @ 0, 0[/color]
 
[color=#000033]  reset    VAR   Bit[/color]
 
[color=#000033]  READ 0, reset[/color]
[color=#000033]  WRITE 0, ~ reset[/color]
 
[color=#000033]  IF reset = 0 THEN[/color]
[color=#000033]    DEBUG CLS, "Press/release RESET to run...", CR[/color]
[color=#000033]    END[/color]
[color=#000033]  ENDIF[/color]
 
[color=#000033]  DEBUG CR, "Press/release RESET end", CR, CR[/color]
 
[color=#000033]' -- Main routine -- [/color]
 
[color=#000033]  DO[/color]
[color=#000033]    DEBUG "Program running", CR[/color]
[color=#000033]    PAUSE 750[/color]
  [color=#000033]LOOP[/color]


·
How It Works
The directive DATA @ 0, 0 stores a 0 to address 0 in EEPROM, but only when the program is downloaded.· The BASIC Stamp Editor toggles the BASIC Stamp’s reset line after it’s done downloading the program, so the code runs as soon as the download is complete.· When the program starts running, the READ command copies the 0 value from bit-0 of EEPROM address- into the reset variable.· The program has to store the opposite of whatever is in the reset variable back into EEPROM for the next time the program is restarted (with the RESET button).· That’s what WRITE 0, ~ reset does.· It stores “not” of reset back into bit-0 of EEPROM address 0.· When reset is 0, the WRITE command stores 1 in EEPROM address 0, bit-0.· When reset is 1, the WRITE command stores a 0 there.
·
When the program gets to the IF…THEN statement, it ends since reset is 0.· When the RESET button on the BOE is pressed and released, the program runs a second time.· This time, the READ command copies the 1 stored in EEPROM address-0, bit-0 into reset.· Now reset is 1, so the WRITE command stores 0 to EEPROM address-0, bit-0.· Since reset is 1, the IF…THEN statement does not end the program, so the rest of the program executes. ·
·
The second time the RESET button is pressed/released, EEPROM address-0, bit-0 stores a 0, and the program ends immediately.· The third time, it stores a 1, so the rest of the program runs again, and so-on.
·
Incorporating it into Your Programs
Here a code block that you can add to the beginning of most programs to get RESET toggle functionality:
·
' RESET init
·
· DATA···· @0, 0
· reset··· VAR·· Bit
· READ 0, reset
· WRITE 0, ~ reset
· IF reset = 0 THEN END
·
For application programs, it’s better to add the DATA directive to the EEPROM Data section, the reset variable declaration to the Variables section and the READ, and WRITE commands and the IF…THEN statement to the beginning of the Initialization section.
·
Let’s say your program already needs all the EEPROM from address 0 to 174.· You can adjust this code to work with EEPROM address 175 like this:
·
' RESET init
·
· DATA···· @175, 0
· reset··· VAR· ·Bit
· READ 175, reset
· WRITE 175, ~ reset
· IF reset = 0 THEN END
___________________________________________________________________
·
This draft material is part of a forthcoming Stamps in Class text by Andy Lindsay.
·
(c) 2004·by Parallax Inc - all rights reserved.···

Post Edited (Andy Lindsay) : 12/2/2004 9:37:26 PM GMT
Sign In or Register to comment.