Code analysis

Let us analyse below the meaning of the lines of code applied to the smart-streetlight software:

code-11

At the beginning of the program, we define some variables (sensorPin, HistoryValue and printBuffer) to store the sensor port number, a historical value (for further comparison) and a string to send the lectures to the serial port.

code-12

Line 10 define the Arduino pin used to control the relay.

code-13

These lines run once:

  • Line 14: open serial communication at a 9600 baud rate.

  • Line 15: initialize relay pin (A5) as an output pin.

  • Line 16: turn the pump off (to be sure the pump is off when Arduino starts).

Let’s now see the void loop() group of instructions:

code-14

Line 21 reads the value from the sensor (A0) and store it in the humidity variable.

code-15

Here is where we compare the HistoryValue. These lines run only if lecture variation is greater than ten (making a comparison against the last lecture). The objective is to track significant variations only. Line 25 builds and stores a chain to printBuffer variable, and then (line 26) send the chain to the serial port for monitoring (you can see the values opening the Monitor tab on the left of the Arduino Web Editor). Finally, HistoryValue sets to the last value.

code-16

Lines 29 to 36 establish the relationship between the sensor (the soil moisture sensor) and the actuator (the water pump). Once the sensor is fixed in the final position (in the soil) and connected to the Arduino Uno controller, we should monitor the lower and higher moisture level. We need to edit lines 31 and 35 and put the correct values where the pump should turn on or off.