November 22, 2024, 02:48:10 AM

News:

You can now use Vixen to program your Prop-1 and Prop-2 controllers!  Get started quickly and easily, without having to learn PBASIC.  Details in the Library forum.


Reading buttons with PROP-1

Started by robomaster-1, October 10, 2007, 02:10:28 PM

Previous topic - Next topic

robomaster-1

Hi Jon,
I am having a problem reading two swicthes that I have on pins six and seven on a Prop-1. The pins are pulled down Low and go Hi when the switch is pushed. Can you help me. I an including a copy the current  program.   


' =========================================================================
'
'   File....... Timer-1A.BS1
'   Purpose.... Control Digital Timer Unitus button on Pin seven to start stop timer
'   Author..... Magic and Technology, (c) 2007 - All Rights Reserved
'   E-mail..... tim_lewis_53@hotmail.com
'   Started.... 17 Sept 2007
'   Updated....
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


SYMBOL  Start   = PIN7                         'Start Button on Pin 7
SYMBOL  Stop    = PIN6                         'Stop Button on Pin 6
SYMBOL  S1      = 0                            ' S1-P0 Reset Relay
SYMBOL  S2      = 1                            ' S2-P1 Start/Stop Relay
SYMBOL  S3      = 2                            ' S3-P2 Timer Mode Relay
SYMBOL  No      = 0

Reset:                                         'Initializing Timer Unit
  PAUSE 100                                    'Pause for 100 milliseconds.
  HIGH  S1                                     'Push Rest Button
  PAUSE 100                                    'Pause for 100 milliseconds.
  LOW S1                                       'Release Reset Button
  PAUSE 100                                    'Pause for 100 milliseconds.
  HIGH S3
  PAUSE 100
  LOW S3
  PAUSE 100

Main:
  IF Start = No THEN Main                       ' wait for Start button to be Pressed
  PAUSE 40                                      ' Delay for 40 milliseconds.
  HIGH S2                                       ' Push Start Button
  PAUSE 40                                  ' hold for 40 milliseconds.
  LOW S2                                        ' Release Start Button
  PAUSE 40                                    ' hold for 40 milliseconds.
  GOTO Main                                     ' back to Main



Tim J. Lewis
Magic and Technology

livinlowe

Do you have the jumper on 7 in the dn position? Also, you can put your reset section in a for next loop, which would do the same thing your doing, just look better.
Shawn
Scaring someone with a prop you built -- priceless!

JonnyMac

You can actually look at the inputs directly -- like this:

' =========================================================================
'
'   File...... Switches_v1.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2007 EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  BtnStart        = PIN7
SYMBOL  BtnStop         = PIN6


' -----[ Constants ]-------------------------------------------------------

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0

SYMBOL  Pressed         = 1
SYMBOL  NotPressed      = 0

SYMBOL  Yes             = 1
SYMBOL  No              = 0


' -----[ Variables ]-------------------------------------------------------


' -----[ Initialization ]--------------------------------------------------

Reset:


' -----[ Program Code ]----------------------------------------------------

Main:
  IF BtnStart = No AND BtnStop = No THEN Main   ' wait for a press

Check_Start:
  IF BtnStart = No THEN Check_Stop
    DEBUG "Start button is pressed.", CR

Check_Stop:
  IF BtnStop = No THEN WrapUp
    DEBUG "Stop button is pressed.", CR

WrapUp:
  DEBUG CR
  PAUSE 100
  GOTO Main



The problem with this is that a switch can change states while you're processing them.  To get around that, just grab the switches and process them after.  This version grabs the buttons and converts the inputs to a value between zero (neither pressed) and three (both are pressed).

' =========================================================================
'
'   File...... Switches_v2.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2007 EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  BtnStart        = PIN7
SYMBOL  BtnStop         = PIN6


' -----[ Constants ]-------------------------------------------------------

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0

SYMBOL  Pressed         = 1
SYMBOL  NotPressed      = 0

SYMBOL  Yes             = 1
SYMBOL  No              = 0


' -----[ Variables ]-------------------------------------------------------

SYMBOL  theButtons      = B2


' -----[ Initialization ]--------------------------------------------------

Reset:


' -----[ Program Code ]----------------------------------------------------

Main:
  theButtons = PINS / 64                        ' scan button port

  BRANCH theButtons, (No_Press, One_Press, Two_Press, Both_Press)
  GOTO Main

No_Press:
  DEBUG "No buttons pressed", CR
  GOTO WrapUp

One_Press:
  DEBUG "Stop button pressed", CR
  GOTO WrapUp

Two_Press:
  DEBUG "Start button pressed", CR
  GOTO WrapUp

Both_Press:
  DEBUG "Both buttons pressed", CR
  GOTO WrapUp

WrapUp:
  PAUSE 100
  GOTO Main



The next version takes advantage of the bit-level access of B0 (and B1) to access button state, individually, after the scan:

' =========================================================================
'
'   File...... Switches_v3.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2007 EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  BtnStart        = PIN7
SYMBOL  BtnStop         = PIN6


' -----[ Constants ]-------------------------------------------------------

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0

