Equip your Genius

Welcome to the Parallax Discussion Forums, sign-up to participate.

Magnetic switch to mechanical counter

Posts: 1,698
Hello!
I have two mechanical counters here, one uses 24v to click its armature to increment its displayed count. And the uses 12v to do the same thing. And also a collection of NC (Normally Closed) reed switches, and a collection of NO (Normally Open) switches as well. I'm brainstorming for a simple program that would wait for the switch to either open in the case of NC ones, or conversely close for the NO ones, and then cause a connected solid state relay to have one of the two, simply count.

It's the glue in the form of a BASIC program that has me peeved.
---
Mascot away.

Comments

• Posts: 22,927
There are all sorts of simple examples in "What's a Microcontroller?" for testing switches and activating something (like a solid state relay) when the switch is opened or closed or whatever. How about making up a prose description of what you want your gadget to do? You could do a flowchart if you want, but that's more work to post. Once you have a description that you're happy with, it's easy to translate it to Basic.
• Posts: 6,414
edited 2019-10-03 - 23:16:14
With XOR (^) you can determine which inputs changed, irrespective of how they changed. For example, let's say that IN0 and IN1 are normally open and IN2 and IN3 are normally closed. All inputs have pull-downs, and the other side of the switch connects to 5v.

This bit of code samples the inputs and waits for a change. By using XOR we can find the pins that changed state -- a "1" bit in delta tells us the corresponding input changed. By looking at the actual value in the last scan, we can respond to that input as required.
```' {\$STAMP BS2}
' {\$PBASIC 2.5}

oldState        VAR     Nib
newState        VAR     Nib
delta           VAR     Nib

Setup:
DIRA = %0000                                  ' all inputs
oldState = INA

Main:
PAUSE 25
newState = INA
IF newState = oldState THEN Main

Change_Detected:
delta = newState ^ oldState
oldState = newState

Process_Input0:
IF delta.BIT0 = 1 THEN
IF newState.BIT0 = 1 THEN                   ' normally-open input
' active
ELSE
' released
ENDIF
ENDIF

Process_Input1:
IF delta.BIT1 = 1 THEN
IF newState.BIT1 = 1 THEN                   ' normally-open input
' active
ELSE
' released
ENDIF
ENDIF

Process_Input2:
IF delta.BIT2 = 1 THEN
IF newState.BIT2 = 0 THEN                   ' normally-closed input
' active
ELSE
' released
ENDIF
ENDIF

Process_Input3:
IF delta.BIT3 = 1 THEN
IF newState.BIT3 = 0 THEN                   ' normally-closed input
' active
ELSE
' released
ENDIF
ENDIF

Clean_Up:
GOTO Main
```
Sign In or Register to comment.