Teacher's Guide for mOway Smart City

Page 1

mOway Smart City Teacher’s guide


Teacher’s guide

Index

Index

Introduction .......................................................................................................................... 4 Smart city .............................................................................................................................. 4 Exercise 1: Programming introduction .............................................................................. 6 Arduino environment .......................................................................................................... 7 Program 1.1 ........................................................................................................................ 8 Applications ........................................................................................................................ 9 Exercise 2: Variables and libraries ................................................................................... 10 Elements ........................................................................................................................... 10 Program 2.1: Variables ..................................................................................................... 10 Libraries ............................................................................................................................ 13 Applications ...................................................................................................................... 15 Exercise 3: Sensors reading ............................................................................................. 16 Elements ........................................................................................................................... 16 Sensor characteristics ...................................................................................................... 16 Program 3.1 ...................................................................................................................... 18 Applications ...................................................................................................................... 20 Exercise 4: Barrier control ................................................................................................ 21 Elements ........................................................................................................................... 21 Barrier characteristics ....................................................................................................... 21 Program 4.1 ...................................................................................................................... 22 Applications ...................................................................................................................... 24 Exercise 5: Streetlights control ........................................................................................ 25 Introduction ....................................................................................................................... 25 Elements ........................................................................................................................... 25 Streetlights characteristics ................................................................................................ 25 Program 5.1 ...................................................................................................................... 27 Program 5.2 ...................................................................................................................... 28 Program 5.3 ...................................................................................................................... 29 Program 5.4 ...................................................................................................................... 30 Program 5.5 ...................................................................................................................... 31 Applications ...................................................................................................................... 32 Exercise 6: Keyboard control ........................................................................................... 33 Elements ........................................................................................................................... 33 Serial communication ....................................................................................................... 34 Program 6.1 ...................................................................................................................... 35

www.moway-robot.com

2


Teacher’s guide

Index

Applications ...................................................................................................................... 43 Exercise 7: Night lighting .................................................................................................. 44 Elements ........................................................................................................................... 44 Strategy ............................................................................................................................ 44 Program 7.1 ...................................................................................................................... 45 Applications ...................................................................................................................... 46 Exercise 8: Traffic lighting ................................................................................................ 47 Elements ........................................................................................................................... 47 Strategy ............................................................................................................................ 48 Program 8.1 ...................................................................................................................... 48 Program 8.2 ...................................................................................................................... 51 Exercise 9: Crossing control ............................................................................................ 52 Elements ........................................................................................................................... 52 Strategy ............................................................................................................................ 52 Program 9.1 ...................................................................................................................... 54 Applications ...................................................................................................................... 56 Exercise 10: Smart city ...................................................................................................... 57 Elements ........................................................................................................................... 57

www.moway-robot.com

3


Teacher’s guide

Introduction

Introduction The purpose of this book is to provide with a guide for learning and teaching mOway Smart City programming. Firstly simple exercises are explained, in order to learn the basic concepts of programming. Then more complete exercises are developed, so that it is possible to see all the characteristics of mOway Smart City. The exercises of this book show applications that could be done by a smart city. The strategy to achieve the goal is explained and the necessary code is included. Flowchart diagrams are also included to make comprehension easier.

Smart city A smart city is more efficient, consumes less energy and is able to manage itself thanks to information and communication technology. Some examples of smart city applications are:  Control the traffic lights depending on the quantity of traffic, for a more efficient circulation.  Increase the lighting of the street if there is traffic and decrease it there is no traffic, for energy saving.  Report the city state (temperature, air quality, etc.) by means of a web server which can be accessed from a PC, phone, tablet, etc.

mOway Smart City is an educational resource which, with mOway robot, makes it easier to understand the concept of “smart city”. It is a stimulating way of learning basics programming and electronics, because the results can be seen immediately. As real applications are shown (lights and barrier automatic activation, cars safety systems, etc.) the comprehension of these concepts is easier. mOway Smart City has elements that allows to develop applications that could be use in a real smart city. For example:  A black line circuit that the mOway robot can follow autonomously.  Streetlights which turn on when it is dark

www.moway-robot.com

4


Teacher’s guide

Introduction

 A barrier that controls the traffic through a crossing.  Sensors to get information of the state of the city.  Etc.

The center of mOway Smart City is a controller board based on Arduino® architecture. This board can be programmed by the user by means of Arduino environment. mOway Smart City installer includes all the necessary libraries for controlling all the devices of the smart city (barrier, streetlights and sensors). In addition, the controller board of mOway Smart City is compatible with the modules of mOway robot (RF module, WiFi module and Camera module). These devices expand the applications of the smart city. It is recommended to read the mOway Smart City manual, included in the installation folder. In is also recommended to check the official web page of Arduino: http://arduino.cc/en.

www.moway-robot.com

5


Teacher’s guide

Programming introduction

Exercise 1: Programming introduction In this exercise the programming of mOway Smart City is introduced, using the Arduino environment. mOway Smart City is an environment that makes possible to easily understand concepts about how a smart city works. It has devices (streetlights, barrier, etc.) autonomously activated by means of sensors. These elements are controlled by the controller board of mOway Smart City, which receives signals from the sensors and, depending of these signals, activates the elements of the smart city. The controller board works by means of a program. This is the element used in this exercise:  Controller board

A program is a sequence of instructions that determine the actions of a system, in this case the smart city. For example, a program to make the streetlights blink could consists of the following actions:

START

Turn on streetlights

Wait for 1 second

Turn off streetlight

Wait for 1 second

www.moway-robot.com

6


Teacher’s guide

Programming introduction

In order to develop a program it is necessary to define how the elements of the smart city should work, for example: turn on the streetlights when it is dark, move the barrier when mOway robot approaches, etc. Once this is defined the program can be written to perform these actions. When the program is finished it is programmed in the controller board and it is tested. If it does not work as expected, the program should be corrected and the controller boards reprogrammed. The controller board of mOway Smart City is programmed by means of Arduino environment. Now some characteristics of this environment are explained.

Arduino environment The controller board of mOway Smart City is based on Arduino architecture, so that it is compatible with the programming environment provided by Arduino. This environment has a text editor and resources for compile the program and program the controller board. It can be downloaded from this link: http://arduino.cc/en/Main/Software. The programming language for mOway Smart City is similar to C/C++. In this manual example of code is explained for learning the basic characteristics of it. A reference of this language can be found in the Arduino official web page: http://arduino.cc/en/Reference/HomePage. For further information about Arduino environment see the next link: http://arduino.cc/en/Guide/Environment.

www.moway-robot.com

7


Teacher’s guide

Programming introduction

