Edge Trigger

otm

Member
Just wondering if its possible to set up an edge trigger within Picaxe 28X code with 4 input pins. Doesn't matter if you can't.

Cheers,

Owen

PS. If you can is it possible, then can i have the answer in BASIC.
 

Fowkc

Senior Member
Do you mean an interrupt?

If so, then yes, you can. Get the Basic Commands PDF and look up the SETINT command.

You specifiy which pins to monitor, and what condition to look for. When that condition is met, the program will jump to an interrupt routine. For example:

SETINT %00001111,%00001111

The first %00001111 tells the interrupt to look for that binary sequence on the input pins.
The second %00001111 tells it to only look at the first four pins, and ignore the rest. So it would trigger when the pin state was XXXX1111, where X means "it doesn't matter about this pin".
 

otm

Member
I am creating a project for my GCSE where children put a shape into a cutout eg circle can i use the setint to act as a edge trigger on one or more of the inputs. for example the child puts in a cirlce, the rest of the program runs, however if they leave the spahe in the program keeps running. on and on and on...

So can i use the setint to act as the edge trigger so if they leave the shape in the programe will ignore it the second time, until the shape has been removed (in my case turn from 0 (on) back to 1 (off)) and it will keep going exculding more shapes as they leave them in.

To help heres my current Pic programme so you can have more of an idea as to what my project does..

i2cslave $c4,i2cfast,i2cbyte
readi2c 1, (b2)
debug b2

main:
GoSub prc_WELCOME
GoTo label_21
label_21:
If pin0 = 0 Then label_22
If pin1 = 0 Then label_23
If pin2 = 0 Then label_24
If pin5 = 0 Then label_25
GoTo label_21


label_22:
serout 7,4,(254,1)
pause 1000
writei2c 0, (0,0,5,2,"Triangle T. R. I. A. n. G. L. e. Triangle.",0)
serout 7,4,("A Triangle")
writei2c 0, (64)
pause 2000
serout 7,4,(254,1)
pause 500
serout 7,4,("T")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("TR")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("TRI")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("TRIA")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("TRIAN")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("TRIANG")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("TRIANGL")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("TRIANGLE")
pause 2000
serout 7,4,(254,1)
pause 500
serout 7,4,("A Triangle")
pause 2000
serout 7,4,(254,1)
GoTo label_26

label_23:
serout 7,4,(254,1)
pause 1000
writei2c 0, (0,0,5,2,"Circle. C. I. R. C. L. e. Circle. ",0)
serout 7,4,("A Circle")
writei2c 0,(64)
pause 2000
serout 7,4,(254,1)
pause 500
serout 7,4,("C")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("CI")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("CIR")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("CIRC")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("CIRCL")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("CIRCLE")
pause 2000
serout 7,4,(254,1)
pause 500
serout 7,4,("A Circle")
pause 2000
serout 7,4,(254,1)
GoTo label_26

label_24:
serout 7,4,(254,1)
pause 1000
writei2c 0, (0,0,5,2,"Square. s. q. U. A. R. e. Square.",0)
serout 7,4,("A Square")
writei2c 0,(64)
pause 2000
serout 7,4,(254,1)
pause 500
serout 7,4,("S")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("SQ")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("SQU")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("SQUA")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("SQUAR")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("SQUARE")
pause 2000
serout 7,4,(254,1)
pause 500
serout 7,4,("A Square")
pause 2000
serout 7,4,(254,1)
GoTo label_26

label_25:
serout 7,4,(254,1)
pause 1000
writei2c 0, (0,0,5,2,"Rectangle. R. e. C. T. A. n. G. L. e. Rectangle.",0)
serout 7,4,("A Rectangle")
writei2c 0,(64)
pause 2000
serout 7,4,(254,1)
pause 500
serout 7,4,("R")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("RE")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("REC")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("RECT")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("RECTA")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("RECTAN")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("RECTANG")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("RECTANGL")
pause 1000
serout 7,4,(254,1)
pause 500
serout 7,4,("RECTANGLE")
pause 2000
serout 7,4,(254,1)
pause 500
serout 7,4,("A Rectangle")
pause 2000
serout 7,4,(254,1)
pause 1000
GoTo label_26

label_26:
End

