This step will deal with the new, as yet untested, code:



At the bottom of the code you will find individual methods for each switch or light (e.g. kitchen, bedside, etc.). When you add a new item or if you just want to change any that are already there this is the section you will need to edit first. The structure of an individual on/off method is:



void switchName (boolean on, boolean multioff){



The two Booleans are whether the switch is to be turned on (true) or off (false) and the second is whether it should turn off any timed run-times (to make sure that things don’t randomly turn on/off if you finish requiring a run-time before it expects you to be finished).



The if statement within the method determines whether the switch is turned on or off using a line of code that looks similar to this:



setOnOff(x, x, on);



this calls a method named “setOnOff”. The code for this can be seen towards the bottom of the sketch. This takes care of turning on and off the devices and for feedback through the serial port. The layout of the code is:



setOnOff(int group, int plug, boolean on)



The boolean “on” in each device’s method already says whether you want it to turn on or off so you only need to change the group and plug values for each device. For instance if I wanted to turn on the plug 4 in group 3 then I’d write:



setOnOff(3, 4, on);





Scrolling down roughly half way, there is a section headed

“/////////////// The following section is for turning on and off individual devices ///////////////”



This is the next section that will need to be dealt with.



There follows 12 lines of very similar looking code for the different appliances/lights.



The first line of each bit follows the same structure:

else if (packBuff[0] = 'P' && packBuff[1] == 'x' && packBuff[2] == 'x')



This is the section in which you specify which selector from your phone you are refereeing to. For instance, if you wanted to specify what should be done when you press a button that is setup to send the message P04 you would change the above code to:



else if (packBuff[0] = 'P' && packBuff[1] == '0' && packBuff[2] == ‘4’)



Because all toggles have two messages that can be sent (sticking with the P04 example it can either be P04H or P04L depending on the state the toggle has been changed to) there needs to be some logic to process the two separately. As they both start with P04 bit makes sense to have a second nestled if statement.



if (packBuff[3] == 'H'){



This tells the code what to do if it is turned on. The statement two lines lower:



else {



Tells the code what to do if the last letter is not H (i.e. L) which means you can specify what you want it to do when it’s turned off.



If you are using a button rather than a toggle you can get rid of the nestled if function as there won’t be an additional H or L in the message.



The line of code just under the if “(packBuff[3] == 'H'){“ is the action to take when it’s on. For instance if you want the desk lamp to turn on you call the “desk” method and tell it that you want it to be on and that you want it to turn off any run-times that are currently going:



desk(true, true);



In the “else” statement you will want to turn off the same switch and again for it to cancel any run-times that are currently in action:



desk(false, true);



If you would rather this particular button didn’t turn off any run-times then the second variable needs to be set to false (i.e. desk(true, false); and desk(false, false);)



The next stage is buttons that, when pressed, will affect more than one plug socket (e.g. entertainment turning on or off the TV, Xbox and sound system, or All lights on/off). For these you quite simply have more than one individual device method called (see examples of this already in the code).



The slightly more involved section is the “run-time” section. Setting up timing automations requires a few more changes.



The first thing to do is to declare two variables before void(setup). One int that will act as a boolean and one unsigned long which will store the current time.



When the button is pressed you will want it to set on or off anything that you want done at the start. You will then need to set the unsigned long to the current time (in seconds since January 1st 1970) and then the int to 1 (e.g. teethStart = now.unixtime(); and teethActive = 1;).



When the int == 1 the run-time will begin. Before the section where you specify what happens when each button is pressed the run-time must be specified. Start with an if statement that checks to see if the int == 1. If so, then start the run-time (e.g. if (teethActive == 1){ ).



The next section will be the first with timing elements. By setting up if statements involving time queries you can set things to turn on and off at a given number of seconds after the button was initially pressed.



For instance, if you want something to turn on/off after three minutes then the syntax is:



if (teethStart+120 == now.unixtime()){



This is basically checking if the current time is the same as the time the button was pressed add 120 seconds.



At the end of the statement in a run-time the activation int should be set to 0 so the runtime is no longer checked through (making the whole system more efficient).



The active int variable (e.g. teethActive) must be added to the multiOff statement.



There is another section, which makes sure that, no more than one run-time can be running at any one time. It keeps the latest one to be turned on running.



In this section all the activation ints are summed and if the answer is greater than 1 then more than one is active at the same time. If this is the case then the times at which the run-times were started are compared and the runtime with the greatest number is kept active. You will need to add any additional run times to this section but looking at the code that’s already there it should be come fairly self explanatory.