## 4 Mobility Aware Duty Cycling Algorithm (MADCAL)
### Duty Cycling Adjusted with MADCAL  
Algorithm 2 is designed to be inserted within the existing MAC code in order to establish the node wake-up time. This is based on calculating the current sink position by utilising the sink start position, the size of the circle circumference, and the current simulation time. This enables the static nodes to calculate the sink position without the need for beacons or other energy consuming methods such as a global positioning system (GPS) [40]. The sink position is then compared to the coordinates of the start of the threshold calculated in algorithm 1, with node wake-up time determined by how long it will take the sink to reach the threshold, or else reverting to the original code using the check interval as input, in the event the sink is already within the threshold.  
Figure 5: Adjusted threshold calculation.  
* Lines 1, 2--the Sleep procedure begins. The default sleep interval is set as checkInterval.
* Lines 3-9--applies to significant nodes only. The thresholdTime function is called to establish the time it will take for the sink to reach the threshold. If the threshold has been reached, the interval to wake-up reverts to the checkInterval; otherwise, it is set to the time it will take for the sink to reach the threshold.
* Lines 10-12--if this is not a significant node the wake-up schedule reverts to the checkInterval.
* Lines 13, 14--the wake-up is calculated by adding the interval to the current simulation time, the Sleep procedure ends.
* Line 15--the start of the thresholdTime function to establish how long it will take the sink to reach the threshold.
* Lines 16, 17--the current sink position is calculated as sinkPos based on simulation time and the initial sink start position, with the withinThreshold function called to establish if the sink is already within the communication threshold or not.  
* Lines 18-21--if the threshold has not been reached yet, set the arc to the distance the sink must travel to reach the sinkThresholdBefore coordinates. Establish the current quartile in which the sink resides and calculate the time left to reach the threshold as the size of the arc in metres divided by the speed of the MSN in mps.
* Lines 22-24--the threshold has been reached, therefore the time left to reach the threshold is set as zero.
* Lines 25, 26--the timeToThreshold is returned and the thresholdTime function ends.
* Line 27--the start of the withinThreshold function to establish the position of the sink node in relation to the static node communication threshold.
* Lines 28, 29--establish if the distance between the current sink position and the thresholdAfter coordinates is greater than the size of the entire threshold. If so then the threshold has not been reached and thresholdReached is set to false.
* Lines 30, 31--however, if this is not true and the distance between the current sink position and the thresholdAfter coordinates is less than the size of the entire threshold. Now establish if the sink is before or after the threshold. If the distance between the sink position and the thresholdBefore coordinates is greater than the thresholdDistance then the sink must be beyond the threshold and therefore thresholdReached is set to false.
* Lines 32-34--if the sink is also within thresholdDistance of thresholdBefore however, this means the sink is within the threshold. Therefore thresholdReached is set to true.
* Lines 35, 36--thresholdReached is returned and the withinThreshold function ends.