prc_WELCOME:
serout 7,4,(254,1)
pause 1000
writei2c 0, (0,0,5,5,"Please place A shape",0)
serout 7,4,("Please place a")
serout 7,4,(254,192)
serout 7,4,("Shape!")
writei2c 0,(64)
pause 4000
serout 7,4,(254,1)
pause 1000
serout 7,4,("Please place a")
serout 7,4,(254,192)
serout 7,4,("Shape!")
pause 4000
serout 7,4,(254,1)
pause 500
Return

at label 26 it terminates, so they would have to place a reset switch on pin 1 to start the program again, however, like i said if they leave the sahpe in the program repeats itself..


I know its confusing but any help will be Great, and it doesn't matter if you can't....


Thankyou,

Owen
 

BeanieBots

Moderator
Still a little confusing EXACTLY what you are trying to do. I'm guessing that each shape activates a different set of switches. If this is the case, I would forget interrupts (at least for now, unless you are confident about how to use them) and set up a program loop that checks the inputs.
Use a command such as b0=pins to read all the inputs at the same time.
Mask off any inputs that you don't care about. (please ask if you are unsure about how do that) and then jump to whatever routine does the required action for that particular set of inputs.

For example:-
b0=pins
b0=b0 & $0F 'don't care about bits 4 to 7

If b0=0 then... no inputs activated
if b0=1 then... only input 1 activated
if b0=2 then... only input 2 activated
if b0=3 then... input 1 AND input 2 activated
etc, etc.

There are much better ways of writing the code such as branch and/or select case but the above should give you an idea of the principal.
 

otm

Member
Sorry butDont understand the last post and you dont understande exactly what i'm getting at... so we are both on the same level...

Each shape is a switch and it sets off the AXE033 lcd display and the SPE030 speech synth. If the leave the shape in after the program has run once it would do the same thing again...

I am confused about variables because I am a complete newcomer to BASIC and Picaxe chips.

The easiest way to describe it is that i want to make it see if all pins = 0 a the vey begining of the program ( if 1 out of the four shapes has been left in when it loops then the pin will equal 0 ) and to make any that are 1, so it will ignore them.

Does that make it any clearer? can you explain in more detail the use of variables if they are the solution.

Thankyou Very much.

Owen
 

BeanieBots

Moderator
Is it,
triangle activates switch 1
rectangle activates switch 2 and so on?
If the shape is left in place, then keep repeating or stop?

If I have understood correctly, then it should be quite easy.

Start:
if pin1=1 then triangle
if pin2=1 then rectangle
...
goto start

triangle:
do whatever for triangle
goto start

rectangle:
do whatever for rectangle
goto start

Unfortunatley I have to go off-line now but hopefully somebody else can produce something with a bit more detail for you.
 
The only thing missing from the last post is a state variable. I'm brand new to the PICAXE so I can't write the code for you (I just got my first 08M and 18X last night (Thanks Brian)) but I can write pseudo code...

variable state
variable lastState

START:
input state

if state <> lastState
Save the new state
else
loop to START

'Figure out what changed and act on it.
if pin1 gosub PIN1
if pin2 gosub PIN2
.
.
.
goto START

PIN1:
Do pin 1 stuff here.
return

PIN2:
Do pin2 stuff here.
return
 
And of course, as soon as I hit post I recognized the problem in the pseudo code. The main loop should look like this instead:

START:

if state == savedState goto START

' Figure out what changed

if state[0] <> savedState[0] then gosub PIN0
if state[1] <> savedState[1] then gosub PIN1
.
.
.
' AFTER you check all the pins then...
savedState = state

goto START
 

otm

Member
Now i've never used any puesudo code ever ( to be honest I dont even know what the word means) but from reading code if i were to fill it out like so would it work or not:

START:

if state == 0 goto START

' Figure out what changed

if state[0] <> savedState[0] then gosub Shape 1
if state[1] <> savedState[0] then gosub shape 2

' AFTER you check all the pins then...
savedState = state

goto START

To be honest I haven't got a clue, its all a bit too complicated. I too have only had two picaxe projects the first being a simple 08M which was simple and this, project which i seen to have thrown into the deep end.

For anyone who is still confused, because i think i've confused you with what i want.

i only want one of the 4 magnetic switches in my circuit to trip once. a shape is placed, and that triggers the output, when the program loops if the magnet switch is still tripped the same output will be tripped again but i dont want that.

i think the puesdo code does what I want, I think it checks the state of all switchs before running the rest of the programme from my descicion commands. if any switch registers as triggered i would want the programme to ignore it so the putput corresponding to that switch never triggers, until the switch is untriggered by removing the shape.

