You are here: Connection Troubleshooting > USB Resets BASIC Stamp

USB Resets BASIC Stamp

A BASIC Stamp connected to a PC via a USB can be reset by the USB connection. This can occur with USB-based boards, and with serial boards using the USB to Serial Adapter. Resets can happen when:

The resets are caused by the PC's operating system checking to see if a new plug-and-play device (such as a mouse) was just connected. Disconnecting your board may cause a single reset. Reconnecting typically causes several resets in a row during the first few seconds.

Tip:  If you are using the USB to Serial Adapter, resets are easy to avoid. After connecting your board to your PC the first time, always disconnect and reconnect at the serial (9-pin) end of the USB to Serial Adapter. Leaving the Adapter connected to your PC keeps the virtual communications port open, preventing an unwanted reset.

What BASIC Stamp Resets Do

Each time a BASIC Stamp is reset:

Make sure to keep this in mind while working on your BASIC Stamp projects. Some programs have variable values that you do not want to lose when you disconnect or reconnect your board to the PC. This is common with datalogging activities.

Datalogging Activities

Some Stamps in Class activities use BASIC Stamp programs that log sensor data. Typically, the programmed board is disconnected from the PC, and taken someplace to record data with sensors. Afterward, the board is reconnected to the PC so the sensor data can be displayed on the Debug Terminal or saved to the PC for later use.

If a program only records sensor data in RAM as variable values, a USB-based board will lose that data when reconnecting to the PC because the BASIC Stamp will reset. So, USB-based boards must stay connected to the computer with programs that hold data RAM as variable values.

For USB-based boards and datalogging programs with applications where the board is disconnected and reconnected to the PC, the program should:

Save Values to EEPROM using READ, WRITE, and DATA

A simple solution for retaining values between resets is to save them in portions of the EEPROM program memory that are not used to store the actual program. Since the program is stored starting at the highest memory address, the low memory addresses are usually available for storing these values.

Use the WRITE command to store values to addresses in BASIC Stamp EEPROM memory. Then, use READ commands to retrieve those values from EEPROM, even after connecting it to the PC. You can build READ commands into your main program, or even run a separate program to retrieve those values.

Pre-initialized values can also be stored in EEPROM with the DATA directive.

See the PBASIC Language Reference's READ, WRITE, and DATA commands for more information.

Prevent Unexpected Bugs by Starting Your Programs with a One-Second Delay

If your application requires reconnecting your USB board to the PC while the BASIC Stamp program is running, always add a one-second delay to the beginning of your program. Examples of one-second delays for BS2 modules include: PAUSE 1000 and FREQOUT Pin, 1000, Frequency.

Adding a one-second delay at the start of your program could prevent these two unwanted behaviors:

  1. If the BASIC Stamp transmits serial messages immediately after these resets, the operating system could mistake it for a serial mouse or other plug-and play-device.
  2. If your program advances a value stored in EEPROM with each reset, the value will increase by one or more (ten is not uncommon) each time you connect your USB BASIC Stamp board to a PC.

Again, the one-second delay at the very beginning of the program prevents these issues. Other ways to prevent them include leaving the power off until one second after you have connected your USB board, or pressing and holding the reset button for that time.

Programs that Use the Reset Button

Some programs use your board's Reset button to advance a value stored in EEPROM with each reset, or use the reset to trigger a "record" or "playback" mode.

For reset button applications with USB boards, it's best to design the application assuming the reset will happen when you disconnect and reconnect the board to the PC. Then, make sure to press and hold the reset button each time you disconnect or reconnect your board. This will ensure that a single, expected reset occurs, and not multiple resets as can happen with reconnection.

This example program uses a DATA directive to pre-initialize the value stored by EEPROM address 0 to 255 when the program is loaded. The READ command fetches this value from EEPROM and adds 1 to it. For a byte variable, 255 + 1 = 0 since 255 is the largest it can store, so it rolls over to zero. Taking the remainder of this eeVal/2 results in either 0 or 1. The WRITE command stores this modified value back to EEPROM address 0. Then, the ON…GOTO… command uses eeVal, which stores 0 or 1. With each reset of the BASIC Stamp, the value advances from 0 to 1, to 0 again, and so on…

' {$STAMP BS2}                      ' Select BS2 as target module
' {$PBASIC 2.5}                     ' Select PBASIC 2.5 as language

eeVal VAR Byte                      ' EEPROM value variable
DATA 255                            ' Pre-initialize EEPROM address 0 to 255

PAUSE 1000                          ' Always wait 1 s before advancing EEPROM

READ 0, eeVal                       ' Fetch value from EEPROM address 0
eeval = eeVal + 1                   ' Add 1 to eeVal
eeval = eeval // 2                  ' Take remainder of eeVal / 2
WRITE 0, eeVal                      ' Write new result back to EEPROM

ON eeVal GOTO Playback, Record      ' Use eeVal result to select routine

Playback:                           ' Playback label
  DEBUG "Playing back..."           ' Debug message indicating playback started
  ' Report logged data code here    ' Your playback code goes here
  END                               ' End, don't record after playback

Record:                             ' Record label
  DEBUG "Recording..."              ' Message indicates recording started
  ' Datalogging code here           ' Your datalogging code goes here
  END                              	

Check for the PC Connection with SERIN

Another useful strategy is to write a program routine that checks to see if the board is connected to the PC. Then the program can make decisions based on this information. For example, "if not connected, then log data” and “if connected, then display data.”

The code example below demonstrates one way to do this. It displays a message to press any key, then a SERIN command that waits for a message from P16 (the programming port) for 2000 ms. If a character is typed into the Debug Terminal’s Transmit windowpane during that time, the program continues through the Playback routine. If not, it jumps to the Record routine.

' {$STAMP BS2}                      ' Select BS2 as target module
' {$PBASIC 2.5}                     ' Select PBASIC 2.5 as language

char VAR Byte                       ' Variable for getting character

PAUSE 1000                          ' Always wait 1 s before serial transmit
DEBUG "Press any key...", CR        ' User prompt

' Wait 2 s for character from Debug Terminal’s Transmit windowpane.  Jump to 
' Record label if no character received during that time.  Continue to Playback
' label if character received.
SERIN 16, 84, 2000, Record, [char]   

Playback:                           ' Playback label
  DEBUG "Playing back..."           ' Debug message indicating playback started
  ' Report logged data code here    ' Your playback code goes here
  END                               ' End, don’t record after playback

Record:                             ' Record label
  DEBUG "Recording..."              ' Message indicates recording started
  ' Datalogging code here           ' Your datalogging code goes here
  END                               	

See the PBASIC Language Reference's SERIN command for more information.

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012