Algemene beschrijving van domotica en de door mij gekozen technieken

Old school phone as doorbell and domoitcs interface!

Door SuperKris op woensdag 14 november 2018 21:22 - Reacties (17)
Categorieën: Domotica Algemeen, Elektronica algemeen, Mijn domotica oplossingen, Views: 6.065

https://tweakers.net/ext/f/Uz13amv1hOXMmVNxvM5VElhk/full.jpg

Introduction
Many of you may like the cool looks of an old dial phone. I know I do, but in 2018 it’s hard to find any real purpose for them. Suddenly it hit me! Why not turn it into a doorbell? I guessed it would be a 10 minute job. Just supply some voltage to the coil and let it ring! Man I was wrong…


After a 2 week project I learned a lot and now have an awesome doorbell and domotics interface.

The current version features the following functions:
  • Doorbell function with simple button
  • MySensors integration with NRF24 radio
  • Wirelessly activate 5 different ringtones
  • Alarm signal
  • Working dial with 10 virtual switches
The idea is basically that it will ring just like a old phone when somebody presses the doorbell button. If you don’t want any wires for that, you can just sent a command from any button attached to your domotics controller. You can also set your domotics controller to ring different ringtones for any events like a door that has opened, or a set timer that has passed.

The dail also acts like 10 virtual switches. Your domitcs controller will see these as 10 different switches that will be turned on and immediately be turned off again. You can use this to trigger events like turning a light on, or set the heating to a different setpoint.

The wireless function is done by the incredible MySensors library. In my opinion its one of the best platforms for home build sensors and actors. Its cheap to build, very reliable, and the possibilities are endless. You will need a MySensors gateway attached to your domitics controller. I’m using the USB version. Building one is very easy and doesn’t require knowledge of MySensors, Arduino, or electronics. If you just want the doorbell, don’t worry about all the other functions. Just leave out the radio and the connections to the dail. The Arduino code will work fine without.

As controller I’m using Domoticz running on a Raspberry Pie 3 with a 433mhz RFlink gateway (also a great project!), the MySensors gateway, and a Xiaomi gateway. This allows me to control all these devices from this old phone!

PCB

The electronics:
The old phone bell isn’t very easy to power. It was meant to be powered by a 90Vtt 25HZ sine wave. This is why you cant just power it with a 12V DC source. However, a H-bridge will allow you to power it with DC. I’m using a MT3608 step up converter PCB that can be bought on Aliexpress for less than € 1,-. It can convert a low voltage DC source into 28VDC which is enough to power the coil of the bell. The problem is that we need to be able to reverse the polarity so the hammer swing the other way. A H-bridge can be a solution for this. If your interested in knowing how this H-bridge works, you can read this post: Electronics & Home Automation: H-bridge for controlling phone bell

You will need a Arduino. All Arduino’s will do, but I highy recommend using a Nano. The Nano V3 that RobotDyn makes is sold on AliExpress for approx. 2 or 3,- I recommend this type because it has better voltage regulators than most brands. We need a good 3V3 output for the radio module, or else we may need an extra regulator. RobotDyn also uses the more common micro USB port instead of mini USB, and overall it has more and better components.

The connection of the radio is not handled in this blogpost. You can find all the info you need for this on the MySensors website. Do not forget to use a extra capacitor as instructed on the MySensors page. For the radio module you can just get any type of AliExpress. The also cost less than € 1,-.
You will need to do some basic soldering for getting the components on a PCB. Prototype board is fine for this application. I used the following components:

P-FET’s: 2x IRF9540
N-FET’s: 2x IRF3205
NPN transistor: BC547

For the FET’s you need a Ugs-on of -4,5/4,5 minimum. They should be able to handle a Ugs-max of 10V. The rated current is not really an issue, but I like them to be a bit overpowered so voltage spikes from the coil cannot destroy them.

For the resistors any type will do. None of them will run more than a few mA so ¼ watt will be fine. Values are not very important either. Double or half value will not make any difference. Just be sure the resistor between the 28V and the FET’s gate is ½ of the value of the one below it, and it will work fine.

https://tweakers.net/ext/f/ViAO48PUdtp0axaUdcMsbmP1/full.png

The program:
If you have never used a Arduino before it simple. Install the Arduino software, install the MysSensors library and the bounde2 library, copy the code-paste the code below, select the right board, and upload.

Feel free the make any modifications to the code. Its pretty simple code and every line is explained. It’s very easy to edit the ringtones. Just change the values as described in the code.