Does that help?

can someone extend in a more detailed way the use of puesdo code..

Sorry for being such a pain (you proberly want to throttle me).

Owen
 

BeanieBots

Moderator
pseudo code is just a very quick way of describing a program. It's a sort of middle road between a flow diagram and the full correct syntax code.

a bit like this:-

if input is active then
turn on LED
else
turn off led
end if

as apposed to:-
if pin1=0 then
high 1
else
low 1
endif

which would not be quite as obvious what is going on.

Your problem, in pseudo code.

Start:
Check inputs
if one is activated that reperesents a shape, then goto that shape's routine
Go back to start

first shape routine:
do whatever is needed.
goto end routine

.. other shape routines here

end routine
if inputs are cleared then
goto start
else
go back to end routine
end if

hope that helps

 

otm

Member
cheers, that clears up puesdo code, but how do i no use it to make an edge trigger, what code can i use to make the PIC think that input pin 1 has a logic level of 1 even if it is accutally logic level 0?

Cheers

PS. the chip i am using is a PIC28X (Because I know some code is only avaliable in certain PICS)

Owen
 

Rickharris

Senior Member
If i understand correctly you want a toy where putting a shape in a hole triggers a visual and audio response for that shape. Then if that shape is left in the hole you want it to be ignored and any new shapes to be detected.

I guess if a shape is removed you want that to be noticed and the system reset so it can be recognised again if it is replaced.

Ok you need to detect shape in/shape out. this could easily be a physical switch/magnetic device/light operated sensor.

Example
Start:
If a shape is in set a flag for that shape
do the routine for that shape.
Check for any shapes in.
If flag is set ignore that shape.
if shape is removed reset the flag for that shape.



This flag is just a software note that the shape has been put in/taken out a code you can use a variable for this setting it to 0 if the slot is empty and to 1 of the shape is in. It is easy to check the flag when you scan the holes to see if a shape has already been noted (flag=1)

On the other hand you can cure this mechanically perhaps by pushing the shapes out with a spring so they cant be left in the holes, or let the fall though to be use again.


 

otm

Member
That is exactly what I want.... Thankyou.

The mechanical option is an idea however my school has limited resorces so it proberly isn't viable.

Like i have said, I am completly new to me so can you please walk through the code snipits of code I would need to set up the flags.

Thankyou Very much, I know I've made a big fuss, but every little helps.

Thankyou again.

Owen
 

premelec

Senior Member
You perhaps need to start with one shape and the logic required:

top:

'look for shape1 and done flag not set
IF pin1 AND NOT flag1 THEN shape1

'detected shape flag been done shape gone:
IF NOT pin1 AND flag1 THEN shape1clear

shape1:
flag1 = 1 ' note that shape1 has been found
'do shape one stuff here outputs etc
GOTO top

shape1clear: 'clear the flag
flag1 = 0 'shape not found or reported
GOTO top

[or you can always clear flag1 when pin1 = 0]

In short check state of each shape switch over and over and when _first_ found do something - flag it when done - unflag it when shape removed... Set up for only one item - test out then set up for another - hope for enough variables for whole project or resort to peek and poke flags... or other tricks - remember bit#... variables can hold 0 and 1 to serve as flags. Hope this helps - start small and work on up! I need sleep - hope I haven't got too messy here :)
 

otm

Member
so see if I've got this right

top:

'look for shape1 and done flag not set
IF pin1=0 AND NOT flag1=1 THEN shape1
IF pin1=1 AND flag1=1 THEN shape1clear

shape1:
flag1 = 1 ' note that shape1 has been found
'do shape one stuff here outputs etc
GOTO top

shape1clear: 'clear the flag
flag1 = 0 'shape not found or reported
GOTO top

