November 24, 2024, 09:18:46 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.


Multiple inputs Prop - 1 Simple

Started by gadget-evilusions, August 22, 2015, 05:00:23 AM

Previous topic - Next topic

gadget-evilusions

I am trying to read 3 inputs, once all 3 inputs are "IsPressed" I want to activate Out0 for 30 seconds.

Below is the program I made. Unfortunately once Reg1 is pressed, the program stops. I added the Debug and once "Reg1 = 1" shows  up on the screen, nothing else happens. I am sure there is a better way to do what I need to do.

' =========================================================================
'
'   File......
'   Purpose...
'   Author....
'   E-mail....
'
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================

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

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

' -----[ I/O Definitions ]-------------------------------------------------
SYMBOL  Reg1         = PIN7
SYMBOL  Reg2         = PIN6
SYMBOL  Reg3         = PIN5
SYMBOL  Box          = PIN0
' -----[ Constants ]-------------------------------------------------------
SYMBOL  NotPressed      = 0                     ' for active-low
SYMBOL  Pressed         = 1
SYMBOL  IsOn            = 1
SYMBOL  IsOff           = 0
' -----[ Variables ]-------------------------------------------------------

SYMBOL  timer           = W4                    ' for event timing

' -----[ Initialization ]--------------------------------------------------
Powerup:
  PAUSE 1000
Reset:
  PINS = %00000000                              ' clear all outputs
  DIRS = %00000001                              ' make P0-P1 outputs
  timer = 0
' -----[ Program Code ]----------------------------------------------------
Main:
timer = 0

Check_Trigger:
PAUSE 5                                       ' loop pad
timer = timer + 5
IF Reg1 = NotPressed THEN Main
IF timer < 100 THEN Check_Trigger                 ' wait for 0.1 sec input
DEBUG Reg1

Check_Trigger2:
timer = 0
PAUSE 5                                       ' loop pad
timer = timer + 5
IF Reg2 = NotPressed THEN Main
IF timer < 100 THEN Check_Trigger2             ' wait for 0.1 sec input
DEBUG Reg2

Check_Trigger3:
timer = 0
PAUSE 5                                       ' loop pad
timer = timer + 5
IF Reg3 = NotPressed THEN Main
IF timer < 100 THEN Check_Trigger3             ' wait for 0.1 sec input
DEBUG Reg3

Box = IsOn
PAUSE 30000
Box = IsOff

GOTO Reset


' -----[ Subroutines ]-----------------------------------------------------

' -------------------------------------------------------------------------

' -----[ User Data ]-------------------------------------------------------
Brian
Evilusions LLC
www.evilusions.com for all your pneumatic components

JackMan

The problem is where you have  "timer = 0" located for Trigger2 and Trigger3. Move it above each of those sections.

JonnyMac

August 22, 2015, 07:38:22 AM #2 Last Edit: August 22, 2015, 07:41:04 AM by JonnyMac
Here's how I scan multiple inputs:

' =========================================================================
'
'   File......
'   Purpose...
'   Author....
'   E-mail....
'
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================

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

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

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

SYMBOL  Reg1         = PIN7
SYMBOL  Reg2         = PIN6
SYMBOL  Reg3         = PIN5
SYMBOL  Box          = PIN0


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

SYMBOL  NotPressed      = 0                     ' for active-low
SYMBOL  Pressed         = 1
SYMBOL  IsOn            = 1
SYMBOL  IsOff           = 0


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

SYMBOL  regs            = B2                    ' button inputs
SYMBOL  idx             = B3                    ' loop control


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

Power_Up:
  PAUSE 1000

Reset:
  PINS = %00000000                              ' clear all pins
  DIRS = %00000001                              ' make P0 an output


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

Main:
  regs = %11100000                              ' enable Reg1-Reg3
  FOR idx = 1 TO 10                             ' 10 x 5ms = 50ms
    PAUSE 5                                     ' hold
    regs = regs & PINS                          ' scan Reg1-Reg3
  next
  IF regs <> %11100000 THEN Main


  Box = IsOn
  PAUSE 30000

  GOTO Reset


' -----[ Subroutines ]-----------------------------------------------------

' -------------------------------------------------------------------------

' -----[ User Data ]-------------------------------------------------------



As you can see, this is a smaller program and doesn't use any more variable space (I traded your Word variable for two Byte variables).

Here's what's happening: We preload the variable regs with the value the corresponds to all three inputs being active (%11100000); this enables those inputs. Then we drop into a loop where we scan the inputs (PINS) and do a logical AND on the regs variable.  Here's how AND (&) works

1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0

Keep in mind that this logic is applied bit-by-bit for everything in regs. Only the upper three bits have a chance because we pre-loaded those bits with 1 at the top of the loop.

So you see, the loop is verifying that the three inputs are active and stay active during the loop. When that is the case, all three bits in regs will be set and we will activate the box output channel.

Here's an example: When we enter the loop only Reg1 is pressed; that would create a value of %10000000 on PINS.

%11100000 & %10000000 = %10000000

At the end of the loop the value in regs is no longer %11100000 as required, so we jump back to Main and try again.
Jon McPhalen
EFX-TEK Hollywood Office

gadget-evilusions

Jack, Thank you. After working for 16 hours, I couldn't figure out what I did wrong as I was falling asleep at the computer. I don't normally make simple mistakes like that.

Jon, thanks for a much better what to do what I wanted to. I appreciate your time and the information.
Brian
Evilusions LLC
www.evilusions.com for all your pneumatic components

JackMan

Your welcome Brian. We've all been there, staring at something when you're already tired and it just doesn't click.  ;)

JonnyMac

When I'm stuck on code I will force myself to step away from it and do something else -- going back with fresh eyes is helpful. That said, the bug Jack pointed out is subtle, and very easy to miss. Good catch, Jack.
Jon McPhalen
EFX-TEK Hollywood Office