code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/**
 * The MySensors Arduino library handles the wireless radio link and protocol
 * between your home built sensors/actuators and HA controller of choice.
 * The sensors forms a self healing radio network with optional repeaters. Each
 * repeater and gateway builds a routing tables in EEPROM which keeps track of the
 * network topology allowing messages to be routed to nodes.
 *
 * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
 * Copyright (C) 2013-2015 Sensnology AB
 * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
 *
 * Documentation: http://www.mysensors.org
 * Support Forum: http://forum.mysensors.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 * 
 * 
 * **************************************************************************************************
 * This project convertes a old phone into a doorbell and domotics interface.                       *
 * All the info and schematics can be found on the following link:                                  *
 *                                                                                                  *
 * https://superkris.tweakblogs.net/blog/16935/old-school-phone-as-doorbell-and-domoitcs-interface! *
 *                                                                                                  *
 * **************************************************************************************************
 */

// ##### mysensors setup ###### (Edit child ID numbers if you want)
#define ProjectName "OldPhone" // Name that is vissible in controller
#define ProjectVersion "0,1"   // Version that is vissible in controller
#define MY_DEBUG               // Enable debug prints to serial monitor
#define MY_RADIO_NRF24         // Enable and select radio type attached
#define childIdDoor 1          // child ID number used by mysensors foor doorbell button
#define childIdRing_1 11       // child ID number used by mysensors to activate ringtone 1
#define childIdRing_2 12       // child ID number used by mysensors to activate ringtone 2
#define childIdRing_3 13       // child ID number used by mysensors to activate ringtone 3
#define childIdRing_4 14       // child ID number used by mysensors to activate ringtone 4
#define childIdRing_5 15       // child ID number used by mysensors to activate ringtone 5
#define childIdRingAlarm 16    // child ID number used by mysensors to activate alarm
#define childIdDail_1 101      // child ID number used by mysensors when the number 1 is dailed
#define childIdDail_2 102      // child ID number used by mysensors when the number 2 is dailed
#define childIdDail_3 103      // child ID number used by mysensors when the number 3 is dailed
#define childIdDail_4 104      // child ID number used by mysensors when the number 4 is dailed
#define childIdDail_5 105      // child ID number used by mysensors when the number 5 is dailed
#define childIdDail_6 106      // child ID number used by mysensors when the number 6 is dailed
#define childIdDail_7 107      // child ID number used by mysensors when the number 7 is dailed
#define childIdDail_8 108      // child ID number used by mysensors when the number 8 is dailed
#define childIdDail_9 109      // child ID number used by mysensors when the number 9 is dailed
#define childIdDail_10 110     // child ID number used by mysensors when the number 0 is dailed

// ###### Include libraries #####
#include <SPI.h>               // needed for NRF24
#include <MySensors.h>         // mysensors library
#include <Bounce2.h>           // for debouncing buttons and dail

// ###### I/O pin number setup ###### (edit pin numbers if you connected the electronics to diffrent I/O pins)
#define button 3               // input pin activated when doorbell button is pressed
#define edail 8                // input pin activated when dail is used (Enable Dail)
#define pdail 7                // input pin activated with each tick of the dail (Pulse Dial)
#define lbell 4                // output pin to swing bellh hamer left
#define rbell 5                // output Pin to swing bellh hamer right

// ##### Debouncer variables ##### (no not edit)
Bounce debouncerButton = Bounce(); // Create button debouncer for doorbell button 
Bounce debouncerEdail = Bounce();  // Create button debouncer for Enable Dail
Bounce debouncerPdail = Bounce();  // Create button debouncer for Pulse Dial 
bool valueButton = 0;              // Debounced I/O bit from doorbell button pin
bool oldValueButton = 0;           // Old value to compare to current value 
bool valueEdail = 0;               // Debounced I/O bit from enable dail pin
bool oldValueEdail = 0;            // Old value to compare to current value
bool valuePdail = 0;               // Debounced I/O bit from pusle dail pin
bool oldValuePdail = 0;            // Old value to compare to current value


// ##### Variables for dailing disk decoding ##### (do not edit)
int dailCount = 0;                 // dail counter (while running)
int newDailCount = 0;              // dail counter set when dailing is completed