if this is wrong please tell me, (because i've never been sure that AND, or NOT are even basic commands. I'm totally confused, you are really going to have to spell things out to me because i just unfortunalty don't get it) Hoever, if it is correct can I just multipy it 3 times, With this last final check Thankyou Very much

Owen


Edited by - otm on 24/01/2007 20:09:54
 

premelec

Senior Member
I haven't run your code so I don't know if it works - do you have the editor? There is a 'syntax check' option that will check if the terms you used in your program are correct though it won't tell you if you are doing what you want to do....

top:

IF bit1 = 0 AND bit1 = 1 THEN doit

doit:
b5 = b4 + b6 ' do whatever you want here...
GOTO top

Will run for instance - when dealing with a variable, which is 0 or 1 only, bit1 = NOT 0 must be the same as bit1 = 1....

Please look further at manual 2 for operators, variables and syntax - and by all means write some code and check the syntax with the editor... this is how we learn - a certain amount of logic and some empiricism.

Yes your endeavor is not too difficult to accompish and you CAN do it! _You_ need to write the code and debug it - it's a sort of a puzzle that when solved will be satisfying... For specific parts of the solution you can get help in this forum... I hope... they certainly have helped me at times... give it a go...
 

Rickharris

Senior Member
You have got the idea, as said the best way forward now is to try it out and refine the programme. this isn't going to need much in the way of test hardware, just a few switches and LEDs - then bingo you have the programme and can build the final hardware.

Go for it <img src="smile.gif" width=15 height=15 align=middle>

PS. As a student you have a responsibility to do the work once pointed in the right direction. As teachers (and I am) I do not do students projects for them - although we help and point out directions no one else here will either so off you go and gain the experience.

Remember &quot;education is what is left after you forget what you learned&quot;.

Edited by - rickharris on 25/01/2007 21:34:36
 

otm

Member
I know what you are saying about education and not letting others do it for you, which I haven't.... I've worked all of it out so far by myself.. However, you can't learn if you don't know what your being taught. I still now don't understand the use of variables because i've never used them before. For instance how do I set a to a specific state, and how do I make the software read it, because once I've got that I can incorporate it into some decision commands and hey presto I an Edge trigger.

All I need is a more comprehensive understanding of how to use variables, once i've got that i can sort out the rest, because i've looked at the manual, and it makes just as much sense to me as looking at it in Italian.


Thankyou very much

Owen

 

Rickharris

Senior Member
The picaxe in general has up to 14 variables. they are locations in memory that the computer can use to do maths. They are identified by a set of pre set labels B0, B1, B2,B3 up to B13.

So Let B1=10 will put the value 10 into the location B1.

Let B4=2 will put 2 into B4

B5=B1+B4 will result in B5 containing the value 12 (10+2).

Because you can set these variables to any value you want (they clear when the picaxe is switched off by the way) you can use them to keep a log of things happening (i.e. as what programmers call flags)

So B6=1 will set B6 to the value 1 when you need to note something has happened.

The code to read this looks something like this:

<code><pre><font size=2 face='Courier'>
start:

if pin3=1 then flag ' sets B6 to 1 if input 3 is triggered - B6 will remember this even if input 3 is reset.


'Lots of other code....


if b6=1 then alarm 'read the value of B6 (the flag) and trigger an alarm if B6 contains 1

goto start

flag:
B6=1
goto start


Alarm:
B6=0 'reset B6 to 0
high 4 ' set alarm on to alert user.
goto start

</font></pre></code>

This is not working code as such but illustrates how a variable i.e. flag can be used to remember an event that has happened and now reset or stopped.

Edited by - rickharris on 25/01/2007 23:02:41
 

premelec

Senior Member
One caveat - byte variables b0 to b14 can only hold 8 bits - 0-255 - as values and if you exceed the capacity of the variable with your math it will usually wrap around through 0. Word variables are 16 bits 0-2^16 -1 however in the PICAXE a word variable is made up of 2 byte variables - e.g. W0 is b0 and b1 so if you change b0 or b1 w0 will also change and if w0 changes b0 or b1 or both change... these are just rules to be observed and learned - for this particular microcirchip.... read the manual and try stuff - the light will dawn!
 

adub

New Member
To continue the above...

As you are setting each &quot;flag&quot; to either 0 for off and 1 for on you could quickly use up the 14 available variables. There are two variables that can be expanded to use all 8 bits that make up the one byte. b0 and b1 make 16 bit variables available to you.

b0 is made up of bit0, bit1, bit2...bit7
b1 is bit8 though bit15.
These 16 bit variables can only hold 0 or 1, but that's all you need for flags.

There is a good read about variables on page 8 of the picaxe_manual_2.pdf that's easy to find by clicking help from the programming editor.

Enjoy the puzzle.
Arvin
 

otm

Member
thankyou very much, sorry for the late reply. i've now made an edge trigger using variables, however, when the shape is pulled out, it does the output for it again, so it does it once when you first place the shape, and once again when you pull it out, now I don't paticulally want this,but i can't seem to fathom why it does it, it's proberly really simple, but heck it's me we're talking about.

Heres my edge trigger code:



reset1a:

If pin0 = 1 Then reset1b
GoTo reset2a

reset2a:

If pin1 = 1 Then reset2b
GoTo reset3a

reset3a:

If pin2 = 1 Then reset3b
GoTo reset4a

reset4a:

If pin5 = 1 Then reset4b
GoTo label_21a

label_21a:

If pin0 = 0 Then var1
GoTo label_22a

label_22a:

If pin1 = 0 Then var2
GoTo label_23a

label_23a:

If pin2 = 0 Then var3
GoTo label_24a

label_24a:

If pin5 = 0 Then var4
GoTo reset1a

var1:

If b0 = 1 Then label_22a
GoTo label_21b

var2:

If b1 = 1 Then label_23a
GoTo label_22b

var3:

If b2 = 1 Then label_24a
GoTo label_23b

var4:

If b3 = 1 Then label_21a
GoTo label_24b



reset1b:

b0 = 0
GoTo reset2a

reset2b:

b1 = 0
GoTo reset3a

reset3b:

b2 = 0
GoTo reset4a

reset4b:

b3 = 0
GoTo label_21a

label_21b:

b0 = 1
'rest of outputs...
GoTo label_21a

label_22b:

b1 = 1
'rest of outputs...


label_23b:

b2 = 1
'rest of outputs...
GoTo label_21a

label_24b:

b3 = 1
'rest of outputs...
GoTo label_21a

Can anyone see a problem?

Thankyou very much

Owen
 

premelec

Senior Member
this may not help much but I can't take the time to go through your code -

My suggestion is Symbol Labels!

e.g. the switch input on a triangle could be
SYMBOL Tri_Sw = pin1

This makes it easier to follow what's happening when the variables are language related to your real world device...

Code that has too many references to obscure places - GOTOs - is referred to a spagetti or tangled code... hard to follow around the jumps... make the labels mean something in the real word too....

e.g.

NOW_SET_Tri_Sw_0:
Tri_Sw = 0
GOTO xxx

This also makes it possible to remember what you are doing when you look at the code a year later :) Use REMs too...