SYMBOL  Pressed         = 1
SYMBOL  NotPressed      = 0

SYMBOL  Yes             = 1
SYMBOL  No              = 0


' -----[ Variables ]-------------------------------------------------------

SYMBOL  theButtons      = B0
SYMBOL   bStop          = BIT6                  ' corresponds to PIN6
SYMBOL   bStart         = BIT7                  ' corresponds to PIN7


' -----[ Initialization ]--------------------------------------------------

Reset:


' -----[ Program Code ]----------------------------------------------------

Main:
  theButtons = PINS                             ' scan button port
  IF bStart = No AND bStop = No THEN Main       ' wait for press

Check_Start:
  IF bStart = No THEN Check_Stop
    DEBUG "Start button is pressed.", CR

Check_Stop:
  IF bStop = No THEN WrapUp
    DEBUG "Stop button is pressed.", CR

WrapUp:
  DEBUG CR
  PAUSE 100
  GOTO Main
Jon McPhalen
EFX-TEK Hollywood Office

robomaster-1

October 11, 2007, 11:18:23 AM #3 Last Edit: October 11, 2007, 11:20:19 AM by robomaster-1
Thanks Jon I will try these out and when I get it working I will Post the final version of the code. I did not know about the Branch instruction or that of the AND instruction. Thank You for the help.
Tim J. Lewis
Magic and Technology

JonnyMac

October 11, 2007, 11:30:44 AM #4 Last Edit: October 11, 2007, 11:50:41 AM by JonnyMac
Remember that the BASIC Stamp editor has an online help file that is context sensitive; all you have to do is double-click a keyword to highlight it, and then press the [F1] key to go right to that topic in the help file.  It's a valuable tool.

BRANCH is a very useful tool in the BASIC Stamp as it can be used to replace multiple IF-THEN statements.  So, instead of:

Main:
  IF myButtons = %00 THEN Press_None
  IF myButtons = %01 THEN Press_1
  IF myButtons = %10 THEN Press_2
  IF myButtons = %11 THEN Press_Both


... you can do the same thing with just one line of code:

Main:
  BRANCH myButtons, (Press_None, Press_1, Press_2, Press_Both)


Note that the comparison values are contiguous and start with zero.  There's a really clever trick with LOOKDOWN to take a non-contiguous list and create a contiguous, zero-based index -- I'll put that in my book.

Jon McPhalen
EFX-TEK Hollywood Office

robomaster-1

Thank You Jon always something new to learn about PBasic. WOW
Tim J. Lewis
Magic and Technology

robomaster-1

Jon here is my updated code. It works ok, but either of the two buttons will start and stop the timer. I have not able to get it work like I want it with the start and stop working one at a time. Please take a look I can't see what I am doing wrong?   
Tim Lewis


' =========================================================================
'
'   File...... Timer-4.BS1
'   Purpose.... Control Digital Timer Unit uing two button on Pin Seven and Pin 6to start stop timer
'   Author..... Tim Lewis
'   E-mail..... tim_lewis_53@hotmail.com
'   Started.... 17 Sept 2007
'   Updated.... 16 Oct 2007
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
' Control of Stop-Watch/Digital Clock Unit uing two Buttons on Pin Seven and Pin six
' to start and stop and reset timer unit.
'   How the Stop-Watch / Timer Works :
'   1. Press S3 button to get into Timer Mode (see the display will indicate "00:00:00")
'   2. Press S2 button to start timer. If you want to stop timer press S2 button again.
'   3. Press S1 button to reset the timer
' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  BtnStart        = PIN7
SYMBOL  BtnStop         = PIN6


' -----[ Constants ]-------------------------------------------------------

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0

SYMBOL  Pressed         = 1
SYMBOL  NotPressed      = 0

SYMBOL  Yes             = 1
SYMBOL  No              = 0
SYMBOL  S1      = 0                            ' S1-P0 Reset Relay
SYMBOL  S2      = 1                            ' S2-P1 Start/Stop Relay
SYMBOL  S3      = 2                            ' S3-P2 Timer Mode Relay

' -----[ Variables ]-------------------------------------------------------

SYMBOL  theButtons      = B2


' -----[ Initialization ]--------------------------------------------------

Reset:
  PAUSE 100                                    'Pause for 100 milliseconds.
  HIGH  S1                                     'Push Rest Button
  PAUSE 100                                    'Pause for 100 milliseconds.
  LOW S1                                       'Release Reset Button
  PAUSE 100                                    'Pause for 100 milliseconds.
  HIGH S3                                      'Push Set Timer Mode Button
  PAUSE 100                                    'Pause for 100 milliseconds.
  LOW S3                                       'Release Set Timer Mode Button
  PAUSE 100                                    'Pause for 100 milliseconds.

' -----[ Program Code ]----------------------------------------------------

Main:
  theButtons = PINS / 64                        ' scan button port

  BRANCH theButtons, (No_Press, One_Press, Two_Press, Both_Press)
  GOTO Main

No_Press:
'  DEBUG "No buttons pressed", CR
  GOTO WrapUp