// ##### Variables for ringtone generator ##### (do not edit)
bool ringPhone = 0;                // bit that enables the rining of the phone
bool coilPowered = 0;              // safety bit that checks if the coil is already powered
int RingCyclePhase = 0;            // Keeps track of current ring cycle phase (1-5)
int ring = 0;                      // counter for amount of ring cycles passed
int repeat = 0;                    // counter for amount of repeats passed
unsigned long currentMillis = 0;   // used to keep track of current time
unsigned long onPulseMillis = 0;   // timer start for on pulse used for ring bell coil
unsigned long offPulseMillis = 0;  // timer start for off pulse used for ring bell coil
unsigned long ringPauseMillis = 0; // timer start for pause between 2 ringing sessions

// ##### Variables for ringtone 1 ##### (Edit the values as you like)
int onPulse_1 = 20;                // time the coil is powered to swing the hammer in ms. Typical value 5-50ms. No point trying other values
int offPulse_1 = 10;               // time the coil is switched off in ms (SETTING THIS TIME TO LOW MIGHT DAMAGE THE FETS) typical vaule 5-50ms
int ringX_1 = 20;                  // amount of ring cycles (controlls the length of each ring) 
int ringPause_1 = 2500;            // time paused between the each "ring"
int repeatX_1 = 3;                 // amount of "rings"

// ##### Variables for ringtone 2 ##### (Edit the values as you like)
int onPulse_2 = 15;                // time the coil is powered to swing the hammer in ms. Typical value 5-50ms. No point trying other values
int offPulse_2 = 7;                // time the coil is switched off in ms (SETTING THIS TIME TO LOW MIGHT DAMAGE THE FETS) typical vaule 5-50ms
int ringX_2 = 4;                   // amount of ring cycles (controlls the length of each ring) 
int ringPause_2 = 200;             // time paused between the each "ring"
int repeatX_2 = 2;                 // amount of "rings"

// ##### Variables for ringtone 3 ##### (Edit the values as you like)
int onPulse_3 = 15;                // time the coil is powered to swing the hammer in ms. Typical value 5-50ms. No point trying other values
int offPulse_3 = 7;                // time the coil is switched off in ms (SETTING THIS TIME TO LOW MIGHT DAMAGE THE FETS) typical vaule 5-50ms
int ringX_3 = 4;                   // amount of ring cycles (controlls the length of each ring) 
int ringPause_3 = 200;             // time paused between the each "ring"
int repeatX_3 = 4;                 // amount of "rings"

// ##### Variables for ringtone 4 ##### (Edit the values as you like)
int onPulse_4 = 20;                // time the coil is powered to swing the hammer in ms. Typical value 5-50ms. No point trying other values
int offPulse_4 = 10;               // time the coil is switched off in ms (SETTING THIS TIME TO LOW MIGHT DAMAGE THE FETS) typical vaule 5-50ms
int ringX_4 = 40;                  // amount of ring cycles (controlls the length of each ring) 
int ringPause_4 = 0;               // time paused between the each "ring"
int repeatX_4 = 1;                 // amount of "rings"

// ##### Variables for ringtone 5 ##### (Edit the values as you like)
int onPulse_5 = 15;                // time the coil is powered to swing the hammer in ms. Typical value 5-50ms. No point trying other values
int offPulse_5 = 10;               // time the coil is switched off in ms (SETTING THIS TIME TO LOW MIGHT DAMAGE THE FETS) typical vaule 5-50ms
int ringX_5 = 7;                   // amount of ring cycles (controlls the length of each ring) 
int ringPause_5 = 650;             // time paused between the each "ring"
int repeatX_5 = 5;                 // amount of "rings"

// ##### Variables for alarm ##### (Edit the values as you like)
int onPulseAlarm = 20;            // time the coil is powered to swing the hammer in ms. Typical value 5-50ms. No point trying other values
int offPulseAlarm = 10;           // time the coil is switched off in ms (SETTING THIS TIME TO LOW MIGHT DAMAGE THE FETS) typical vaule 5-50ms
int ringXAlarm = 15;              // amount of ring cycles (controlls the length of each ring) 
int ringPauseAlarm = 500;         // time paused between the each "ring"
int repeatXAlarm = 1250;          // amount of "rings" With th other timing setting above 1250 rings is approx a 1/2 hour of alarm.

// ##### Variables for current ringtone ##### (No point in edditing. These values are overwritten by other programmed ringtones)
int onPulse = 0;                   // time the coil is powered to swing the hammer in ms. (set to 0 by default)
int offPulse = 0;                  // time the coil is switched off in ms.(set to 0 by default)
int ringX = 1;                     // amount of ring cycles. (set to 1 by default so code doesnt run)
int ringPause = 0;                 // time paused between the each "ring" (set to 0 by default)
int repeatX = 1;                   // amount of "rings" (set to 1 by default so code doesnt run)