You are getting there - persist!
 

otm

Member
Thanks for that i'll rembember that, i may even add it to my current project.. However, I still don't seem to understand why it's not working. The reason why I've got so many goto's is because I wanted each pin to be a different location so I can tell the program to skip to a certaian part of the project, not just a small part.

Any ideas as to why its not working, will be much appreciated.

Thankyou,

Owen
 

SD2100

New Member
You might be getting too tangled up using goto's. This might work ???, it checks if a shape has been put into a hole then displays a message on the LCD and a flag is set so the message is only displayed once. The program then continues checking for other shapes. If a shape is removed then the flag for that shape is cleared, if the same shape is put back in then the message is displayed again.

<code><pre><font size=2 face='Courier'>
#picaxe 28x
symbol flags = b0
symbol PinState = b1

Main:
if pin0 = 1 and bit0 = 0 then
gosub Triangle
elseif pin1 = 1 and bit1 = 0 then
gosub Circle
elseif pin2 = 1 and bit2 = 0 then
gosub Square
elseif pin3 = 1 and bit3 = 0 then
gosub Rectangle
end if
gosub CheckIfShapeRemoved
goto Main

Triangle:
bit0 = 1
'LCD Triangle display code goes here
return

Circle:
bit1 = 1
'LCD Circle display code goes here
return

Square:
bit2 = 1
'LCD Square display code goes here
return

Rectangle:
bit3 = 1
'LCD Rectangle display code goes here
return

CheckIfShapeRemoved:
PinState = pins &amp; $0f
if PinState &lt; flags then
flags = PinState
end if
return
</font></pre></code>


Edited by - Phil75 on 31/01/2007 12:22:48
 

otm

Member
for some reason everytime I put more than 1 Gosub into my program the software has an issue with it, any ideas why?

just says: Error on this line, when i getrid of the GoSub,it moves to the next line?

Thanks,

Owen
 

Dippy

Moderator
Post an example of your problem, just a simple dozen lines, so everyone can see how you are calling and returning from the GoSub.

You'll probably discover you're doing something wrong.
 

otm

Member
all works however,