One_Press:
'  DEBUG "Start button pressed", CR
   PAUSE 40                                      ' Delay for 40 milliseconds.
      HIGH S2                                       ' Push Start Button
      PAUSE 40                                      ' hold for 40 milliseconds.
      LOW S2                                        ' Release Start Button
      PAUSE 40                                    ' hold for 40 milliseconds.
  GOTO WrapUp

Two_Press:
'  DEBUG "Stop button pressed", CR
  PAUSE 40                                      ' Delay for 40 milliseconds.
      HIGH S2                                       ' Push Start Button
      PAUSE 40                                      ' hold for 40 milliseconds.
      LOW S2                                        ' Release Start Button
      PAUSE 40                                    ' hold for 40 milliseconds.
  GOTO WrapUp

Both_Press:
'  DEBUG "Reset button pressed", CR
   PAUSE 40                                      ' Delay for 40 milliseconds.
      HIGH S1                                       ' Push Reset Button
      PAUSE 40                                      ' hold for 40 milliseconds.
      LOW S1                                        ' Release Reset Button
      PAUSE 40                                    ' hold for 40 milliseconds.
  GOTO WrapUp

WrapUp:
  PAUSE 100
  GOTO Main
Tim J. Lewis
Magic and Technology

JonnyMac

Well, both One_Press and Two_Press do exactly the same thing (press/release S2) -- this would explain what you're seeing.  You should probably write test snippets to get the behavior you want, and then link to them after the button press scan.
Jon McPhalen
EFX-TEK Hollywood Office

robomaster-1

October 19, 2007, 12:45:05 PM #8 Last Edit: October 19, 2007, 12:48:12 PM by robomaster-1
Jon,
The S2 Button/Relay starts and stops the Timer unit. There is not a separate start and stop buttons, so you push S2 to start it and push S2 a second time to stop it. Is there a way to get is to do it only once for the One_Press and Two_Press routines?
Tim J. Lewis
Magic and Technology

JonnyMac

Give this program a try.  This version keeps track of the clock's status.

' =========================================================================
'
'   File....... Timer-5.BS1
'   Purpose.... Control Digital Timer Unit
'   Author..... Tim Lewis (updated by Jon Williams, EFX-TEK)
'   E-mail..... tim_lewis_53@hotmail.com
'   Started.... 17 SEP 2007
'   Updated.... 19 OCT 2007
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
'
' Control of Stop-Watch/Digital Clock Unit uing two Buttons on Pin Seven
' and Pin six to start and stop and reset timer unit.
'
' How the Stop-Watch / Timer Works :
'   1. Press S3 button to get into Timer Mode (see the display will
'      indicate "00:00:00")
'   2. Press S2 button to start timer. If you want to stop timer press
'      S2 button again.
'   3. Press S1 button to reset the timer


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  BtnStart        = PIN7                  ' SETUP = DN
SYMBOL  BtnStop         = PIN6                  ' SETUP = DN
SYMBOL  S3              = PIN2                  ' S3-P2 Timer Mode Relay
SYMBOL  S2              = PIN1                  ' S2-P1 Start/Stop Relay
SYMBOL  S1              = PIN0                  ' S1-P0 Reset Relay


' -----[ Constants ]-------------------------------------------------------

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0

SYMBOL  Pressed         = 1
SYMBOL  NotPressed      = 0

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  Running         = $FF
SYMBOL  Stopped         = $00


' -----[ Variables ]-------------------------------------------------------

SYMBOL  status          = B2
SYMBOL  theButtons      = B3


' -----[ Initialization ]--------------------------------------------------

Reset:
  PINS = %00000000                              ' clear outputs
  DIRS = %00000111                              ' configure output pins

  PAUSE 100

  S1 = Pressed                                ' enter timer mode
  PAUSE 100
  S1 = NotPressed
  PAUSE 100

  S3 = Pressed                                ' reset
  PAUSE 100
  S3 = NotPressed
  PAUSE 100

  status = Stopped
  GOTO Clean_Up                                 ' for manual reset


' -----[ Program Code ]----------------------------------------------------

Main:
  PAUSE 25
  IF BtnStart = Pressed THEN Start_Timer        ' scan buttons
  IF BtnStop = Pressed THEN Stop_Timer
  GOTO Main


Start_Timer:
  IF BtnStop = Pressed THEN Reset               ' reset on both buttons
  IF status = Running THEN Main                 ' skip if already running
    S2 = Pressed
    PAUSE 50
    S2 = NotPressed
    status = Running                            ' set running mode
    GOTO Clean_Up


Stop_Timer:
  IF BtnStart = Pressed THEN Reset              ' reset on both buttons
  IF status = Stopped THEN Main                 ' skip if already stopped
    S2 = Pressed
    PAUSE 50
    S2 = NotPressed
    status = Stopped                            ' set stopped mode
    GOTO Clean_Up


Clean_Up:
  IF BtnStart = Pressed THEN Clean_Up           ' force buttons release
  IF BtnStop = Pressed THEN Clean_Up
  GOTO Main
Jon McPhalen
EFX-TEK Hollywood Office