// ##### setup MySensors message containers #####
MyMessage msgDoor(childIdDoor,V_STATUS);        // message container used for doorbell button
MyMessage msgRing_1(childIdRing_1,V_STATUS);    // message cointainer for ringtone 1
MyMessage msgRing_2(childIdRing_2,V_STATUS);    // message cointainer for ringtone 2
MyMessage msgRing_3(childIdRing_3,V_STATUS);    // message cointainer for ringtone 3
MyMessage msgRing_4(childIdRing_4,V_STATUS);    // message cointainer for ringtone 4
MyMessage msgRing_5(childIdRing_5,V_STATUS);    // message cointainer for ringtone 5
MyMessage msgAlarm (childIdRingAlarm,V_STATUS); // message cointainer for ringtone 5
MyMessage msgDail_1(childIdDail_1,V_STATUS);    // message container used when the number 1 is dailed
MyMessage msgDail_2(childIdDail_2,V_STATUS);    // message container used when the number 2 is dailed
MyMessage msgDail_3(childIdDail_3,V_STATUS);    // message container used when the number 3 is dailed
MyMessage msgDail_4(childIdDail_4,V_STATUS);    // message container used when the number 4 is dailed
MyMessage msgDail_5(childIdDail_5,V_STATUS);    // message container used when the number 5 is dailed
MyMessage msgDail_6(childIdDail_6,V_STATUS);    // message container used when the number 6 is dailed
MyMessage msgDail_7(childIdDail_7,V_STATUS);    // message container used when the number 7 is dailed
MyMessage msgDail_8(childIdDail_8,V_STATUS);    // message container used when the number 8 is dailed
MyMessage msgDail_9(childIdDail_9,V_STATUS);    // message container used when the number 9 is dailed
MyMessage msgDail_10(childIdDail_10,V_STATUS);  // message container used when the number 0 is dailed

void setup()  
{  
//##### I/O pin function setup #####
  pinMode(button,INPUT);    //set the already defined I/O pin as input
  pinMode(edail, INPUT);    //set the already defined I/O pin as input
  pinMode(pdail, INPUT);    //set the already defined I/O pin as input
  pinMode(lbell, OUTPUT);   //set the already defined I/O pin as output
  pinMode(rbell, OUTPUT);   //set the already defined I/O pin as output
 
// ##### Debouncer setup #####
  debouncerButton.attach(button);
  debouncerButton.interval(5);
  debouncerEdail.attach(edail);
  debouncerEdail.interval(5);
  debouncerPdail.attach(pdail);
  debouncerPdail.interval(5);
}

// ##### Function of MySensors that presents all attached sensors to the controller #####
void presentation() { 
  sendSketchInfo(ProjectName, ProjectVersion);  // Send the sketch version information to the gateway and Controller
  present(childIdDoor, S_BINARY);               // Present doorbell button as binary switch
  present(childIdRing_1, S_BINARY);             // Present ringtone 1 as a binary switch
  present(childIdRing_2, S_BINARY);             // Present ringtone 2 as a binary switch
  present(childIdRing_3, S_BINARY);             // Present ringtone 3 as a binary switch
  present(childIdRing_4, S_BINARY);             // Present ringtone 4 as a binary switch
  present(childIdRing_5, S_BINARY);             // Present ringtone 5 as a binary switch
  present(childIdRingAlarm, S_BINARY);          // Present alarm as a binary switch
  present(childIdDail_1, S_BINARY);             // Present the dailing of number 1 as binary switch
  present(childIdDail_2, S_BINARY);             // Present the dailing of number 2 as binary switch
  present(childIdDail_3, S_BINARY);             // Present the dailing of number 3 as binary switch
  present(childIdDail_4, S_BINARY);             // Present the dailing of number 4 as binary switch
  present(childIdDail_5, S_BINARY);             // Present the dailing of number 5 as binary switch
  present(childIdDail_6, S_BINARY);             // Present the dailing of number 6 as binary switch
  present(childIdDail_7, S_BINARY);             // Present the dailing of number 7 as binary switch
  present(childIdDail_8, S_BINARY);             // Present the dailing of number 8 as binary switch
  present(childIdDail_9, S_BINARY);             // Present the dailing of number 9 as binary switch
  present(childIdDail_10, S_BINARY);            // Present the dailing of number 0 as binary switch
}

