setint - see a problem with this code?

fred_b

Member
I have a PIR sensor that goes high when motion is sensed. It is connected to pin 2 on my 18m2 chip. I have a pulldown resistor on pin 2 as well. I use the download cable and terminal for debugging.

When I run the following code, the terminal displays "0 0 0 0 0 0 0 ...." and so on until motion is sensed and then it displays " 252 252 252 252 252..." for a few seconds while the receiver is activated.

pir:
readadc 2,w0
sertxd (#w0," ")
goto pir

So that works fine and shows that pin 2 goes high when it should (when motion is sensed).

I want an interrupt to happen when motion is sensed so I changed the code to:

setint #00000100,#00000100
pir:
readadc 2,w0
sertxd (#w0," ")
goto pir

interrupt:
sertxd ("interrupt")
end

When I run this, it displays the adc value just like before. When triggered, it still displays 252 in the terminal but doesn't go to the interrupt subroutine.

What am I missing? (probably something simple!)
 

kevrus

New Member
Fred_B, just an observation, but if the PIR switches from low to high, as suggested in the first line of your post, why are you testing for an analogue input? Perhaps the interrupt doesn't work with analogue inputs. Analogue inputs are usually used to measure a varying voltage, and not just a change of state from low to high.
Don't forget to re-issue the interrupt and the interrupt routine needs to end in a 'return', like this

Code:
#picaxe 18m2


setint %00000100,%00000100		'sets interrupt on pin2 going high 


	do
	'possibly do something here
	loop 

interrupt:
	sertxd ("interrupt")		'display this on PC
	 do
	 loop until pinC.2 = 0		'required if you only want to see one message on screen
	setint %00000100,%00000100	'always re-issue the interrupt before
	return				'returning back
 
Last edited:

hippy

Ex-Staff (retired)
Perhaps the interrupt doesn't work with analogue inputs.
That's correct. For PICAXE M2 the READADC command automatically sets the pins as analogue inputs and, when analogue, if read as digital they will only return low levels regardless.

There should be no need to read the PIR output as analogue; just read pin 2 as digital ...

Do
SerTxd( #pinC.2 )
Loop

If that shows "0" normally, and "1" when the PIR is activated, then it should continue to work when using interrupts.
 

fred_b

Member
Ahh ha. Thank you.

The pin is used in an earlier part of the program as an analogue input.

I will need to configure it to a digital input after that:

I tried this:

input 2
pir:
sertxd (".")
if pin2=1 then
sertxd("high")
endif
goto pir

I must be rusty at my programming because this does not work either. (Does not send "high" to terminal when the sensor is activated)
 
Last edited:

lbenson

Senior Member
Note that this program (essentially functionally equivalent) does work in the simulator, which indicates that something is wrong with your hardware setup.
Code:
#picaxe 18m2

  input 2
pir:
  if pin2=1 then 
    sertxd("1")
  else
    sertxd ("0")
  endif
  pause 500
  goto pir
Or as was suggested:
Code:
#picaxe 18m2

  input 2
pir:
  sertxd(#pinC.2)
  pause 500
  goto pir
 
Last edited:

fred_b

Member
Note that this program (essentially functionally equivalent) does work in the simulator, which indicates that something is wrong with your hardware setup.
Code:
#picaxe 18m2

  input 2
pir:
  if pin2=1 then 
    sertxd("1")
  else
    sertxd ("0")
  endif
  pause 500
  goto pir
Or as was suggested:
Code:
#picaxe 18m2

  input 2
pir:
  sertxd(#pinC.2)
  pause 500
  goto pir
pir.jpg

It seems that it must be wired properly since it was reading a high value of 252 on activation (when I was using the readadc command) and read 0 when not activated. The schematic is attached.
 
Last edited:

fred_b

Member
Ok. I made a new short program to test the operation:

pir:
sertxd (#pinC.2)
pause 100
goto pir

When I run this, it works perfectly. It displays 0 until the PIR is activated and then displays 1 for a second or so.

Now I changed the program to:

readadc2,w0
input 2
pir:
sertxd (#pinC.2)
pause 100
goto pir

When I run this. It no longer displays '1' on PIR activation.

Apparently, the input command does not change the analogue input to a digital input.

So how would you do that?
 
Top