# Shenzhen I/O – Passive Infrared Sensor

I think we are at problem number 7 in Shezhen I/O where we are asked to help a security company design a passive infrared sensor that will trigger an alarm between a given time interval, if there is too much activity. We are given a problem where were have the a simple sensor input and a simple alarm output. We also have a simple input giving us time. Further more we have 2 xbus inputs giving us the on and off times. The problem description is simple enough, if the timer is between the on and off time (loop) and the sensor reading is 20 or higher we should trigger the alarm. Just like the signals here tells us

## Structure of the passive infrared sensor

I already used the trick of a DX300 last time to convert between an xbus signal and a simple output that only flips on and off. So if we use that again we have 2 simple pins and 3 xbus pins. Which means that a MC6000 can cover all of it, if there is enough room for the lines of codes needed.  But let us start coding based on the following structure of the design.

## Coding the thing

Since the timing if cyclical I haven’t found a way to just check if the time if between the two, since it varies in the different test cases. On the other hand the clock never skips a beat. So we can just flip a switch whenever the time equals the on or off time. I have used the acc register for it, and used the following code

```# Check turn on
teq p1 x0
+ mov 100 acc
# Check turn off
teq p1 x1
+ mov 0 acc
```

That means we know that if the acc is 100 then the sensor is active. Actually checking whether the alarm should be triggered is fairly trivial and uses the code.

```  tgt acc 0
+ tgt p0 19
+ mov 10 x2
- mov 0 x2

slp 1
```

We start by checking if we are activated. If that is the case we check if the signal is larger then 19, since we need to trigger if the signal is 20 or higher. If that is the case. We will move 10 to x2 as the connection gives us a 100 on the output side for that, and then sleep. If we shouldn’t trigger we will move 0 to x2.

In total that gives us 9 lines of code plus a few comments. Easily fittable withing the available 14 lines of code that we can put into the MC6000 unit.

All in all that means the solution will look like

This gives us a solution in

Cost: 6
Energy: 358
Lines of code: 9

I know that some managed to find a solution in fewer lines of code, but I cannot figure out how to do that.

### Posted by Kristian

8 lines:

teq p1 x3
+ not

teq p1 x2
+ not

tlt p0 20
+ mov 0 x0
– mov acc x0

slp 1

MÉHÉSZ GYÖRGY ÁKOS

Cost: 6
Energy: 301
Lines of code: 7

Same layout except the alarm goes to the p2 port of the DX300.

teq p1 x0
– teq p1 x1
+ not
tlt p0 20
+ mov 0 x2
– mov acc x2
slp 1

Or another optimization on the energy front. (this one is exactly the same layout as in the original post)

Cost: 6
Energy: 230
Lines of code: 12

@ sub p1
@ tlt acc 0
@ slp acc
tgt p0 19
+ mov 10 x2
– mov 0 x2
slp 1
teq x1 p1
+ mov 0 x2
+ slp 999

Yawin

MÉHÉSZ, your las code is wrong. It don’t works.

This code has the same idea and works:
mov x0 acc
sub p1
tlt acc 0
slp acc

c:tgt p0 19
+ mov 10 x2
– mov 0 x2
slp 1

teq x1 p1
+ mov 0 x2
– jmp c

Cost: 6
Energy: 288
Lines of code: 12

Ari Krohn Berle

Not exactly the best, but my solution uses 352 power, has 13 lines of code and costs ¥6.

start: teq p1 x0
+ jmp armed
– slp 1
jmp start
armed:teq x1 p1
– jmp alarm
+ mov 0 x2
jmp start
alarm:tlt p0 20
– mov 100 x2
+ mov 0 x2
slp 1
jmp armed

This is a really helpful blog. You’ve provided supplemental info that the manual doesn’t cover and it would be difficult to solve some of the concept projects without your help.

Inefficiency is my name

I have a MC6000 which has
p0 connected to the sensor
p1 connected to the timer
x0 connected to the OFF time
x1 connected to the ON time
x2 connected to a MC4000
with this code:
MC4000
tgt p0 19
– jmp loop
tcp x1 x0
+ jmp A
– jmp B
A: tlt p1 x1
+ tcp p1 x0
– mov 100 x2
jmp loop
B: tlt p1 x0
+ tcp p1 x1
– jmp loop
mov 100 x2
loop: slp 1

I thought i needed to deal with the 2 different situations based upon if the ON time was greater than the OFF or the opposite.

The MC4000 goes to the alarm with this code
mov 0 p1
slx x0
mov x0 p1
slp 1

I know it’s pretty bad… but is the only solution i was able to find on my own, and not with ease.

https://ibb.co/nMcw9yY