void loop() {
  currentMillis = millis();                           // update timer

// ##### debouncer updater ##### 
  debouncerButton.update();                           // Update debouncer for doorbell button
  valueButton = debouncerButton.read();               // Set current value of doorbell button 
  debouncerEdail.update();                            // Update debouncer for enable dail
  valueEdail = debouncerEdail.read();                 // Set current value of enable dail
  debouncerPdail.update();                            // Update debouncer for pulse dail 
  valuePdail = debouncerPdail.read();                 // Set current value of pulse dail

// ##### Mysensors code to check doorbell button and sent message ##### 
  if (valueButton != oldValueButton) {                // Check if the value of the button has changed 
     send(msgDoor.set(valueButton==HIGH ? 1 : 0));    // Transmit the new value
     if (valueButton == HIGH ){                       // If the buttin was high
        onPulse = onPulse_1;                          // Write varialble of selected ringtone to the variable of the current ringtone
        offPulse = offPulse_1;                        // Write varialble of selected ringtone to the variable of the current ringtone
        ringX = ringX_1;                              // Write varialble of selected ringtone to the variable of the current ringtone
        ringPause = ringPause_1;                      // Write varialble of selected ringtone to the variable of the current ringtone
        repeatX = repeatX_1;                          // Write varialble of selected ringtone to the variable of the current ringtone
        ringPhone = HIGH;                             // Set the phone ring bit to high so the ringing can begin
       }
     oldValueButton = valueButton;                    // Change old value so this doenst loop
  }

// ##### Mysensors code to read dail counter and sent message as one individual button #####
  switch (newDailCount) {         // Check the current vallue of the completed counter
     case 1:                      // if value is equal to 1 
       send(msgDail_1.set(1));    // Transmit ON message for dail switch 1 
       send(msgDail_1.set(0));    // Transmit OFF message for dail switch 1. Some home automation software prefers this.
       break;                     // end of case
     case 2:                      // if value is equal to 2 
       send(msgDail_2.set(1));    // Transmit ON message for dail switch 2
       send(msgDail_2.set(0));    // Transmit OFF message for dail switch 2. Some home automation software prefers this.
       break;                     // end of case
     case 3:                      // if value is equal to 3 
       send(msgDail_3.set(1));    // Transmit ON message for dail switch 3
       send(msgDail_3.set(0));    // Transmit OFF message for dail switch 3. Some home automation software prefers this.
       break;                     // end of case
     case 4:                      // if value is equal to 4 
       send(msgDail_4.set(1));    // Transmit ON message for dail switch 4 
       send(msgDail_4.set(0));    // Transmit OFF message for dail switch 4. Some home automation software prefers this.
       break;                     // end of case
     case 5:                      // if value is equal to 5 
       send(msgDail_5.set(1));    // Transmit ON message for dail switch 5 
       send(msgDail_5.set(0));    // Transmit OFF message for dail switch 5. Some home automation software prefers this.
       break;                     // end of case
     case 6:                      // if value is equal to 6 
       send(msgDail_6.set(1));    // Transmit ON message for dail switch 6 
       send(msgDail_6.set(0));    // Transmit OFF message for dail switch 6. Some home automation software prefers this.
       break;                     // end of case       
     case 7:                      // if value is equal to 7 
       send(msgDail_7.set(1));    // Transmit ON message for dail switch 7 
       send(msgDail_7.set(0));    // Transmit OFF message for dail switch 7. Some home automation software prefers this.
       break;                     // end of case       
     case 8:                      // if value is equal to 8 
       send(msgDail_8.set(1));    // Transmit ON message for dail switch 8 
       send(msgDail_8.set(0));    // Transmit OFF message for dail switch 8. Some home automation software prefers this.
       break;                     // end of case       
     case 9:                      // if value is equal to 9 
       send(msgDail_9.set(1));    // Transmit ON message for dail switch 9 
       send(msgDail_9.set(0));    // Transmit OFF message for dail switch 9. Some home automation software prefers this.
       break;                     // end of case       
     case 10:                     // if value is equal to 10 
       send(msgDail_10.set(1));   // Transmit ON message for dail switch 10 
       send(msgDail_10.set(0));   // Transmit OFF message for dail switch 10. Some home automation software prefers this.
       break;                     // end of case       
  }
  newDailCount = 0;                // Reset the completed counter so this doesnt loop

  
// ###### Code for checking enable dail and sending state trough serial  ######  
  if (valueEdail != oldValueEdail && valueEdail == HIGH) {          // Check if enable dail has changed AND if its currently its currently activated
     Serial.println("dail is activated...");                        // If so sent message 
     oldValueEdail = valueEdail;}                                   // And change old value so this doenst loop
     else if (valueEdail != oldValueEdail && valueEdail == LOW) {   // Check if enable dail has changed AND if its currently its currently deactivated
     Serial.println("dail is deactivated...");                      // If so sent message
     newDailCount = dailCount;                                      // Write the counted pulses to the New Dail Count
     dailCount = 0;                                                 // Reset the dail count for next dail 
     oldValueEdail = valueEdail;                                    // And change old value so this doenst loop
  }

// ###### Code for checking pusle dail and sending state trough serial ######
  if (valuePdail != oldValuePdail && valueEdail == HIGH) {          // Check if dail pulse has changed AND if currently its currently activated
     if (valuePdail == LOW) {                                       // Only take action when the signal goes from high to low to prevent double count
        dailCount++;                                                 // If the conditions are met increase counter by 1
        Serial.print("Tick! Total ammout of pulses: ");              // Serial print a messagge saying a pulse was detected
        Serial.println (dailCount);                                  // Serial print a the current value of the counter
     }
       oldValuePdail = valuePdail;                                  // Change old value so this doenst loop  
  }

// ##### ringtone code #####
// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
// WARNING    "lbell" and "rbell" can NEVER be high at the same time. If this does happen your FET's will short and can go up in smoke!    WARNING
// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
// 
// This code must run within the void loop and can not use any loops of its own. This makes the code below a lot more complex, but alows
// the rest of the code to keep running while the bell is ringing. This way the other functions stay are not paused
  if (ringPhone == HIGH && ring <= ringX && RingCyclePhase != 5) {            // activeate ringing if ringPhone bit is set, the set amount of rings isnt reached, and the ringCyclePhase is not 5 (waiting)
     if (coilPowered == LOW && RingCyclePhase == 0) {                         // only activate coil if the coil is powered down and the ring cycle is complete
        coilPowered = HIGH;                                                   // enable safety bit that can be checked if coil is powered 
        RingCyclePhase = 1;                                                   // set ring cycle phase to 1 (coil powered to move hammer left)
        digitalWrite(lbell, HIGH);                                            // power coil to swing hammer into left poistion
        onPulseMillis = currentMillis;                                        // set start for timer for the on pulse 
        Serial.println ("left bell high");                                    // // *****Debug code***** Enable to see what is happening on serial monitor
     }
     if (currentMillis - onPulseMillis >= onPulse && RingCyclePhase == 1) {   // Check if the set time (onPulse) has passed and the ring cycle is still in phase 1
        digitalWrite(lbell, LOW);                                             // power down coil
        coilPowered = LOW;                                                    // disable safety bit that can be checked if coil is powered 
        RingCyclePhase = 2;                                                   // set ring cycle phase to 2 (coil powered down after moving hammer left)              
        offPulseMillis = currentMillis;                                       // set start for timer for the off pulse
        Serial.println ("left bell low");                                      // // *****Debug code***** Enable to see what is happening on serial monitor
     }
     if (currentMillis - offPulseMillis >= offPulse && RingCyclePhase == 2 && coilPowered == LOW) {  // check if the set time (offPulse) has passed and the ring cycle is still in phase 2
        coilPowered = HIGH;                                                   // enable safety bit that can be checked if coil is powered
        RingCyclePhase = 3;                                                   // set ring cycle phase to 3 (coil powered to move hammer right)
        digitalWrite(rbell, HIGH);                                            // power coil to swing hammer into right poistion
        onPulseMillis = currentMillis;                                        // set start for timer for the on pulse 
        Serial.println ("right bell high");                                   // *****Debug code***** Enable to see what is happening on serial monitor
     }
     if (currentMillis - onPulseMillis >= onPulse && RingCyclePhase == 3) {   // check if the set time (onPulse) has passed and the ring cycle is still in phase 3
        digitalWrite(rbell, LOW);                                             // power down coil
        coilPowered = LOW;                                                    // disable safety bit that can be checked if coil is powered  
        RingCyclePhase = 4;                                                   // set ring cycle phase to 4 (coil powered down after moving hammer left)              
        offPulseMillis = currentMillis;                                       // set start for timer for the off pulse
        Serial.println ("right bell low");                                    // Debug code, enable to see what is happening on serial monitor
     }
     if (currentMillis - offPulseMillis >= offPulse && RingCyclePhase == 4) { // check if the set time (offPulse) has passed and the ring cycle is still in phase 4
        RingCyclePhase = 0;                                                   // set ring cycle phase to 0 (ring cycle completed)           
        ring++;                                                               // add 1 count to the "ring" counter    
        Serial.print("numbers of rings : ");                                  // *****Debug code***** Enable to see what is happening on serial monitor
        Serial.println (ring);                                                // *****Debug code***** Enable to see what is happening on serial monitor
     }         
  }     
  if (ring >= ringX){                                                         // if the amount of "ring" is the same as the amount set in "ringX"....
     ring = 0;                                                                // reset ring counter
     RingCyclePhase = 5;                                                      // set ring cycle phase to 5 (wait to start new ring cycle)       
     Serial.println ("Ring cylce 5 reached (pause)");                         // *****Debug code***** Enable to see what is happening on serial monitor
     ringPauseMillis = currentMillis;                                         // set start for timer for the pause
  }  
  if (currentMillis - ringPauseMillis >= ringPause && RingCyclePhase == 5) {  // if the set pause time has passed and the ring cycle is actualy in its pause phase
     RingCyclePhase = 0;                                                      // set ringCyclePhase back to 0 so the ringing can resume again
     Serial.println ("End off pause, start ringing again");                   // *****Debug code***** Enable to see what is happening on serial monitor
     repeat++;                                                                // add 1 count to the "repeat" counter   
  }   
  if (repeat >= repeatX){                                                     // if the amount of repeat is the same as the amount set in "repeatX"....
     repeat = 0;                                                              // reset the repeat counter
     Serial.println ("All ringing done.");                                    // *****Debug code***** Enable to see what is happening on serial monitor 
     ringPhone = LOW;                                                         // ringtone is done. disable ringphone bit so the ringing stops 
  }      
}