edit: again..... It does work beautifully, once all the shapes have been place in andyou pull one out and put it in again it works, however the last shape you put in doesn't run again,

so if i put in:

circle,
square,
rectangle,
triangle,

and put them out and place triangle it doesn't run again, unlike the rest that do, i think this is because it was the last to switch.


However,major progress made thankyou,

Owen.

Edited by - otm on 31/01/2007 22:21:51

Edited by - otm on 31/01/2007 22:22:32

Edited by - otm on 31/01/2007 22:31:54
 

SD2100

New Member
If you are using what I posted above and you havn't got the &quot;CheckIfShapeRemoved&quot; sub right it won't reset the flag when the shape is removed, this will cause it to not
respond when the shape is put back in.
 

otm

Member
your going to have to explain that one.

I have used your checkifshaperemoved sub, it means that the only shape not to respond again when all have been placed in and taken out is the last shape placed if that makes any sense at all..

Owen.
 

lbenson

Senior Member
Try this.
<code><pre><font size=2 face='Courier'>
CheckIfShapeRemoved:
PinState = pins &amp; $0f
flags = PinState
return
</font></pre></code>

You might put a pause in before &quot;goto main&quot; to reduce the likelyhood that in your fast-cycling code a change will occur between your &quot;if&quot;s and your &quot;check&quot;--that will make the change more likely to occur during the pause.
 

otm

Member
I've simulated the whole program in the programmer (I didn't realise you could simulate the code, I change some options and up it popped!)

with your alternative idea,unfortunatly accroding the the program it doesn't even latch, it just keeps on running the program,

I like it at the moment apart from that 1 point about the last shape, if you could make it so the last shape does respond when replaced I'dd be over the moon....

Thankyou for your suggestion,

Owen.
 

SD2100

New Member
Don't know what you mean by dosn't &quot;latch&quot; ?? the program should keep cycling and checking for inputs.

In the simluator watch the FLAGS and PINSTATE variables, with no shapes in the holes flags and pinstate should = 0, as shapes are inserted the value should increase to a maximum of 15

Triangle = 1
Circle = 2
Square = 4
Rectangle = 8

as shapes are removed the value of the shape will be subtracted from flags until all are removed which will = 0.

Edited by - Phil75 on 01/02/2007 01:19:37
 

otm

