Hello!
On Habré appear and appear articles on the implementation of the Smart Home. The most important problem (or just for me) is turning on / off the light in the bathroom. It seems that the thing is not tricky - but how many options are there. After reading the articles, including
here and
here , I thought, "But it could have been easier."
This worm sharpened me about six months. And when it became freer with work, I matured.
I say that I like to do programming and radio design from school. Microcontrollers gave real joy - all at once. And Arduino is not here, not because
I hate him for this task is redundant, or because I want to be not like everyone else, I just have not gotten to him (or he to me).
Let us return to our sheep (well, either to our light, or to our toilet). For me personally, drawing in the head TZ (yes, draw, this is when you can’t even articulate, not what you write on paper) is much more difficult than it is to realize. After weeks of thinking, this is what I got about:
- the light should turn on when I open the door (I go for example);
- the light should turn on when I close the door (went into the bathroom with the door open and closed behind me);
- the light should turn on when I enter without touching the door (I looked into the hands to wash);
- auto light off after a certain time;
- the light should not turn off when I'm inside and do not even move.
It seems that everything is logical and simple, but I have not found a beautiful solution in any of the articles I have met. The simplest is the motion sensor. It turns on the light when someone is there and turns it off after a while. For my purposes, he lacks only a reed switch a couple - to follow, the door is open or closed.
I do not understand why, until now, manufacturers have not reached this point. Or reached, but did not reach me?
The algorithm is simple:
- if the motion sensor has triggered - turn on the light;
- if the state of the reed switch has changed (door opened / closed) - turn on the light;
- if the motion sensor has triggered when the door is closed (the reed switch is closed) - do not turn off the light until the door is opened;
- Well, turn off the light after a while.
Now TZ is clear, I need:
- Motion Sensor;
- reed switch;
- MK to control this mess.
The cheapest DD (infrared), some kind of reed switch was bought, ATTiny2313.

We disassemble the motion sensor, we see inside:

control board with an infrared receiver and a mirror in the middle and:

BP and relay. I was lucky, in DD there is everything you need: a relay, a transistor for matching, the rest of the harness (even the diode). When the sensor is triggered, a TTL signal is issued, it is enough to intercept it, and the signal from my MC is transmitted instead.
In ISIS drew a diagram (if done, then beautifully)
in BASCOM-AVR wrote a program:
Code$ regfile = "attiny2313.dat"
$ crystal = 4000000
$ hwstack = 40
$ swstack = 16
$ framesize = 32
Config Porta = Output
Config Portb = Output
Config Portd = Output
Config Portd.2 = Input
Config Portd.3 = Input
Config Int0 = Rising
Config Int1 = Change
Enable Interrupts
Enable Int0
Enable Int1
Config Debounce = 300
On Int0 Dd
On Int1 Gerkon
Dim Timecount As Integer
Dim Timelock As Bit
Timecount = 0
Timelock = 0
Portb.0 = 0
Portb.1 = 0
')
Do
If Timecount <200 Then
Portb.0 = 1
Else
Portb.0 = 0
End if
If Timelock = 0 Then
Timecount = Timecount + 1
End if
If Timecount> 250 Then
Timecount = 250
End if
Waitms 100
Loop
Dd:
Disable interrupts
Timecount = 0
If Pind.3 = 1 Then
Timelock = 1
End if
Enable Interrupts
Return
Gerkon:
Disable interrupts
Timecount = 0
If Pind.4 = 0 Then
Timelock = 0
End if
Enable Interrupts
Return
Made emulation, it seems like everything works (after debugging, of course). I collected the layout and checked it (it’s not so difficult to assemble such layouts, the main thing is to start):

We cut the roads in DD and connect them according to the
inflamed imagination of the concept:

Checked - earned. Automatic shutdown after about 1 min 20 sec (not for some reason, it just happened right away, but I was satisfied), the rest of the work is according to a preconceived logic.
Here I will make a retreat. The fact is that I solder from the time when the transistors MP39 and MP42 were in use. Soldered and written was a lot. When the scheme I developed (and especially the program) starts working the first time - I feel discomfort, so rarely does this happen to me. A couple of hours were killed for testing, I did not find any bugs, I continued to work.
Collected in the working version (LUT was not useful):

With the help of scotch tape and someone's mother, all this insulated and secured in the case. As a result, the received copy does not appear to be different from the original one; even the wiring diagram has not changed (unless a pair of wires was added for the reed switch):

The main thing - after each step to check the performance, floated - we know.
Installation and other commonplace miss.
The wife took without enthusiasm and called "garbage" (nonsense, still appreciate - and where to go).
Budget:
- DD - 250 p. (I did not find cheaper)
- reed switch - 38 p.,
- ATTiny2313 - 140 p. (horse price, but you wanted yesterday).
For constructive criticism thanks in advance.