1 00:00:00,370 --> 00:00:06,370 Hello! In this exercise we are going to learn how to use a distance sensor. 2 00:00:08,369 --> 00:00:13,890 You will be able to find it in the basic library, it's somewhere here, okay here, 3 00:00:13,890 --> 00:00:19,969 Sensor de Distancia en EspaƱol. Just click and drag and connect it to the Arduino board this way. 4 00:00:19,969 --> 00:00:27,969 There is, I'm going to zoom in a little bit, there is a ground connection connected to the ground of 5 00:00:27,969 --> 00:00:35,729 of the Arduino board, a 5V connection connected to the 5V of the Arduino board here, 5A ground, 6 00:00:35,729 --> 00:00:42,270 and there is also a control wire, a control connector, that we are going to connect it 7 00:00:42,270 --> 00:00:44,250 to 13, for instance. 8 00:00:44,250 --> 00:00:48,969 So this is the way we have connected our distance sensor. 9 00:00:48,969 --> 00:00:50,210 How can we program it? 10 00:00:50,210 --> 00:00:51,210 Really easy. 11 00:00:51,210 --> 00:00:58,090 If we open the code, let's focus on the first block. 12 00:00:59,350 --> 00:01:00,109 It's here. 13 00:01:00,990 --> 00:01:04,189 Well, first of all, we are going to use a variable that is called distance, 14 00:01:04,450 --> 00:01:06,150 and we are going to use the block defineer. 15 00:01:06,390 --> 00:01:11,549 This is the way we take a value and we store it inside the variable. 16 00:01:12,189 --> 00:01:14,049 And the interesting one is the purple block. 17 00:01:14,109 --> 00:01:17,730 The purple block that is a little sensor, the distance ultrasonic. 18 00:01:17,730 --> 00:01:19,129 I'm going to zoom a little bit. 19 00:01:19,129 --> 00:01:25,469 Leer el sensor de distancia ultrasonico en el pasador del desencadenador. 20 00:01:25,469 --> 00:01:35,010 Too much pieces of vocabulary to say that the sensor is going to be connected to 13. 21 00:01:35,010 --> 00:01:43,450 This is where we connect the sensor, igual que el desencadenador, forget about it, just 22 00:01:43,450 --> 00:01:49,010 be sure this is the way you have it, and we are going to measure the distance in centimeters. 23 00:01:49,010 --> 00:01:55,310 we use this purple block to read a number and store it inside this 24 00:01:55,310 --> 00:01:59,650 variable. And with this second block, you already know it, we are going to send the 25 00:01:59,650 --> 00:02:05,689 value to the serial monitor, ok? Let's have a look at the simulation, but I'm 26 00:02:05,689 --> 00:02:10,969 sure you already understand how it would work. We will read a distance and we will 27 00:02:10,969 --> 00:02:16,009 send it to the Arduino board and we will be able to see it using the serial 28 00:02:16,009 --> 00:02:21,750 monitor okay so let's start the simulation okay I'm going to close it 29 00:02:21,750 --> 00:02:26,629 make it easier to understand I'm going to zoom out a little bit and nothing 30 00:02:26,629 --> 00:02:34,250 happens apparently but if I click here let's have a look this one 31 00:02:34,250 --> 00:02:41,370 appears the ball is simulating a kind of object in the in the close to the sensor 32 00:02:41,370 --> 00:02:56,310 And if I open the serial monitor, you will be able to see that here the distance is 303 centimeters, and we are reading 303 centimeters. 33 00:02:56,629 --> 00:03:08,129 If I move the ball closer, the distance is 192 centimeters, and here, for instance, 63 centimeters, we are reading 63. 34 00:03:08,129 --> 00:03:10,469 It means that the simulation works. 35 00:03:10,830 --> 00:03:12,409 Fine, perfect. Really easy. 36 00:03:12,409 --> 00:03:22,590 but I agree that this is not really really really easy to understand how it 37 00:03:22,590 --> 00:03:31,030 works in real life so I'm going to to show you a real distance sensor measuring 38 00:03:31,030 --> 00:03:38,560 in the real life in real life I mean you have it here I have created this code 39 00:03:38,560 --> 00:03:43,180 and I have uploaded it to Arduino board and the Arduino board now it's already 40 00:03:43,180 --> 00:03:50,740 working. I'm going to use this book as an object and what the object is far away 41 00:03:50,740 --> 00:03:55,900 there's a green light. Can you see the green light? If the object moves 42 00:03:55,900 --> 00:04:04,740 closer we have a yellow light and if the object is even 43 00:04:04,740 --> 00:04:10,099 closer there is a red light. So we are measuring distances and the 44 00:04:10,099 --> 00:04:13,340 Arduino board is behaving according to the distance. Can you see it? 45 00:04:14,639 --> 00:04:17,699 So this is the exercise you will have to program using 46 00:04:17,699 --> 00:04:21,519 the simulation, of course, because you don't have the real Arduino board, okay? 47 00:04:22,300 --> 00:04:26,199 Can you see that it works? Okay, fine, perfect. So let's 48 00:04:26,199 --> 00:04:30,300 go back to our simulation. Here, you have 49 00:04:30,300 --> 00:04:34,180 everything you need. You have the sensor. When you are simulating, you 50 00:04:34,180 --> 00:04:38,120 can click on it, remember? And if you are simulating, you can click on it 51 00:04:38,120 --> 00:04:40,779 and you will have a virtual object to move it. 52 00:04:42,759 --> 00:04:44,600 You have these pieces of code, 53 00:04:45,600 --> 00:04:49,300 reading using the purple block and storing it inside a variable, 54 00:04:49,600 --> 00:04:54,220 and you will use this variable to check the conditions. 55 00:04:54,759 --> 00:04:57,740 If the distance is, for instance, I don't know. 56 00:04:58,199 --> 00:04:59,120 Let's have a look with the... 57 00:04:59,120 --> 00:04:59,480 Oh, I'm sorry. 58 00:05:00,259 --> 00:05:00,839 I made a mistake. 59 00:05:03,279 --> 00:05:04,259 Now it will work. 60 00:05:04,259 --> 00:05:17,819 If the distance, for instance, is higher than, I don't know, for instance, 200 centimeters, a green LED will be shining. 61 00:05:18,639 --> 00:05:30,360 If the distance is between 200 and 100, for instance, or yes, 100 is a good value, 100, and a yellow LED will be shining. 62 00:05:30,360 --> 00:05:36,600 and if the distance is lower is lower than 100 centimeters a red led will be shining okay 63 00:05:36,600 --> 00:05:43,439 what do you need you need three leds connected here with their protection resistors connected 64 00:05:43,439 --> 00:05:52,899 to for instance 12 11 and 10 and you will also need some blocks you will need a comparing blocks 65 00:05:52,899 --> 00:06:04,500 You will need blocks like C, if, and a comparison like higher, and you will also have to check to combine different conditions. 66 00:06:04,920 --> 00:06:13,600 For example, higher than, I mean, lower than 200 and higher than 30. 67 00:06:13,699 --> 00:06:20,339 Remember, you already know how to combine conditions and all of them inside the block if. 68 00:06:20,339 --> 00:06:34,339 So there will be three blocks if, each one with different conditions, for instance, higher than 200, lower than 200 and higher than 100 and lower than 100. 69 00:06:35,199 --> 00:06:38,620 And for each condition, one of the LEDs will be shining. 70 00:06:39,019 --> 00:06:41,939 It will shine for a short period of time. 71 00:06:43,120 --> 00:06:45,000 We will wait for this period of time. 72 00:06:45,100 --> 00:06:45,740 It will be shining. 73 00:06:46,139 --> 00:06:48,100 We will wait and then it will be off again. 74 00:06:48,100 --> 00:06:52,180 OK, so I have described the structure of the program to you. 75 00:06:52,300 --> 00:06:55,339 If you don't understand, just watch the video again. 76 00:06:55,339 --> 00:06:59,139 OK, remember, you have now, you know, everything you need to know 77 00:06:59,139 --> 00:07:00,620 to do the exercise. OK. 78 00:07:00,620 --> 00:07:02,540 And this is the exercise you have to do. 79 00:07:02,540 --> 00:07:05,779 You will have to program something that will behave the same way 80 00:07:05,779 --> 00:07:09,100 my real Arduino board is behaving now. 81 00:07:09,100 --> 00:07:12,100 OK, so the exercise is easy. 82 00:07:12,339 --> 00:07:14,699 Look and watch again the video if you have to. 83 00:07:15,939 --> 00:07:17,420 OK, good luck.