Program 1.1 In order to understand how to use the Arduino environment, let’s start with an easy example for showing a message on the computer screen. The controller board sends this message to the computer via serial communication, through the USB cable. In Arduino code, the structure of the program is divided into two functions: “setup” and “loop”:  “Setup” function is for configuring and starting some of the resources of the controller board. In this case the serial communication with PC is configured, in order to send messages to the computer, with a speed of 57600 bauds: void setup() { // Initialize serial communication Serial.begin(57600); }

 Loop part is for the main program, which executes in an infinite loop. In this case, it shows the message “Hello world” on the computer screen each second: void loop() { // Print message on the serial monitor Serial.println("Hello World"); // Delay of 1 second delay(1000); }

The complete program looks like this. Grey colour texts (preceded by “//”) are comments, i.e., they are not compiled but they are useful to understand the program. void setup() { // Initialize serial communication Serial.begin(57600); } void loop() { // Print message on the serial monitor Serial.println(“Hola mundo”); // Delay of 1 second delay(1000); }

www.moway-robot.com

8


Teacher’s guide

Programming introduction

You can test this program by copying it in the text editor of Arduino environment. Then the controller board is programmed by clicking on the “Upload” icon. IMPORTANT: It is necessary to select the right board, in the tab “Tools -> Board -> Arduino Uno”. Check the “Quick Guide” for more information.

After a few seconds the program is saved in the controller board. In order to see the result, click on the “Serial Monitor” icon:

The following window will appear. IMPORTANT: It is necessary to configure the serial communication speed to 57600 bauds in the marked field of the image.

Applications In this exercise we have learnt how to program the controller board of mOway Smart City. We have also seen how to send messages from controller board to computer. In other exercises we will use these concepts to display sensor values on the computer screen and to control the devices of the smart city from the computer.

www.moway-robot.com

9


Teacher’s guide

Variables and libraries

Exercise 2: Variables and libraries We will learn the concepts of "variable" and "library" in this exercise. The using of variables and libraries allow us to read the sensors of the smart city and control the barrier and streetlights. Therefore, the learned in this example will facilitate us the development of the next exercises.

Elements These are the elements of the mOway Smart City to be used in this exercise:  Controller board

Program 2.1: Variables In programming, a variable stores a value that normally varies throughout the program. Let’s see an example that uses a variable to show on the screen the seconds elapsed. Below, you can see the program flow diagram:

START (counter = 0)

Show the counter value

1 second delay

Increase counter

www.moway-robot.com

10


Teacher’s guide

Variables and libraries

The program begins defining the variable in which we will keep the seconds elapsed, called "seconds". It is an integer variable (int) and starts with a value of 0. For more information about the types of variables, see the reference page of Arduino, http://arduino.cc/en/Reference/HomePage. //******************************************************* // Variables //******************************************************* int seconds = 0; // Variable for counting seconds

In the “setup” function the serial communication with the computer is configured. This is necessary for sending the data to the computer in a correct way, in order to be displayed on the serial monitor of Arduino environment. In this case, the serial speed is configured to 57600 bauds. void setup() { // Initialize serial monitor Serial.begin(57600); }

In the “loop” function, the value of the variable "seconds" is written in the serial monitor. Then it waits for a second and it increases the variable. The loop function is an infinite loop, so this code is repeated indefinitely. Each time it is repeated, the variable "seconds" increases after waiting 1 second. void loop() { // Write the message on the serial monitor Serial.print("Seconds: "); Serial.println(seconds); // Delay of 1 second delay(1000); // Increase the counter of seconds seconds = seconds + 1;

}

www.moway-robot.com

11


Teacher’s guide

Variables and libraries

The complete code of the program is: //******************************************************* // Variables //******************************************************* int seconds = 0; // Variable for counting seconds //******************************************************* // // Main program // //******************************************************* void setup() { // Initialize serial monitor Serial.begin(57600); } void loop() { // Write the message on the serial monitor Serial.print("Seconds: "); Serial.println(seconds); // Delay of 1 second delay(1000); // Increase the counter of seconds seconds = seconds + 1; }

The result that appears on the serial monitor of Arduino environment is:

Later we will see how the variables are useful for working with mOway Smart City. For example, variables can be used to store the sensors values. Then these variables are read and the elements of the smart city can be controlled depending on these values.

www.moway-robot.com

12


Teacher’s guide

Variables and libraries

Libraries A library is a file which contains written code for performing some functions. The library allows us to use that code without writing it in our program. For example, the following actions would be necessary to read the light sensor: 1. Read the analog signal from the sensor. 2. Change the value of the signal at the correct scale. 3. Assign that value to a variable. If there not were a library, the user would have to write the code of all these actions in its program, which would complicate the code. In addition, some knowledge of electronics would be needed. For mOway Smart City, the use of the light sensor library makes it not necessary to know the functioning of it, because with only one line of code the library makes all the actions (read the port that is connected to the sensor, scale the value received, etc.). Now, we see an example of using libraries for showing the value of the light sensor on the computer screen. This example is similar to the previous one, but instead of increasing the value of a variable, we assign to this variable the value of a sensor.

START

Read sensor

Display the sensor value

1 second delay

The library of the light sensor ("light_sensor.h") and the library responsible for accessing the input and output ports of the controller board ("lib_io.h") are included. Also, the Wire library is included, since it is necessary for the mOway Smart City libraries. The libraries are added with the directive "#include":

www.moway-robot.com

13


Teacher’s guide

Variables and libraries

//******************************************************* // Libraries //******************************************************* #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <light_sensor.h> // Light sensor library

It is necessary to indicate the port plate which connects the light sensor, in this case is the connector 4 (CON4): //******************************************************* // Devices //******************************************************* LightSensor lightSensor(CON4); // Light sensor in the connector 4

The complete program is: //******************************************************* // Libraries //******************************************************* #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <light_sensor.h> // Light sensor library //******************************************************* // Devices //******************************************************* LightSensor lightSensor(CON4); // Light sensor in the connector 4 //******************************************************* // Variables //******************************************************* int lightLevel; // Variable for the value of the sensor //******************************************************* // // Main program // //******************************************************* void setup() { // Initialize serial monitor Serial.begin(57600); } void loop() { Serial.print("Light level: "); // Read light sensor LightLevel = lightSensor.Light(); // Display sensor value Serial.println(lightLevel); // Delay of 1 second delay(1000); }

www.moway-robot.com

14


Teacher’s guide

Variables and libraries

When you block the light sensor, the sensor value decreases (in this case, from 100% to 0% of light):

Applications The libraries and the variables are very useful for developing programs for controlling mOway Smart City. Variables will be used mainly for saving the values of the sensors, so that we will be able to control the elements of the smart city depending on the state of the city (environmental light, temperature, traffic state, etc.). Libraries will make easier the programming, because they make not necessary to develop the code for accessing to the elements of the city.

www.moway-robot.com

15


Teacher’s guide

Sensors reading

Exercise 3: Sensors reading In this exercise we will learn how to use mOway Smart City sensors. The controller card reads values from different sensors and then it will show them on the computer screen. In subsequent exercises we will use the information provided by the sensors to control the smart city elements in an autonomous way.

Elements These are the elements that will be used in this exercise:  Controller board  Proximity sensor  Light sensor  Temperature sensor

Sensor characteristics A sensor is an electronic device that provides information of the environment in which it is located. The smart city has proximity sensors, a light sensor and a temperature sensor. The following lines describe these sensors.

www.moway-robot.com

16


Teacher’s guide

Sensors reading

The proximity sensor indicates whether there is an object near itself, and how far it is. It has an infrared light transmitter and a receiver. If an object is near the sensor, the infrared light emitted by the transmitter bounces off the objet and it is received by the receiver of the sensor. The value provides by the sensor varies between 0% (not detected objet) and 100% (very close objet). The proximity sensor can be used to switch on the streetlights when mOway robot passes underneath them, or to detect the arrival of a mOway to the crossroad and activate the barrier.

The light sensor provides the amount of ambient light that receives the smart city. It works by an electronic device called photodiode, which generates an electrical current depending on the amount of light emitted upon it. It provides a value between 0% (total darkness) and 100% (bright light). This sensor can be used to switch on the streetlights when it is getting dark. The light sensor is distinguished by the brown coloured component indicated in the following picture:

The temperature sensor works by an electronic device which generates a voltage that varies depending on the ambient temperature. The value is provided in Celsius degrees or Fahrenheit degrees. This sensor can be used to monitor the environmental status of the smart city. The temperature sensor is distinguished by the black coloured component shown on the picture:

www.moway-robot.com

17


Teacher’s guide

Sensors reading

Program 3.1 The program starts including the libraries which contain the functions for reading the sensors: “proximity_sensor.h” for the proximity sensor, “light_sensor.h” for the light sensor, and “temperature_sensor.h” for the temperature sensor. Along with these three libraries it is necessary to include “lib_io.h” for input/output ports of the controller card and “Wire.h”, which is used by the previous libraries. //******************************************************* // Libraries //******************************************************* #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <proximity_sensor.h> // Proximity sensor library #include <light_sensor.h> // Light sensor library #include <temperature_sensor.h> // Temperature sensor library

Then the variables for storing the sensor values are defined. These are integer type variables (int). //******************************************************* // Variables //******************************************************* int distanceValue; // Stores the proximity sensor value int lightValue; // Stores the light sensor value int temperatureValue; // Stores the temperature sensor value

Next the devices that will be use are defined, in this case the sensors. In this part of the program it is defined the type of the sensor and how it is called it along the program, along with the port of the controller board where it is connected. NOTE: The name of the sensor can be anyone that the user chooses, but should be assigned a name easy to identify. The type of each sensor is defined on the mOway Smart City libraries and it must always the one in that code, i.e.:

Sensor

Type

Proximity Sensor

ProximitySensor

Light Sensor

LightSensor

Temperature Sensor

TemperatureSensor

www.moway-robot.com

18


Teacher’s guide

Sensors reading

In this exercise the proximity sensor is connected to the port 2 (CON2), the light sensor is in the port 4 (CON4) and the temperature sensor in the port 5 (CON5).

//******************************************************* // Devices //******************************************************* ProximitySensor proximitySensor(CON2); LightSensor lightSensor(CON4); TemperatureSensor temperatureSensor(CON5);

In the main program the proximity sensor is read, storing the sensor value in the variable e “distanceValue”. After that, the value is shown on the Arduino serial monitor. It does the same for the light and the temperature sensors. The loop ends whit a 1 second pause, to give time for reading the values on the screen. The complete program is as follows:

//******************************************************* // Libraries //******************************************************* #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <proximity_sensor.h> // Proximity sensor library #include <light_sensor.h> // Light sensor library #include <temperature_sensor.h> // Temperature sensor library

//******************************************************* // Variables //******************************************************* int distanceValue; // Stores the proximity sensor value int lightValue; // Stores the light sensor value int temperatureValue; // Stores the temperature sensor value

//******************************************************* // Devices //******************************************************* ProximitySensor proximitySensor(CON2); LightSensor lightSensor(CON4); TemperatureSensor temperatureSensor(CON5);

www.moway-robot.com

19


Teacher’s guide

Sensors reading

//******************************************************* // // Main program // //******************************************************* void setup() { // Initialize serial communication Serial.begin(57600); } void loop() { // Read proximity sensor and display the value distanceValue = proximitySensor.Distance(); Serial.print(“Proximity sensor:\t”); Serial.println(distanceValue); // Read light sensor and display the value lightValue = lightSensor.Light(); Serial.print(“Light sensor:\t\t”); Serial.println(lightValue); // Read temperature sensor and display the value temperatureValue = temperatureSensor.Temperature(C); Serial.print(“Temperature sensor:\t”); Serial.println(temperatureValue); Serial.println(“--------------------------------------“); delay(1000); // Pause of 1 second }

NOTE: The “\t” characters are used to tabulate the text. They are added for a clearer displaying of the result, but they are not essential. The result is shown below. The proximity sensor gives a value of 0, so it is not detecting any object. The light sensor gives a value of 79%, so it´s a medium-high level of light. Finally, the temperature sensor value is 20ºC.

Applications In the following exercises we will see how the elements of the smart city can be controlled autonomously thanks to the sensors.

www.moway-robot.com

20


Teacher’s guide

Barrier control

Exercise 4: Barrier control In this exercise we will learn how to manage the barrier of mOway Smart City. This element can be used to control the traffic through the crossroads and to practice with obstacle sensors of mOway robot.

Elements These are the elements of mOway Smart City that will be used in this exercise:  Controller board  Barrier

Barrier characteristics The barrier is moved by a servomotor which can raise the barrier, lower it or place it in a certain angle. The actuator of the barrier is controlled by a pulse width modulation signal (PWM). The library of the barrier generates this signal. The barrier can be placed in any of the holes of the circuit. In addition, it can be oriented according to the direction of mOway. IMPORTANT: The barrier can only be connected to port CON1, because this port is responsible for generating the modulated signal mentioned above.

www.moway-robot.com

21


Teacher’s guide

Barrier control

Program 4.1 This program shows all the actions that can be performed by the barrier. It consists in raising the barrier, lowering it and placing it in a 45 degrees angle. The program repeats these movements indefinitely.

START

Raise the barrier

Lower the barrier

Place the barrier at 45 degrees

The program begins by defining the libraries. The standard libraries "Wire.h" and "lib_io.h" are included, as we have seen in previous exercises. Also includes the barrier library ("barrier.h"). This one uses the "Servo.h" library, which is a library of Arduino to control servomotors. //******************************************************* // Libraries //******************************************************* #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O port Library #include <Servo.h> // Servomotor Library #include <barrier.h> // Barrier Library

Next it is defined the name of the barrier. Unlike the other elements of mOway Smart City, in this case we do not have to indicate the connection port. This is because it is always connected to the port CON1 on the controller card. //******************************************************* // Devices //******************************************************* Barrier barrier;

www.moway-robot.com

22


Teacher’s guide

Barrier control

The main program consists in raising the barrier, lowering it and placing the barrier in a 45 degrees angle. Between each movement the program waits for a second. In order to move the barrier we must follow four steps: 1. Firstly servomotor starts working with the "Init" funtion. It is necessary to specify the port of the barrier, which is always "CON1_DIG". 2. Then the action starts (move up, move down or place in an angle). 3. Then the program has to wait for about 300 milliseconds until the barrier reaches the indicated position. 4. Finally, the servomotor is stopped with the "Stop" function.

//******************************************************* // // Main program // //******************************************************* void setup() { } void loop() { // Raise the barrier barrier.Init(CON1_DIG); barrier.Up(); delay(300); barrier.Stop(); delay(1000); // Pause of 1 second // Lower the barrier barrier.Init(CON1_DIG); barrier.Down(); delay(300); barrier.Stop(); delay(1000); // Pause on 1 second // Barrier at 45 degrees barrier.Init(CON1_DIG); barrier.Angle(45); delay(300); barrier.Stop(); delay(1000); // Pause on 1 second }

NOTE: The pause of 300 milliseconds for the barrier to reach the desired position could be reduced or even eliminated, as long as the program takes 300 milliseconds to reach the next function of barrier moving.

www.moway-robot.com

23


Teacher’s guide

Barrier control

The complete program is as follows: //******************************************************* // Libraries //******************************************************* #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O port Library #include <Servo.h> // Servomotor Library #include <barrier.h> // Barrier Library //******************************************************* // Devices //******************************************************* Barrier barrier; //******************************************************* // // Main program // //******************************************************* void setup() { } void loop() { // Raise the barrier barrier.Init(CON1_DIG); barrier.Up(); delay(300); barrier.Stop(); delay(1000); // Pause of 1 second // Lower the barrier barrier.Init(CON1_DIG); barrier.Down(); delay(300); barrier.Stop(); delay(1000); // Pause on 1 second // Barrier at 45 degrees barrier.Init(CON1_DIG); barrier.Angle(45); delay(300); barrier.Stop(); delay(1000); // Pause on 1 second }

Applications With this exercise we learned the steps to follow in order to place the barrier in a specific position. This element will be used to control the traffic at the crossroads of the circuit, along with the proximity sensor

www.moway-robot.com

24


Teacher’s guide

Streetlights control

Exercise 5: Streetlights control Introduction In this exercise, we will learn how to manage streetlights in mOway Smart City. Different examples of code are provided, in order to show how to activate the streetlights. This will help to develop other exercises, such as adapting the smart city lighting to the ambient light, regulate light intensity depending on the traffic, etc.

Elements These are the elements of mOway Smart City that will be used in this exercise:  Controller board  Streetlights

Streetlights characteristics The mOway Smart City streetlights are the elements that allow us to lighting the smart city. They have four LEDs distributed along each streetlight post. Controller board accesses to these 2

elements via I C communication. This allows us to connect the streetlights in serial connection. So we only need one port of the controller card to connect all the streetlights. IMPORTANT: The streetlights should only be connected to port 3 of the controller board, 2

2

since it is the only port prepared for I C communication (port CON3 / I C).

www.moway-robot.com

25


Teacher’s guide

Streetlights control

Each streetlight is identified with a number from 1 to 4 in the bottom of the base of each post. As discussed below, it is necessary to specify this identifier in the program when we want to activate each streetlight:

Streetlight identifier (on the base)

Identifier in the program

1

STREETLIGHT_1

2

STREETLIGHT _2

3

STREETLIGHT _3

4

STREETLIGHT _4

The main LED is located in the top of the streetlight post. This LED is white and it is used to illuminate the circuit of the smart city. The intensity of the LED can be regulated with 7 different levels.

Light level

Identifier in the program

Level 1 (minimum intensity)

LEVEL_1

Level 2

LEVEL_2

Level 3

LEVEL_3

Level 4

LEVEL_4

Level 5

LEVEL_5

Level 6

LEVEL_6

Level 7 (maximum intensity)

LEVEL_7

Along the streetlight post there are three blue LEDs. They can be turned on and off individually, so they can be used as indicators or to get different appearances for the streetlight.

Post LED

Identifier in the program

Upper LED

UPPER

Middle LED

MIDDLE

Lower LED

LOWER

www.moway-robot.com

26


Teacher’s guide

Streetlights control

Now we will see different examples to learn how to use the functions that control these devices.

Program 5.1 Let's start with the simplest example, which consists of turning on the main LED of the streetlight. Firstly the libraries for streetlights (“streetlight.h”), the library for the ports of the controller board (“lib_io.h”) and “Wire.h” library are included: //******************************************************* // Libraries //******************************************************* #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <streetlight.h> // Streetlights library

Then the streetlight is defined. In this example, a type “1” streetlight is used (see the bottom of the streetlight base). Therefore, the streetlight is defined as "STREETLIGHT_1". //******************************************************* // Devices //******************************************************* // A streetlight of type “1” is defined Streetlight streetlight(STREETLIGHT_1);

NOTE: In this program there are not configuration functions, so that the “setup” function is empty.

www.moway-robot.com

27


Teacher’s guide

Streetlights control

The main program consists of turning on the streetlight, by activating the main LED with maximum intensity. Here is the code of the entire program.

//******************************************************* // Libraries //******************************************************* #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <streetlight.h> // Streetlights library //******************************************************* // Devices //******************************************************* // A streetlight of type “1” is defined Streetlight streetlight(STREETLIGHT_1); //******************************************************* // // Main program // //******************************************************* void setup() { } void loop() { // Turn on the streetlight to the maximum level streetlight.On(LEVEL_7); }

Program 5.2 Based on the code above, we will make the LED flash every second. In the main program it is added a pause of 500 milliseconds, a turning off of the lamp and another pause of 500 milliseconds. //******************************************************* // Libraries //******************************************************* #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <streetlight.h> // Streetlights library

//******************************************************* // Devices //******************************************************* // A streetlight of type “1” is defined Streetlight streetlight(STREETLIGHT_1);

www.moway-robot.com

28


Teacher’s guide

Streetlights control

//******************************************************* // // Main program // //******************************************************* void loop() { // Turn on the streetlight to the maximum level streetlight.On(LEVEL_7); delay(500); // 500ms pause // Turn off the streetlight streetlight.Off(); delay(500);

// 500ms pause

}

Program 5.3 In this example we will vary the light intensity of the main LED. This is achieved by choosing different levels of intensity when the streetlight is turned on. A pause will be added between changes in light intensity. The pause time is defined by the variable "time" in order to easily adjust the time between changes in intensity. With the value 300 (milliseconds) all the different levels of light can be seen. However, with a lower value (100 milliseconds, for example), the intensity variation is smoother.

//******************************************************* // Libraries //******************************************************* #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <streetlight.h> // Streetlights library

//******************************************************* // Variables //******************************************************* int time = 300; // Pause time between intensity changes //******************************************************* // Devices //******************************************************* // A streetlight of type “1” is defined Streetlight streetlight(STREETLIGHT_1);

www.moway-robot.com

29


Teacher’s guide

Streetlights control

//******************************************************* // // Main program // //******************************************************* void setup() { } void loop() { streetlight.On(LEVEL_1); delay(time); streetlight. On(LEVEL_2); delay(time); streetlight. On(LEVEL_3); delay(time); streetlight. On(LEVEL_4); delay(time); streetlight. On(LEVEL_5); delay(time); streetlight. On(LEVEL_6); delay(time); streetlight. On(LEVEL_7); delay(time); streetlight.Off(); delay(time); }

// Minimum level

// Maximum level // Turn off the streetlight

Program 5.4 In this example we will activate the post LEDs sequentially. These LEDs will be turned on one by one, from the lowest to the highest, and then turned off in reverse order. The main LED will remain off. It starts from the code above. The value of the variable "time" can be changed for the sequence to be slower or faster. //******************************************************* // Libraries //******************************************************* #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <streetlight.h> // Streetlights library

//******************************************************* // Variables //******************************************************* int time = 300; // Pause time between intensity changes //******************************************************* // Devices //******************************************************* // A streetlight of type “1” is defined Streetlight streetlight(STREETLIGHT_1);

www.moway-robot.com

30


Teacher’s guide

Streetlights control

//******************************************************* // // Main program // //******************************************************* void setup() { } void loop() { streetlight.Post(LOWER, ON); delay(time); streetlight.Post(MIDDLE, ON); delay(time); streetlight.Post(UPPER, ON); delay(time); streetlight.Post(UPPER, OFF); delay(time); streetlight.Post(MIDDLE, OFF); delay(time); streetlight.Post(LOWER, OFF); delay(time); }

// Turn on lower LED // Turn on middle LED // Turn on upper LED // Turn off upper LED // Turn off middle LED // Turn off lower LED

Program 5.5 Finally we will see an example that uses three streetlights, being of “1”, “2” and “3” types. To use the three streetlights in the program, we must define an instance for each of them, also indicating the identifier of each one. The name could be anything that the user wants, but in this example every streetlight is called just as its identifier in lowercase:

Streetlight identifier

Name

Identifier in the program

1

streetlight_1

STREETLIGHT_1

2

streetlight_2

STREETLIGHT_2

3

streetlight_3

STREETLIGHT_3

2

When several streetlights are used, the first one is connected to the port CON3 / I C of the controller board. After that the MiniUSB connector of the second streetlight is connected to the USB connector of the first one, and so on. For more information on streetlights mounting, see the "Quick Guide". Below is the code of this example. Each streetlight is turned on with a different intensity and a different lighting post.

www.moway-robot.com

31


Teacher’s guide

Streetlights control

//******************************************************* // Libraries //******************************************************* #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <streetlight.h> // Streetlights library //******************************************************* // Devices //******************************************************* Streetlight streetlight_1(STREETLIGHT_1); // Streetlight of type “1” Streetlight streetlight_2(STREETLIGHT_2); // Streetlight of type “2” Streetlight streetlight_3(STREETLIGHT_3); // Streetlight of type “3” //******************************************************* // // Main program // //******************************************************* void setup() { } void loop() { // Streetlight 1 at minimum intensity // One post LED is turned on streetlight_1.On(LEVEL_1); streetlight_1.Post(LOWER, ON); // Streetlight 2 at medium intensity // Two post LED are turned on streetlight_2. On (LEVEL_4); streetlight_2.Post(LOWER, ON); streetlight_2.Post(MIDDLE, ON); // Streetlight 3 at maximum intensity // Three post LED are turned on streetlight_3. On (LEVEL_7); streetlight_3.Post(LOWER, ON); streetlight_3.Post(MIDDLE, ON); streetlight_3.Post(UPPER, ON); }

Applications With these examples we have seen how we can control the LEDs of the streetlights. This is useful for developing further exercises, such as activate the streetlights according to the ambient light, the traffic, etc. This is achieved using the sensors to know the environment and, depending on their status, control the streetlights.

www.moway-robot.com

32


Teacher’s guide

Keyboard control

Exercise 6: Keyboard control Once we have learnt the code for activating the streetlights, moving the barrier and reading the sensors, we are going to control these elements from the keyboard of the computer. In this exercise we will see how to detect a pressed key from the computer and how to activate the corresponding device depending on the pressed key.

Elements These are the elements of mOway Smart City that will be used in this exercise:  Controller board  Streetlight type “1”  Barrier  Proximity sensor  Light sensor  Temperature sensor

www.moway-robot.com

33


Teacher’s guide

Keyboard control

The devices of the smart city are connected in the following ports:

Device

Controller board port

Barrier

CON1

Proximity sensor

CON2

Streetlight

CON3 / I C

Light sensor

CON4

Temperature sensor

CON5

2

Serial communication Data transmission between the computer and the controller board is done by serial communication, through the USB – MiniUSB cable (the cable used for programming the controller board). In this case, data are the pressed keys. This data is send from the computer through the serial monitor of Arduino environment.

In order to send a key to the controller board, this key is entered (“g” in the example below) and then the “Send” button is pressed. The controller board will receive de “g” key and then it will send the value of the sensors to the serial monitor NOTE: It is necessary to configure the serial communication speed of the monitor to be the same of the speed configured in the program, which is 57600 bauds as it is shown below:

www.moway-robot.com

34


Teacher’s guide

Keyboard control

Program 6.1 The main program is checking continuously if data has arrived to serial port, i.e., if a key has been sent from the serial monitor of Arduino environment. Every time a key is received in the serial port, it is stored in a variable. Then this variable is checked to know if its value is a key for activate some of the devices of the smart city. In this case the program activates the corresponding element. Finally it is necessary to reset the variable to avoid repeating the last activation.

START

Read the key

Corresponding action

YES

Expected key?

NO

on

Reset variable

The keys for controlling the devices are shown below:

Device

Key

Action

Barrier

B

Raise / Lower

Sensors

G

Display sensor values

Streetlight

Q/A

Turn on / turn off the streetlight

W/S

Turn on / turn off the post upper LED

E/D

Turn on / turn off the post middle LED

R/F

Turn on / turn off the post lower LED

www.moway-robot.com

35


Teacher’s guide

Keyboard control

All the elements of mOway Smart City are used, so that it is necessary to include all the libraries used in the previous exercises: //******************************************************* // Libraries //******************************************************* #include <Servo.h> // Servomotor library #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <barrier.h> // Barrier library #include <streetlight.h> // Streetlights library #include <proximity_sensor.h> // Proximity sensor library #include <light_sensor.h> // Light sensor library #include <temperature_sensor.h> // Temperature sensor library

We need a variable to store the key received from the computer, named “key”. We use another variable for storing the value of the sensors. In this case we use only one variable for all the sensors, because it will be refreshed every time a sensor is read. The variable “raised” is for knowing whether the barrier is up or down. //******************************************************* // Variables //******************************************************* char key; // Stores the received key boolean raised = false; int sensorVal;

The device definition: //******************************************************* // Devices //******************************************************* Barrier barrier; ProximitySensor proximitySensor(CON2); LightSensor lightSensor(CON4); TemperatureSensor temperatureSensor(CON5); Streetlight streetlight_1(STREETLIGHT_1); Streetlight streetlight_2(STREETLIGHT_2); Streetlight streetlight_3(STREETLIGHT_3);

www.moway-robot.com

36


Teacher’s guide

Keyboard control

The main program configures the serial communication (in function “setup”) and it checks continuously if there is data available in the serial port (“Serial.available). In this case, data is stored in the “key” variable and it executes the action corresponding to the key (“BarrierControl”, “StreetlightControl”, “SensorControl”).

//******************************************************* // // Main program // //******************************************************* void setup() { // Initialize serial communication Serial.begin(57600); } void loop() { // If a key has been sent, there are data in the serial port if(Serial.available() > 0) { // Read the key key = Serial.read(); BarrierControl(); StreetlightControl(); SensorControl(); // Reset variable key = 0; } }

In order to make easier to understand the program, the code has been divided into three functions. These functions are described in the following lines.

www.moway-robot.com

37


Teacher’s guide

Keyboard control

Barrier is activated with “B” key, both for raising and lowering it. The variable “raised” value is “true” when the barrier is up and it is “false when the barrier is down. Every time the “B” key is sent, it is check its state: if it is down, the action would be to raise it. Otherwise, if it is up the action would be to lower it.

BARRIER CONTROL

“B” key received?

NO

YES YES

Is the barrier raised?

Lower the barrier

NO

Raise the barrier

END

Here is the code of the function for controlling the barrier: //******************************************************* // Barrier control //******************************************************* void BarrierControl() { if(key == 'b' || key == 'B') { if(raised == true) { // Lower barrier raised = false; barrier.Init(CON1_DIG); barrier.Down(); delay(300); barrier.Stop (); } else { // Raise barrier raised = true; barrier.Init(CON1_DIG); barrier.Up(); delay(300); barrier.Stop(); } } }

www.moway-robot.com

38


Teacher’s guide

Keyboard control

In the case of the sensors, the function is easier. Sensors are read by sending “G” key. This function checks if the received key is “G” and in this case it sends the sensor values to the serial monitor.

SENSOR CONTROL

YES

“G” key received?

NO

Display sensor values

END

Here is the code of the function for checking the sensors of the smart city: //******************************************************* // Sensor control //******************************************************* void SensorControl() { if(key == 'g' || key == 'G') { sensorVal = proximitySensor.Distance(); Serial.print("Proximity sensor:\t"); Serial.println(sensorVal); sensorVal = lightSensor.Light(); Serial.print("Light sensor:\t\t"); Serial.println(sensorVal); sensorVal = temperatureSensor.Temperature(F); Serial.print("Temperature sensor:\t"); Serial.println(sensorVal); Serial.println("--------------------------------------"); } }

www.moway-robot.com

39


Teacher’s guide

Keyboard control

The four LEDs of the streetlight are turned on with “Q”, “W”, “E” and “R” keys, and “A”, “S”, “D” and “F” keys turn off the LEDs. The following diagram shows only two actions, to keep it clear. The rest of the actions would be similar, but with other keys.

STREETLIGHT CONTROL

Turn on main LED

YES “Q” key?

NO Turn on upper LED

YES “W” key?

NO (…)

END

Here is the code of the function for controlling the streetlight: //******************************************************* // Streetlight control //******************************************************* void StreetlightControl() { if(key == 'q' || key == 'Q') streetlight_1.On(LEVEL_7); else if(key == 'w' || key == 'W') streetlight_1.Post(UPPER, ON); else if(key == 'e' || key == 'E') streetlight_1.Post(MIDDLE, ON); else if(key == 'r' || key == 'R') streetlight_1.Post(LOWER, ON); else if(key == 'a' || key == 'A') streetlight_1.Off(); else if(key == 's' || key == 'S') streetlight_1.Post(UPPER, OFF); else if(key == 'd' || key == 'D') streetlight_1.Post(MIDDLE, OFF); else if(key == 'f' || key == 'F') streetlight_1.Post(LOWER, OFF); }

www.moway-robot.com

40


Teacher’s guide

Keyboard control

Finally this is the complete program: //******************************************************* // Libraries //******************************************************* #include <Servo.h> // Servomotor library #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <barrier.h> // Barrier library #include <streetlight.h> // Streetlights library #include <proximity_sensor.h> // Proximity sensor library #include <light_sensor.h> // Light sensor library #include <temperature_sensor.h> // Temperature sensor library

//******************************************************* // Variables //******************************************************* char key; // Stores the received key boolean raised = false; int sensorVal;

//******************************************************* // Devices //******************************************************* Barrier barrier; ProximitySensor proximitySensor(CON2); LightSensor lightSensor(CON4); TemperatureSensor temperatureSensor(CON5); Streetlight streetlight_1(STREETLIGHT_1); Streetlight streetlight_2(STREETLIGHT_2); Streetlight streetlight_3(STREETLIGHT_3);

//******************************************************* // // Main program // //******************************************************* void setup() { // Initialize serial communication Serial.begin(57600); } void loop() { // If a key has been sent, there are data in the serial port if(Serial.available() > 0) { // Read the key key = Serial.read(); BarrierControl(); StreetlightControl(); SensorControl(); // Reset variable key = 0; } }

www.moway-robot.com

41


Teacher’s guide

Keyboard control

//******************************************************* // // Functions // //******************************************************* //******************************************************* // Barrier control //******************************************************* void BarrierControl() { if(key == 'b' || key == 'B') { if(raised == true) { // Lower barrier raised = false; barrier.Init(CON1_DIG); barrier.Down(); delay(300); barrier.Stop (); } else { // Raise barrier raised = true; barrier.Init(CON1_DIG); barrier.Up(); delay(300); barrier.Stop(); } } }

//******************************************************* // Sensor control //******************************************************* void SensorControl() { if(key == 'g' || key == 'G') { sensorVal = proximitySensor.Distance(); Serial.print("Proximity sensor:\t"); Serial.println(sensorVal); sensorVal = lightSensor.Light(); Serial.print("Light sensor:\t\t"); Serial.println(sensorVal); sensorVal = temperatureSensor.Temperature(F); Serial.print("Temperature sensor:\t"); Serial.println(sensorVal); Serial.println("--------------------------------------"); } }

www.moway-robot.com

42


Teacher’s guide

Keyboard control

//******************************************************* // Streetlight control //******************************************************* void StreetlightControl() { if(key == 'q' || key == 'Q') streetlight_1.On(LEVEL_7); else if(key == 'w' || key == 'W') streetlight_1.Post(UPPER, ON); else if(key == 'e' || key == 'E') streetlight_1.Post(MIDDLE, ON); else if(key == 'r' || key == 'R') streetlight_1.Post(LOWER, ON); else if(key == 'a' || key == 'A') streetlight_1.Off(); else if(key == 's' || key == 'S') streetlight_1.Post(UPPER, OFF); else if(key == 'd' || key == 'D') streetlight_1.Post(MIDDLE, OFF); else if(key == 'f' || key == 'F') streetlight_1.Post(LOWER, OFF); }

NOTE: A group of keys can be sent alt the same time. For example, if you wanted to turn on all the LEDs of the streetlight, the next group of keys could be sent:

Applications This program is an example of how the smart city could be controlled remotely.

www.moway-robot.com

43


Teacher’s guide

Night lighting

Exercise 7: Night lighting In this exercise the streetlights will activate autonomously when the ambient light is low. The light sensor is used to detect de ambient light level.

Elements These are the elements of mOway Smart City that will be used in this exercise:  Controller board  Light sensor  Streetlight of type “1”

Strategy The objective of this exercise is to turn on the streetlights of mOway Smart City when it is getting dark, i.e., when the ambient light is below a certain level, and turn them off when it is day. The light sensor gives a value between 0% and 100%. In order to know whether it is dark or not, a reference value is chosen. If the value of the sensor is less, it means that it is dark and the streetlights have to be turned on. If the value of the sensor is higher than the reference it means that the ambient light is enough and the streetlights have to be turned off:

START

Read light sensor

YES Turn on streetlights

NO Is it dark?

www.moway-robot.com

Turn off streetlights

44


Teacher’s guide

Night lighting

Program 7.1 Firstly the light sensor library (“light_sensor.h”) and streetlight library (“streetlight.h”) are included, along with the usual libraries for mOway Smart City (“Wire.h” and “lib_io.h”). //******************************************** // Libraries //******************************************** #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O port Library #include <light_sensor.h> // Light sensor library #include <streetlight.h> // Streetlights library

A variable for storing the light sensor value is declared: //**************************************** // Variables //**************************************** int sensorVal; // Variable for light sensor value

The streetlight that will be used is defined. In this example only one streetlight is used (of 2

type “1”), in order to simplify the code. Streetlights are always connected to port CON3 / I C. As you can see below, the light sensor is connected in port CON4: //**************************************** // Devices //**************************************** LightSensor lightSensor(CON4); Streetlight streetlight(STREETLIGHT_1);

// Light sensor in port 4 // Streetlight of type “1” on port 3

In the main program the light sensor is read. If the read value is below the reference (25% in this case) then it is considered that the ambient light is low and that it is necessary to turn on the streetlights. However, if the value of the sensor is higher than 25% then it is considered that the ambient light is enough and the streetlight turns off. NOTE: The reference value can be adjusted depending on the environment in which it is located. The streetlight can turn on with 7 different levels of light intensity, from “LEVEL_1” for minimum intensity to “LEVEL_7” for maximum intensity. In this case the streetlight turns on with the maximum intensity.

www.moway-robot.com

45


Teacher’s guide

Night lighting

The complete program is shown below: //******************************************** // Libraries //******************************************** #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O port Library #include <light_sensor.h> // Light sensor library #include <streetlight.h> // Streetlights library

//**************************************** // Variables //**************************************** int sensorVal; // Variable for light sensor value //**************************************** // Devices //**************************************** LightSensor lightSensor(CON4); Streetlight streetlight(STREETLIGHT_1);

// Light sensor in port 4 // Streetlight of type “1” on port 3

//******************************************************* // // Main program // //******************************************************* void setup() { } void loop() { // Read light sensor sensorVal = lightSensor.Light(); // Compare this value with a reference of 25% if(sensorVal < 25) { streetlight.On(LEVEL_7); } else { streetlight.Off(); } }

Applications This exercise is an example of energy saving applied on the illumination of a city. When the ambient light is enough it is not necessary to keep the streetlights on, so that they can be turned off autonomously.

www.moway-robot.com

46


Teacher’s guide

Traffic lighting

Exercise 8: Traffic lighting One of the advantages of the smart cities is that they can adapt to the environment status for saving energy. In a normal city when it gets dark, the street lighting turns on and keeps on all the night long. If there were no traffic in this street, the energy of the streetlights would be wasted. A smart city can detect whether there is traffic in the street, and consequently adapt the streetlights illumination. So that, when there is no traffic the lights could be turned off or reduce its lighting level. In this exercise the streetlights will be turned on when the robot is detected by the proximity sensor. The streetlights are located on the red circles of the image below, while the proximity sensor is located on the blue circle.

Elements These are the elements of mOway Smart City that will be used in this exercise:  Controller board  Light sensor  Streetlights of typo “1” and “2”

www.moway-robot.com

47


Teacher’s guide

Traffic lighting

Strategy When an object is located in front of the proximity sensor, this sensor gives a value that varies depending on the distance to the object. This value varies between 0% (no object detected) and 100% (object very close). Once that the proximity sensor is read it is necessary to compare its value with a certain reference, so that if the sensor value is higher that the reference it means that the object has been detected. Now the diagram for this program is shown:

START

Read proximity sensor

YES Robot is detected?

Turn on streetlights

NO Turn off streetlights

Program 8.1 Firstly the libraries are included: //******************************************** // Libraries //******************************************** #include <Wire.h> #include <lib_io.h> #include <proximity_sensor.h> #include <streetlight.h>

Then a variable for storing the value of the sensor is defined. It will be the distance value in percentage. //**************************************** // Variables //**************************************** int sensorVal;

www.moway-robot.com

48


Teacher’s guide

Traffic lighting

Next the elements of the smart city are defined. The proximity sensor is connected to 2

CON2 and the streetligts in CON3 / I C. The type “1” and “2” streetlights are used. //**************************************** // Devices //**************************************** ProximitySensor proximitySensor(CON2); Streetlight streetlight_1(STREETLIGHT_1); Streetlight streetlight_2(STREETLIGHT_2);

In the main program the proximity sensor is read. When the sensor does not detect any object, its value is “0”. When an object is approaching the sensor its value increases. In this case a value of “5” is chosen for considering that the robot is passing below the streetlights (it means the 5% of the range of the sensor). NOTE: This value can be adjusted. The lower it is the easier the robot is detected, but this makes that it could detect other objects. When the robot is detected, the streetlights will turn on. Once the robot has passed the zone of the streetlights, the sensor will not detect it anymore and the streetlights will turn off. //******************************************************* // // Main program // //******************************************************* void setup() {} void loop() { // Read proximity sensor sensorVal = proximitySensor.Distance(); // Compare reference of 5% if(sensorVal > 5) { streetlight_1.On(LEVEL_7); streetlight_2.On(LEVEL_7); } else { streetlight_1.Off(); streetlight_2.Off(); } }

www.moway-robot.com

49


Teacher’s guide

Traffic lighting

The complete program is the following: //******************************************** // Libraries //******************************************** #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <proximity_sensor.h> // Proximity sensor library #include <streetlight.h> // Streetlights library

//**************************************** // Variables //**************************************** int sensorVal;

//**************************************** // Devices //**************************************** ProximitySensor proximitySensor(CON2); Streetlight streetlight_1(STREETLIGHT_1); Streetlight streetlight_2(STREETLIGHT_2);

//******************************************************* // // Main program // //******************************************************* void setup() {} void loop() { // Read proximity sensor sensorVal = proximitySensor.Distance(); // Compare reference of 5% if(sensorVal > 5) { streetlight_1.On(LEVEL_7); streetlight_2.On(LEVEL_7); } else { streetlight_1.Off(); streetlight_2.Off(); } }

www.moway-robot.com

50


Teacher’s guide

Traffic lighting

Program 8.2 For safety reasons in a real smart city the streetlights would not turn off, they would reduce the light intensity. The next program shows how to achieve this by changing the turning off function for turning on function with a lower level of light. Libraries, variables and devices definitions are the same that the previous program. //******************************************** // Libraries //******************************************** #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <proximity_sensor.h> // Proximity sensor library #include <streetlight.h> // Streetlights library //**************************************** // Variables //**************************************** int sensorVal; //**************************************** // Devices //**************************************** ProximitySensor proximitySensor(CON2); Streetlight streetlight_1(FAROLA_1); Streetlight streetlight_2(FAROLA_2);

//******************************************************* // // Main program // //******************************************************* void setup() {} void loop() { // Read proximity sensor sensorVal = proximitySensor.Distance(); // Compare reference of 5% if(sensorVal > 5) { streetlight_1.On(LEVEL_7); streetlight_2.On(LEVEL_7); } else { streetlight_1.On(LEVEL_1); streetlight_2.On(LEVEL_1); } }

www.moway-robot.com

51


Teacher’s guide

Crossing control

Exercise 9: Crossing control The circuit of mOway Smart City has a crossroads on the central zone, if there were robots circulating along the circuit they could crash in the crossing. In order to avoid it, the barrier and the proximity sensor can be used. When one of the robots reaches the crossing, it will be detected by the sensor and the barrier will move down, so that the other robot will wait.

Elements These are the elements that will be used in this exercise:  Controller board  Barrier  Proximity sensor  2 mOway robots

Strategy In this exercise 2 mOway robots will go along the inside circuits. So that the robots have to be programmed to follow the black line. Some tips:  Each robot has to go straight forward when one line sensor detects the white colour and the other one detects the black colour.  Otherwise the robot must correct its trajectory to reach the previous state. Robot 1 will circulate on the direction of the purple arrows (next image). When this robot reaches the crossing it will be detected by the proximity sensor. When the controller board receives de sensor signal, it will move down the barrier to avoid the robot 2 to enter the crossing.

www.moway-robot.com

52


Teacher’s guide

Crossing control

Robot 2 will circulate on the direction of blue arrows. In has to follow the line and also check its obstacle sensors continuously, for detect when the barrier is down and to stop in this case. NOTE: The barrier and the proximity sensor can be placed as the image shows. It is also possible to exchange their positions for changing the direction of the robots.

The program reads the proximity sensor and when its value is higher than the reference, this means that the robot 1 is approaching the crossing and the barrier is lowered. When the robot 1 passes the crossing, the proximity sensor does not detect it and the barrier is raised, so that robot 2 can reach the crossing.

START

Read proximity sensor

YES Lower barrier

Robot 1 detected?

www.moway-robot.com

NO Raise barrier

53


Teacher’s guide

Crossing control

Program 9.1 The program begins with the libraries definition: //******************************************************* // Libraries //******************************************************* #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <Servo.h> // Servomotor library #include <barrier.h> // Barrier library #include <proximity_sensor.h> // Proximity sensor library

A variable is defined for storing the proximity sensor value. In this case, the sensor value is in CON2. The barrier is always connected to CON1: //******************************************************* // Variables //******************************************************* int distance; //******************************************************* // Devices //******************************************************* Barrier barrier; ProximitySensor proximitySensor(CON2);

In the main program the value on proximity sensor is compared with a reference of 5%. If it is higher, it means that the robot 1 is detected and the barrier is lowered. In case this value is lower, it means that the robot 1 has passed the crossing and the barrier is raising, letting the robot 2 to advance. //******************************************************* // // Main program // //******************************************************* void setup() {} void loop() { distance = proximitySensor.Distance(); if(distance > 5) { // Lower barrier barrier.Init(CON1_DIG); barrier.Down(); delay(300); barrier.Stop(); // Wait for the robot 1 to pass the crossing delay(1000); }

www.moway-robot.com

54


Teacher’s guide

Crossing control

else { // Raise the barrier barrier.Init(CON1_DIG); barrier.Up(); delay(300); barrier.Stop(); } }

The complete program is the following: //******************************************************* // Libraries //******************************************************* #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <Servo.h> // Servomotor library #include <barrier.h> // Barrier library #include <proximity_sensor.h> // Proximity sensor library //******************************************************* // Variables //******************************************************* int distance; //******************************************************* // Devices //******************************************************* Barrier barrier; ProximitySensor proximitySensor(CON2); //******************************************************* // // Main program // //******************************************************* void setup() {} void loop() { distance = proximitySensor.Distance(); if(distance > 5) { // Lower barrier barrier.Init(CON1_DIG); barrier.Down(); delay(300); barrier.Stop(); // Wait for the robot 1 to pass the crossing delay(1000); } else { // Raise the barrier barrier.Init(CON1_DIG); barrier.Up(); delay(300); barrier.Stop(); } }

www.moway-robot.com

55


Teacher’s guide

Crossing control

Applications This program is an example of traffic regulation in a crossroads by means of a proximity sensor and a barrier. A normal city regulates the traffic with traffic lights. In some roads a presence senor could be installed so that the traffic lights could turn red only when there are other vehicles, for avoid unnecessary waiting. Another example is a level crossing: when it is detected that the train is passing, the barriers are lowered to avoid the vehicles to enter the rails.

www.moway-robot.com

56


Teacher’s guide

Smart city

Exercise 10: Smart city In this exercise we will make the mOway Smart City to manage itself autonomously. All the elements of the city will be used. The functions of the smart city are the following:  Crossroads control  Night lighting  Traffic lighting

As in the previous exercise, two mOway robots will be circulating along the inside part of each circuit. So that mOways have to be programmed to follow the line and to stop when the barrier is down. NOTE: The program of the robots can be completed by using other sensors of mOway: light sensor for turning on its frontal LED, its accelerometer, etc.

Elements These are the elements used in this exercise:  Controller board  Streetlights  Barrier  Proximity sensor  Light sensor  Temperature sensor  2 mOway robots

www.moway-robot.com

57


Teacher’s guide

Smart city

Below the locations for the elements are shown, along with the ports of the controller board in which they are connected. As we have seen in the other exercises, streetlights are connected 2

to CON3 / I C, in series. NOTE: Streetlight of type “4” is not included in the basic kit of mOway Smart City. It must be purchased separately.

Function Crossing control

Traffic lighting

Night lighting

Device

Symbol

Controller board port

Barrier

CON1

Proximity sensor

CON2

Proximity sensor

CON5

2

Streetlight1

F1

CON3 / I C

Streetlight 2

F2

CON3 / I C

Light sensor

SL

CON4

Streetlight 3

F3

CON3 / I C

Streetlight 4

F4

CON3 / I C

www.moway-robot.com

2

2 2

58


Teacher’s guide

Smart city

All the sensors are read and, depending on their values, the smart city will make the corresponding action. In order to make the program easy to understand, the code is divided into functions that perform different tasks.

START

Crossing control

Traffic lighting

Night lighting

www.moway-robot.com

59


Teacher’s guide

Smart city

Crossing control function detects when the robot 1 reaches the crossroads. When this happens, this function moves down the barrier to avoid the robot 2 to enter the crossroads. Once the robot 1 has passed the barrier moves up to let the robot 2 resume its advance. The variable “raised” is use to indicate the state of the barrier. If the robot 1 is detected and the barrier is already raised, it is no necessary to move it up again. Similarly if the barrier is down and the robot 1 has passed, it is not necessary to move it down again. This is for avoiding the activation of the servomotor when it is not necessary to move the barrier.

CROSSING CONTROL

Read the proximity sensor of the crossing

YES

Barrier is raised?

NO

Robot 1 is detected?

NO

YES

Barrier is raised?

YES

NO

Lower barrier

Raise barrier

END

www.moway-robot.com

60


Teacher’s guide

Smart city

The code of the function for controlling the crossing is as follows: //******************************************************* // Crossing control //******************************************************* void CrossingControl () { // Detect robot sensorValue = crossingSensor.Distance(); if(sensorValue > 5) { // Robot 1 has been detected if(raised == true) { // If the barrier is up, move it down barrier.Init(CON1_DIG); barrier.Down(); delay(300); barrier.Stop(); delay(1000); raised = false; } } else { // Robot 1 has been detected if(raised == false) { // If the barrier is down, move it up barrier. Init (CON1_DIG); barrier.Up(); delay(300); barrier.Stop(); delay(1000); raised = true; } } }

www.moway-robot.com

61


Teacher’s guide

Smart city

Traffic lighting function reads the proximity sensor that is next to the streetlights. When the robot passes below the streetlights, the sensor will detect it and the streetlights will turn on. Once the robot is gone, the sensor does not detect it and the streetlights turn off.

TRAFFIC LIGHTING

Read streetlights proximity sensor

YES

Robot is detected?

Turn on streetlights

NO Turn off streetlights

END

The code of the function for controlling the traffic lighting is as follows: //******************************************************* // Traffic lighting //******************************************************* void TrafficLighting() { // Detect robot sensorValue = streetlightSensor.Distance(); if(sensorValue > 5) { // Turn on streetlights streetlight_1.On(LEVEL_7); streetlight_1.Post(LOWER,ON); streetlight_1.Post(MIDDLE,OFF); streetlight_1.Post(UPPER,ON); streetlight_2.On(LEVEL_7); streetlight_2.Post(LOWER,ON); streetlight_2.Post(MIDDLE,OFF); streetlight_2.Post(UPPER,ON); } else { // Turn off streetlights streetlight_1.Off(); streetlight_1.Post(LOWER,OFF); streetlight_1.Post(MIDDLE,ON); streetlight_1.Post(UPPER,OFF); streetlight_2.Off(); streetlight_2.Post(LOWER,OFF); streetlight_2.Post(MIDDLE,ON); streetlight_2.Post(UPPER,OFF); } }

www.moway-robot.com

62


Teacher’s guide

Smart city

Night lighting function changes the streetlights illumination depending on the ambient light. If it is dark the streetlights will turn on with the maximum level of lighting. If the ambient light is low, the streetlights will turn on with the minimum level. Otherwise, if the ambient light is enough, the streetlights will turn off.

NIGHT LIGHTING

Read light sensor

YES Is it dark? Turn on (maximum level)

NO

YES Low light? Turn on (minimum level)

NO

Turn off streetlights

END

The code of the function for controlling the night lighting is as follows: //******************************************************* // Night lighting //******************************************************* void NightLighting() { // Detect ambient light sensorValue = lightSensor.Light(); if(sensorValue < 30) { // If it is dark, turn on the maximum level streetlight_3.On(LEVEL_7); streetlight_3.Post(LOWER,ON); streetlight_3.Post(MIDDLE,ON); streetlight_3.Post(UPPER,ON); streetlight_4.On(LEVEL_7); streetlight_4.Post(LOWER,ON); streetlight_4.Post(MIDDLE,ON); streetlight_4.Post(UPPER,ON); }

www.moway-robot.com

63


Teacher’s guide

Smart city

else if(sensorValue < 60) { // If it is not too dark, turn on the minimum level streetlight_3.On(LEVEL_1); streetlight_3.Post(LOWER,ON); streetlight_3.Post(MIDDLE,ON); streetlight_3.Post(UPPER,OFF); streetlight_4.On(LEVEL_1); streetlight_4.Post(LOWER,ON); streetlight_4.Post(MIDDLE,ON); streetlight_4.Post(UPPER,OFF); } else { // If it is not dark, turn off the streetlights streetlight_3.Off(); streetlight_3.Post(LOWER,ON); streetlight_3.Post(MIDDLE,OFF); streetlight_3.Post(UPPER,OFF); streetlight_4.Off(); streetlight_4.Post(LOWER,ON); streetlight_4.Post(MIDDLE,OFF); streetlight_4.Post(UPPER,OFF); } }

Finally here is the complete code of the smart city program: //******************************************************* // Libraries //******************************************************* #include <Servo.h> // Servomotor library #include <Wire.h> // I2C communication library #include <lib_io.h> // I/O ports library #include <barrier.h> // Barrier library #include <streetlight.h> // Streetlights library #include <proximity_sensor.h> // Proximity sensor library #include <light_sensor.h> // Light sensor library

//******************************************************* // Variables //******************************************************* int sensorValue; boolean raised;

//******************************************************* // Devices //******************************************************* Barrier barrier; // Barrier ProximitySensor crossingSensor(CON2); // Proximity sensor of the crossing LightSensor lightSensor(CON4); // Light sensor ProximitySensor streetlightSensor(CON5); // Proximity sensor of the streetlights Streetlight streetlight_1(STREETLIGHT_1); // Presence activated streetlight Streetlight streetlight_2(STREETLIGHT_2); // Presence activated streetlight Streetlight streetlight_3(STREETLIGHT_3); // Light activated streetlight Streetlight streetlight_4(STREETLIGHT_4); // Light activated streetlight

www.moway-robot.com

64


Teacher’s guide

Smart city

//******************************************************* // // Main program // //******************************************************* void setup() { } void loop() { CrossingControl(); TrafficLighting(); NightLighting(); }

//******************************************************* // // Functions // //******************************************************* //******************************************************* // Crossing control //******************************************************* void CrossingControl () { // Detect robot sensorValue = crossingSensor.Distance(); if(sensorValue > 5) { // Robot 1 has been detected if(raised == true) { // If the barrier is up, move it down barrier.Init(CON1_DIG); barrier.Down(); delay(300); barrier.Stop(); delay(1000); raised = false; } } else { // Robot 1 has been detected if(raised == false) { // If the barrier is down, move it up barrier. Init (CON1_DIG); barrier.Up(); delay(300); barrier.Stop(); delay(1000); raised = true; } } }

www.moway-robot.com

65


Teacher’s guide

Smart city

//******************************************************* // Traffic lighting //******************************************************* void TrafficLighting() { // Detect robot sensorValue = streetlightSensor.Distance(); if(sensorValue > 5) { // Turn on streetlights streetlight_1.On(LEVEL_7); streetlight_1.Post(LOWER,ON); streetlight_1.Post(MIDDLE,OFF); streetlight_1.Post(UPPER,ON); streetlight_2.On(LEVEL_7); streetlight_2.Post(LOWER,ON); streetlight_2.Post(MIDDLE,OFF); streetlight_2.Post(UPPER,ON); } else { // Turn off streetlights streetlight_1.Off(); streetlight_1.Post(LOWER,OFF); streetlight_1.Post(MIDDLE,ON); streetlight_1.Post(UPPER,OFF); streetlight_2.Off(); streetlight_2.Post(LOWER,OFF); streetlight_2.Post(MIDDLE,ON); streetlight_2.Post(UPPER,OFF); } }

//******************************************************* // Night lighting //******************************************************* void NightLighting() { // Detect ambient light sensorValue = lightSensor.Light(); if(sensorValue < 30) { // If it is dark, turn on the maximum level streetlight_3.On(LEVEL_7); streetlight_3.Post(LOWER,ON); streetlight_3.Post(MIDDLE,ON); streetlight_3.Post(UPPER,ON); streetlight_4.On(LEVEL_7); streetlight_4.Post(LOWER,ON); streetlight_4.Post(MIDDLE,ON); streetlight_4.Post(UPPER,ON); } else if(sensorValue < 60) { // If it is not too dark, turn on the minimum level streetlight_3.On(LEVEL_1); streetlight_3.Post(LOWER,ON); streetlight_3.Post(MIDDLE,ON); streetlight_3.Post(UPPER,OFF);

www.moway-robot.com

66


Teacher’s guide

Smart city

streetlight_4.On(LEVEL_1); streetlight_4.Post(LOWER,ON); streetlight_4.Post(MIDDLE,ON); streetlight_4.Post(UPPER,OFF); } else { // If it is not dark, turn off the streetlights streetlight_3.Off(); streetlight_3.Post(LOWER,ON); streetlight_3.Post(MIDDLE,OFF); streetlight_3.Post(UPPER,OFF); streetlight_4.Off(); streetlight_4.Post(LOWER,ON); streetlight_4.Post(MIDDLE,OFF); streetlight_4.Post(UPPER,OFF); } }

www.moway-robot.com

67


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.