Now that all the connections are made, we can test the project. To get started, we will take care of the motion sensor. For this, we will write a very simple sketch that will only make use of the embedded Atmel microcontroller on the Yún board. We first need to declare the pin that the sensor is connected to, as follows:
const int sensor_pin = 8;
Then, in the setup()
function, we will start the Serial connection, as follows:
Serial.begin(9600); delay(1000);
We can also set some delay before data is read from the sensor, as it needs some time to initialize and work correctly. In the loop()
function, we continuously read the value from pin number 8. Remember that the sensor will simply return a logical high state if some motion is detected and a low state otherwise. This means that we can store the sensor's reading into a Boolean variable, as shown in the following line of code:
boolean sensor_value = digitalRead(sensor_pin);