We are finally here, watching the last snippet of Week 2, where we are going to summarize everything that we have learnt
in the course of the week. We are going to assemble a monitoring system device. It will be gathering some data from the outside world, and then
will communicate it to us in various ways. How can this system come in useful? The simplest and most interesting example is the following: you might want to
gather some data for a scientific research. Besides, you might want to signal when one of your parameters changes. We are going to take this into consideration in our project
here. As we already know, the process of reading the analog signal is always the same for a controller, therefore we will study light and temperature changes as an example. Let’s outline our future device on a diagram. We will have three inputs: two of them will be analog inputs of the light and temperature detectors, whilst one will be a digital input
represented by a switch: it will be switching the data that we are outputting on the bar graph. The bar graph is our first output device. We can do without a micro circuit here, as the previous example was
already complex enough. Thus, we have our bar graph, which is our output device, a piezo buzzer and data exchange with computers – they represent
both the input and the output. Next, how are we going to check whether or not our device is
ready? We want it to send data to the computer, following our queries. If we request that the temperature is marked with a “t” symbol, the
device will be transmitting us
the data on current temperature. If we request that brightness is marked as “l”, it will be sending us the data on brightness to the Port Monitor. The bar graph will be continuously displaying data: it will display
one of the parameters first, and then it will display the second parameter after we
have pressed the switch. Furthermore, we are going to establish a borderline, so
that when the temperature parameter is exceeded, there will be a beep. You must be aware of the fact that in place of sound signals, we could turn on the cooling system or trigger any other
reaction. However, it’s not what interests us the most at the moment. Let's better look at how to assemble this device. I want to introduce you to one more component, which makes part of our ecosystem. I won’t be hiding my pride, because I can say that I’ve also participated
in its creation. These are constructor details which help us prototype our device even faster. They are made of expanded PVC, which is easy to work, because out of a standard component, you can make a more suitable one with the help of scissors, glue,
screws, etc. They are easy to use, as they have notches for various standard components. For example, for Troyka-modules. Since all Troyka-modules have standard dimensions, you can use one single detail to secure anything that your heart desires. Let’s see how I will assemble this device. On one side, we have a panel for four Troyka-modules, which holds all the necessary components: the light sensor, the switch, the temperature detector and the piezo buzzer. On the other side, we have it all in the old-fashioned way: there
is a small breadboard with a
bar graph. You can see for yourself here which mounting method is more simplified and aesthetic. I haven’t been hiding from you the fact that inside we have Arduino
with the Troyka-shield. Arduino terminal has all the bar graphs connected to it, whereas all the four modules are connected each to their own
inputs. When we look at the sketch more attentively, we will see where
to connect all the components. Now let’s figure out what the program consists of. I would recommend those who still have some energy left, to try and create the program on your own following my description. Just pause the video and try your hand at it. Meanwhile, we are going to continue here and see what I’ve got. I have taken one of the sketches that we used during Week 2
as the basis, the one where we were outputting the potentiometer’s reading
to the bar graph. I still have the pin marking here. I have also added pins for the light and temperature sensors. Besides, I have added a pin for the buzzer and the switch. This line determines the borderline which, when exceeded, will activate the sound signal. I have also determined some variables that we will be needing: the variables to store temperature and brightness, one variable to count the number of LEDs, which will be turning on in the Troyka- shield bar graph (you are by the way
going to see now how they work), and two variables which we introduced when we worked with the trigger, to store the information on the previous state of the switch and the trigger
itself. Both of them are of the bool type. In the setup, we still have the lines left from the example with the bar
graph, where all the pins that have the bar graph connected to them, have been
made outputs. The buzzer’s pin is the output here, and the switch’s pin is the input. Let me remind you that the “Switch” module that I’m using has a pull-up power resistor, that’s why we don’t need
to set the input pull-up
mode, and we remember that the switch will give us 0 when it’s on, and 1 when it’s
off. I am establishing connection with the computer, and outputting a prompt line, which tells us to introduce “t” to receive data on the temperature, and “l” for the data on brightness. What happens in the loop? You can watch me take various parts of our previous experiments and unite them in
one new experiment. For starters, I shall save the data we read off at our analog inputs in the introduced variables. Consequently, the data from the temperature detector will be
saved in the temperature
variable, and the data
from the light sensor will be saved in the brightness variable. Next comes the fragment which is responsible for data exchange
with the computer. In case if the buffer is not empty, and we have received something
from the computer, we shall check what we have there, save it in the mode variable and have a look at the data. If we have a small t or capital T there, as the user can accidentally press Shift or Caps Lock and send the letter in the wrong register, and the device will have to recognize
this, then we shall output the word “Temperature” in the serial
port, as well as tabulation and the temperature value. In the same manner, we should check if we have received a small l or a
capital L, and then output the message on brightness if we have a small l. We shall then learn how to control the device and output data on the bar graph. We first need to count the parameters on the switch, as we did before, in order to compare them with previous values and change the trigger if the switch has just been pressed. We then need to assign the previous value to the current one, just
as we did before. Depending on the true or false condition of the trigger, we will save either temperature or brightness in the countLeds
variable, which we will be using next. We have then taken the line from the recent example, where we determined the last LED which was on in the bar graph,
and wrote values from 2 to 11 in lastLed. Thanks to map, this value was changed from its initial value of 0-500, which was saved in the countLeds variable. It’s all the same now whether we have temperature or brightness
stored there. We just have a value there. I have decided not to take the full range of 1023, because neither the
light sensor, nor the temperature detector will display any value higher than 500. We get the same cycle that we had in the example with the lastLed variable. We shall turn on as many LEDs as we need in the current situation. As a result, we can output either temperature or brightness. The last function we need to perform is to make sure that we have a sound signal if the temperature level exceeds the determined borderline, in other words, if it becomes too
hot. We will be setting the beep at 2 khz with the duration of 1 second. I want to point out that I have decided not to overload the program by transmitting the read parameters to the C system, for instance. We only work with those standard units which we get when the analog signal is read at the input. If you want, you can find or calculate the formulas, with the help of which you can turn temperature into degrees, and brightness into lumens,
for instance. Let’s now look how this works. For starters, we shall look how the bar graph works. By default, it displays brightness, and if I cover the light sensor with my hand, we shall see that the number of LEDs changes. Then we can press the switch and switch the bar graph into the mode where it displays temperature. Now we can see that three points are on. The fastest way to heat up the sensor is to use a lighter. Apart from the fact that we have seen the points go up on the bar graph, we have also heard a sound signal, because the temperature has
exceeded the borderline. Okay, the display mode seems to work correctly. Now let’s start the Port Monitor and, as we wrote in the setup, we get a prompt about which symbol should request which parameter. For instance, let’s try to request temperature. It shows us these standard units – 162. Let’s check if the capital letter mode works. Yep, it stays the same. Let’s test brightness and then the capital letter, and actually let’s even change brightness. It has change indeed, in fact, it has increased. This is the nature of this light sensor: it increases the reading while brightness decreases. That’s it. Our device works properly. It can now be a useful device, or a fragment of some bigger useful device, and it summarizes almost everything that we have been learning over the course of Week 2. Now I suggest that you take some rest and then try to use all the newly acquired knowledge in the test. I hope, of course, that you are still looking forward to Week 3. See you later.