Member
I want the shapes to latch when they are placed in..... so shape one goes in it latches and the same for each of the shapes, when all the shapes are in nothing should happen, then, when a shape is pulled out it unlatches (but doesn't run the program again) so if i were to put it in again, it would run the program and latch, and it would be able to do that for all the shapes...

Thankyou,

Owen

P.S there is no order to the shapes, they should latch when anyone is paced in in any order..


Edited by - otm on 01/02/2007 22:30:08
 

premelec

Senior Member
As long as you check the shape switch in the loop - looking to see if it's still there and needs to send a re-insertion message - it will not show anyting and be 'latched' in effect - when the loop finds the shape no longer there when it looks at that switch it will set an 'unlatch' bit/flag associated with that shape. The shape flag is in effect a 'latch' showing condition of the latch function.
 

SD2100

New Member
I'm pretty sure the program I posted previously is doing what you want.

1. If a shape is inserted a message is sent to the LCD (once only) ---- &quot;Latched&quot;
2. If that shape is removed nothing happens. (no message to the LCD) --- &quot;Unlatched&quot;
3. If that shape is re-inserted the message is sent to the LCD again (once only) --- &quot;Latched again&quot;

Shapes can be inserted and removed in any order

Any shape can be removed and when put back in it's message will be displayed again

As each shape is inserted it is &quot;latched&quot; so the message is only sent once, If your looking at the program in the simulator and your expecting it to stop it won't, it needs to keep running and checking for shapes that might have been put in or removed. If something happens then the Flag for that shape is either set or cleared &quot;latched or unlatched&quot;

I've tested the code in the simulator and even put it in a 28x to make sure, I test the circuit using LED's on the 28x outputs, so when I switched an input (for a shape) the corresponding LED would flash once this indicated a message sent to the LCD. (once only per insertion of shape).

If the shapes are left in and the circuit is turned off then back on or the reset button is pressed then the LCD will display a message once for each shape that is in the hole, this is because the flags variable is set to zero at startup. The program will cycle through checking for shapes and setting flags as each shape is found. The shapes just need to be pulled from the holes and the flags will be cleared.

Also how have you setup your input switches with pull down resistors ???, if the inputs are floating while the switches are off then this will cause false inputs and the program will think shapes are inserted and removed when their not.


Edited by - Phil75 on 02/02/2007 04:59:57
 

otm

Member
I haven't actually built the entire project, yet.. I'm in the process of making the case, so i've only been going on the simulation, i don't have enough magnets (yet..) to test the real circuit, because i'm using magnet switches with a 100k resistor above it, so in normal mode the voltage at the input pins is 1 (around 4.95v) when the magnet gets near the swith it goes to 0v because i haven't got enough magnets I can only go by the simulation of the code, which may possibly be inaccurate.

I'll reply once I've got things sorted (near to the end of next week (7th - 9th of feb.) hopefully it will be good feedback.

Owen

Edited by - otm on 02/02/2007 21:29:55
 

SD2100

New Member
The program I posted was to send a message to the LCD when an input goes high (shape inserted), as you are going to be using the opposite (low for shape inserted) this will cause a problem in the program.

There are two options:

1. Change your switch setup so when a shape is inserted the input goes high and leave the program as is, or

2. stay with your switch setup (low when shape inserted) and use the modified program below.
<code><pre><font size=2 face='Courier'>

#picaxe 28x
symbol flags = b0
symbol PinState = b1

flags = 15

Main:
if pin0 = 0 and bit0 = 1 then
gosub Triangle
elseif pin1 = 0 and bit1 = 1 then
gosub Circle
elseif pin2 = 0 and bit2 = 1 then
gosub Square
elseif pin3 = 0 and bit3 = 1 then
gosub Rectangle
end if
gosub CheckIfShapeRemoved
goto Main

Triangle:
bit0 = 0
'LCD Triangle display code goes here
return

Circle:
bit1 = 0
'LCD Circle display code goes here
return

Square:
bit2 = 0
'LCD Square display code goes here
return

Rectangle:
bit3 = 0
'LCD Rectangle display code goes here
return

CheckIfShapeRemoved:
PinState = pins &amp; $0f
if PinState &gt; flags then
flags = PinState
end if
return
</font></pre></code>
Each switch needs to be connected as shown below for the program above to work properly.
I think this is what your intending to do anyway.

<code><pre><font size=2 face='Courier'>
28x
5v __
+ -o| |o-
| -o| |o-
.-. -o| |o-
N/O Res| | -o| |o-
Reed | | -o| |o-
switch '-' -o| |o-
_/ | -o| |o-
GND -----o/ o-----o------o|__|o-

.---.
'---'
Magnet
</font></pre></code>

Good luck...


Edited by - Phil75 on 03/02/2007 13:06:59
 

otm

Member
Thanks that really works, better (in the simulation.. ) however, I've noticed that the code seems to be geared to using pins 0, 1, 2, and pin3 however, i'm using I2C in my system, so i can't use pins 3, and 4 so my configuration uses pin 0, 1, 2, and 5, which I have just worked out why pin 5 latches but never unlatches, sorry to throw a bomb into the works....

Owen

Edited by - otm on 04/02/2007 00:15:13
 

SD2100

New Member
That's ok
I changed the following so pin5 can be used instead of pin3

Changed input pin from pin3 to pin5
Changed flags value form 15 to 39
Changed from flag bit3 to bit5
Changed mask from %00001111($0F) to %00100111(39)

See how it goes...
<code><pre><font size=2 face='Courier'>
#picaxe 28x
symbol flags = b0
symbol PinState = b1

flags = 39

Main:
if pin0 = 0 and bit0 = 1 then
gosub Triangle
elseif pin1 = 0 and bit1 = 1 then
gosub Circle
elseif pin2 = 0 and bit2 = 1 then
gosub Square
elseif pin5 = 0 and bit5 = 1 then
gosub Rectangle
end if
gosub CheckIfShapeRemoved
goto Main

Triangle:
bit0 = 0
'LCD Triangle display code goes here
return

Circle:
bit1 = 0
'LCD Circle display code goes here
return

Square:
bit2 = 0
'LCD Square display code goes here
return

Rectangle:
bit5 = 0
'LCD Rectangle display code goes here
return

CheckIfShapeRemoved:
PinState = pins &amp; %00100111
if PinState &gt; flags then
flags = PinState
end if
return
</font></pre></code>
 
Top