void receive(const MyMessage &message){                                // Mysensors code that checks for messages form the mysensors network
  
//  ##### Mysensors code for activating ringtone 1 #####
   if (message.type == V_STATUS && message.sensor == childIdRing_1 && ringPhone != HIGH) {  // Check if the incomming message is a of same as declared (V_status) and check the child ID 
      onPulse = onPulse_1;                                             // Write varialble of selected ringtone to the variable of the current ringtone
      offPulse = offPulse_1;                                           // Write varialble of selected ringtone to the variable of the current ringtone
      ringX = ringX_1;                                                 // Write varialble of selected ringtone to the variable of the current ringtone
      ringPause = ringPause_1;                                         // Write varialble of selected ringtone to the variable of the current ringtone
      repeatX = repeatX_1;                                             // Write varialble of selected ringtone to the variable of the current ringtone
      ringPhone = HIGH;                                                // Set the phone ring bit to high so the ringing can begin
      send(msgRing_1.set(0));                                          // Transmit OFF message for ringtone. Some home automation software prefers this.
      Serial.println ("Received message to play ringtone 1");          // *****Debug code***** Enable to see what is happening on serial monitor 
   }
   
//  ##### Mysensors code for activating ringtone 2 #####
   if (message.type == V_STATUS && message.sensor == childIdRing_2 && ringPhone != HIGH && message.getBool() == 1) {  // Check if the incomming message is a of same as declared (V_status) and check the child ID 
      onPulse = onPulse_2;                                             // Write varialble of selected ringtone to the variable of the current ringtone
      offPulse = offPulse_2;                                           // Write varialble of selected ringtone to the variable of the current ringtone
      ringX = ringX_2;                                                 // Write varialble of selected ringtone to the variable of the current ringtone
      ringPause = ringPause_2;                                         // Write varialble of selected ringtone to the variable of the current ringtone
      repeatX = repeatX_2;                                             // Write varialble of selected ringtone to the variable of the current ringtone
      ringPhone = HIGH;                                                // Set the phone ring bit to high so the ringing can begin
      send(msgRing_2.set(0));                                          // Transmit OFF message for ringtone. Some home automation software prefers this.
      Serial.println ("Received message to play ringtone 2");          // *****Debug code***** Enable to see what is happening on serial monitor 
   }
   
//  ##### Mysensors code for activating ringtone 3 #####
   if (message.type == V_STATUS && message.sensor == childIdRing_3 && ringPhone != HIGH && message.getBool() == 1) {  // Check if the incomming message is a of same as declared (V_status) and check the child ID 
      onPulse = onPulse_3;                                             // Write varialble of selected ringtone to the variable of the current ringtone
      offPulse = offPulse_3;                                           // Write varialble of selected ringtone to the variable of the current ringtone
      ringX = ringX_3;                                                 // Write varialble of selected ringtone to the variable of the current ringtone
      ringPause = ringPause_3;                                         // Write varialble of selected ringtone to the variable of the current ringtone
      repeatX = repeatX_3;                                             // Write varialble of selected ringtone to the variable of the current ringtone
      ringPhone = HIGH;                                                // Set the phone ring bit to high so the ringing can begin
      send(msgRing_3.set(0));                                          // Transmit OFF message for ringtone. Some home automation software prefers this.
      Serial.println ("Received message to play ringtone 3");          // *****Debug code***** Enable to see what is happening on serial monitor 
   }

//  ##### Mysensors code for activating ringtone 4 #####
   if (message.type == V_STATUS && message.sensor == childIdRing_4 && ringPhone != HIGH && message.getBool() == 1) {  // Check if the incomming message is a of same as declared (V_status) and check the child ID 
      onPulse = onPulse_4;                                             // Write varialble of selected ringtone to the variable of the current ringtone
      offPulse = offPulse_4;                                           // Write varialble of selected ringtone to the variable of the current ringtone
      ringX = ringX_4;                                                 // Write varialble of selected ringtone to the variable of the current ringtone
      ringPause = ringPause_4;                                         // Write varialble of selected ringtone to the variable of the current ringtone
      repeatX = repeatX_4;                                             // Write varialble of selected ringtone to the variable of the current ringtone
      ringPhone = HIGH;                                                // Set the phone ring bit to high so the ringing can begin
      send(msgRing_4.set(0));                                          // Transmit OFF message for ringtone. Some home automation software prefers this.
      Serial.println ("Received message to play ringtone 4");          // *****Debug code***** Enable to see what is happening on serial monitor 
   }

//  ##### Mysensors code for activating ringtone 5 #####
   if (message.type == V_STATUS && message.sensor == childIdRing_5 && ringPhone != HIGH && message.getBool() == 1) {  // Check if the incomming message is a of same as declared (V_status) and check the child ID 
      onPulse = onPulse_5;                                             // Write varialble of selected ringtone to the variable of the current ringtone
      offPulse = offPulse_5;                                           // Write varialble of selected ringtone to the variable of the current ringtone
      ringX = ringX_5;                                                 // Write varialble of selected ringtone to the variable of the current ringtone
      ringPause = ringPause_5;                                         // Write varialble of selected ringtone to the variable of the current ringtone
      repeatX = repeatX_5;                                             // Write varialble of selected ringtone to the variable of the current ringtone
      ringPhone = HIGH;                                                // Set the phone ring bit to high so the ringing can begin
      send(msgRing_5.set(0));                                          // Transmit OFF message for ringtone. Some home automation software prefers this.
      Serial.println ("Received message to play ringtone 5");          // *****Debug code***** Enable to see what is happening on serial monitor 
   }

//  ##### Mysensors code for activating alarm #####
   if (message.type == V_STATUS && message.sensor == childIdRingAlarm && message.getBool() == 1) {  // Check if the incomming message is a of same as declared (V_status) and check the child ID 
      onPulse = onPulseAlarm;                                             // Write varialble of selected ringtone to the variable of the current ringtone
      offPulse = offPulseAlarm;                                           // Write varialble of selected ringtone to the variable of the current ringtone
      ringX = ringXAlarm;                                                 // Write varialble of selected ringtone to the variable of the current ringtone
      ringPause = ringPauseAlarm;                                         // Write varialble of selected ringtone to the variable of the current ringtone
      repeatX = repeatXAlarm;                                             // Write varialble of selected ringtone to the variable of the current ringtone
      ringPhone = HIGH;                                                   // Set the phone ring bit to high so the ringing can begin
      Serial.println ("enable alarm signal;");                            // *****Debug code***** Enable to see what is happening on serial monitor 
   }
   if (message.type == V_STATUS && message.sensor == childIdRingAlarm && message.getBool() == 0) {  // Check if the incomming message is a of same as declared (V_status) and check the child ID 
      repeat = repeatXAlarm;
      send(msgRing_1.set(0));                                          // Transmit OFF message for ringtone. Some home automation software prefers this.
      Serial.println ("alarm signal disabled;");                            // *****Debug code***** Enable to see what is happening on serial monitor 
   }

}