1 00:00:01,350 --> 00:00:04,290 Hello, In this exercise we are going 2 00:00:04,290 --> 00:00:06,390 to learn how to use a distance 3 00:00:06,420 --> 00:00:08,460 sensor so you will be able to 4 00:00:08,460 --> 00:00:12,030 find it in the basic library just 5 00:00:12,030 --> 00:00:15,390 scroll down and it's right here click 6 00:00:15,420 --> 00:00:17,520 and drag it and connect it to 7 00:00:17,520 --> 00:00:19,260 the arduino board this way 8 00:00:20,190 --> 00:00:21,210 I'm going to zoom in a little 9 00:00:21,210 --> 00:00:21,390 bit 10 00:00:22,560 --> 00:00:24,600 you can see the ground connection from 11 00:00:24,600 --> 00:00:26,130 the sensor to the arduino board 12 00:00:27,570 --> 00:00:30,210 and a five volts connection connected to 13 00:00:30,210 --> 00:00:32,159 the five volts of the board 14 00:00:33,750 --> 00:00:36,630 and there is also a control wire 15 00:00:36,630 --> 00:00:39,390 or control connector that we are connecting 16 00:00:39,390 --> 00:00:41,970 to number thirteen so this is how 17 00:00:41,970 --> 00:00:43,200 we connect the sensor 18 00:00:44,550 --> 00:00:47,430 now how do we program it so 19 00:00:47,430 --> 00:00:49,080 if we open the code 20 00:00:51,630 --> 00:00:53,790 I'm going to just focus first on 21 00:00:53,790 --> 00:00:55,410 the first block that is here that 22 00:00:55,410 --> 00:00:57,240 we see we're going to use a 23 00:00:57,240 --> 00:01:00,810 variable that is called distance and we're 24 00:01:00,810 --> 00:01:03,630 going to use the block to define 25 00:01:04,560 --> 00:01:05,670 this is the way that we take 26 00:01:05,670 --> 00:01:07,440 a value and we store it inside 27 00:01:07,470 --> 00:01:10,320 the variable and inside is the per 28 00:01:10,320 --> 00:01:13,410 purple block that says read the distance 29 00:01:13,410 --> 00:01:17,550 sensor etc etc it's a very long 30 00:01:17,550 --> 00:01:20,400 name but it basically means that the 31 00:01:20,400 --> 00:01:22,406 sensor is going to be connected to 32 00:01:22,440 --> 00:01:23,280 number thirteen 33 00:01:24,600 --> 00:01:25,650 so if I scroll over 34 00:01:29,100 --> 00:01:30,450 this section you do not need to 35 00:01:30,450 --> 00:01:31,890 change keep it as is 36 00:01:33,480 --> 00:01:34,770 and we are going to measure the 37 00:01:34,770 --> 00:01:36,660 distance in centimeters 38 00:01:42,750 --> 00:01:46,710 so centimeters is there and we use 39 00:01:46,710 --> 00:01:48,990 this purple block to read a number 40 00:01:49,020 --> 00:01:51,210 and store it inside this variable 41 00:01:53,280 --> 00:01:55,500 now with this second block as you 42 00:01:55,500 --> 00:01:57,720 already know we are going to send 43 00:01:57,780 --> 00:01:59,580 the value to the serial monitor 44 00:02:01,350 --> 00:02:03,420 down here okay so let's look at 45 00:02:03,420 --> 00:02:04,170 the simulation 46 00:02:06,930 --> 00:02:08,100 and you can see how it will 47 00:02:08,100 --> 00:02:08,460 work 48 00:02:09,990 --> 00:02:11,400 we will read the distance and we 49 00:02:11,400 --> 00:02:12,960 will send it to the arduino board 50 00:02:12,990 --> 00:02:14,190 and we'll be able to see it 51 00:02:14,190 --> 00:02:15,480 using the serial monitor 52 00:02:17,280 --> 00:02:18,090 so let's start 53 00:02:20,010 --> 00:02:21,150 I'm going to just close the code 54 00:02:21,300 --> 00:02:23,040 to make it easier to see fully 55 00:02:24,150 --> 00:02:25,410 and then zoom out a little bit 56 00:02:26,160 --> 00:02:28,230 and nothing happens at first but if 57 00:02:28,230 --> 00:02:29,880 I click here on the monitor 58 00:02:31,980 --> 00:02:33,990 this new or on the sensor sorry 59 00:02:33,990 --> 00:02:36,210 this new part will appear so this 60 00:02:36,210 --> 00:02:38,910 ball is simulating a kind of object 61 00:02:38,940 --> 00:02:39,960 close to the sensor 62 00:02:41,100 --> 00:02:43,080 and if I open the serial monitor 63 00:02:44,190 --> 00:02:46,740 you will be able to see that 64 00:02:46,770 --> 00:02:49,830 here the distance is three hundred and 65 00:02:49,830 --> 00:02:51,270 three centimeters 66 00:02:54,750 --> 00:02:57,270 and we're reading three hundred three centimeters 67 00:02:58,020 --> 00:02:59,850 if I move the ball closer the 68 00:02:59,850 --> 00:03:01,830 distance is one hundred and ninety two 69 00:03:01,830 --> 00:03:04,410 centimeters one hundred and ninety two and 70 00:03:04,800 --> 00:03:07,920 closer again sixty three and we're reading 71 00:03:07,920 --> 00:03:10,860 sixty three centimeters okay so it means 72 00:03:10,860 --> 00:03:12,477 that the simulation works which 73 00:03:12,540 --> 00:03:12,930 great 74 00:03:14,190 --> 00:03:16,350 but also I will say that it's 75 00:03:16,350 --> 00:03:19,290 not necessarily easy to understand how this 76 00:03:19,290 --> 00:03:21,720 works in real life so I'm going 77 00:03:21,720 --> 00:03:23,580 to show you a real distance sensor 78 00:03:23,610 --> 00:03:25,320 measuring in real life 79 00:03:28,500 --> 00:03:29,910 so we can stop the stimulation 80 00:03:33,420 --> 00:03:34,980 and here I have it set up 81 00:03:35,880 --> 00:03:38,580 so I've created this code and uploaded 82 00:03:38,580 --> 00:03:40,860 it to the arduino board and now 83 00:03:40,860 --> 00:03:42,360 the board is already working 84 00:03:46,200 --> 00:03:47,760 I'm going to use this book as 85 00:03:47,760 --> 00:03:50,160 an object for example and the object 86 00:03:50,160 --> 00:03:51,090 is far away 87 00:03:53,220 --> 00:03:54,870 and there's a green light can you 88 00:03:54,870 --> 00:03:57,000 see the green light as the object 89 00:03:57,000 --> 00:03:58,860 moves closer we see the yellow light 90 00:03:59,820 --> 00:04:02,100 and if the object is even closer 91 00:04:03,120 --> 00:04:04,230 now the red light is shining 92 00:04:05,310 --> 00:04:07,740 okay so we're measuring distances and the 93 00:04:07,740 --> 00:04:10,290 arduino board is behaving according to the 94 00:04:10,290 --> 00:04:13,050 distance that the the sensors reading 95 00:04:14,670 --> 00:04:16,380 so this is the exercise that you 96 00:04:16,380 --> 00:04:19,079 will have to program using the simulation 97 00:04:19,079 --> 00:04:22,110 of course not the program and you 98 00:04:22,110 --> 00:04:23,340 see that it works in real life 99 00:04:23,340 --> 00:04:23,730 ok 100 00:04:25,080 --> 00:04:26,910 so let's go back to our simulation 101 00:04:29,310 --> 00:04:31,740 you have everything you need here you 102 00:04:31,740 --> 00:04:34,650 have the sensor and when you're simulating 103 00:04:34,650 --> 00:04:36,180 you can click on it remember if 104 00:04:36,180 --> 00:04:38,670 you are simulating you can click and 105 00:04:38,670 --> 00:04:40,590 you'll have a virtual object to move 106 00:04:42,120 --> 00:04:43,350 and then you have these pieces of 107 00:04:43,350 --> 00:04:45,990 code these blocks we are reading using 108 00:04:45,990 --> 00:04:48,180 the purple block and storing it inside 109 00:04:48,180 --> 00:04:50,400 a variable and you will use this 110 00:04:50,400 --> 00:04:52,440 variable to check the conditions 111 00:04:53,970 --> 00:04:58,050 so if the distances for instance I 112 00:04:59,160 --> 00:05:01,080 dunno let's let's look before we add 113 00:05:01,080 --> 00:05:02,970 any code if the distance is for 114 00:05:02,970 --> 00:05:07,230 instance higher than two hundred centimeters then 115 00:05:07,440 --> 00:05:09,870 the green LED will be shining if 116 00:05:09,870 --> 00:05:12,570 the distances between two hundred and one 117 00:05:12,570 --> 00:05:13,680 hundred for instance 118 00:05:13,680 --> 00:05:13,890 it's 119 00:05:16,140 --> 00:05:18,030 then the yellow LED will be shining 120 00:05:18,600 --> 00:05:20,460 and if it's lower than one hundred 121 00:05:20,460 --> 00:05:23,580 centimeters for example then the red LED 122 00:05:23,580 --> 00:05:24,240 will be shining 123 00:05:26,010 --> 00:05:27,810 so what do you need well of 124 00:05:27,810 --> 00:05:30,270 course you need three LED connected 125 00:05:30,270 --> 00:05:33,510 here with protection resistors like always 126 00:05:35,010 --> 00:05:36,240 connected to 127 00:05:38,850 --> 00:05:41,040 for instance twelve eleven ten 128 00:05:42,300 --> 00:05:44,760 connectors and you will also need some 129 00:05:44,820 --> 00:05:48,630 comparing blocks for conditions that if then 130 00:05:48,660 --> 00:05:49,260 blocks 131 00:05:52,530 --> 00:05:53,340 let's find that here 132 00:05:54,960 --> 00:05:56,700 and you will also have to combine 133 00:05:56,730 --> 00:06:01,080 different conditions such as lower than two 134 00:06:01,080 --> 00:06:03,330 hundred and higher than thirty just for 135 00:06:03,330 --> 00:06:06,270 instance so remember you already know how 136 00:06:06,270 --> 00:06:08,280 to combine conditions and all of them 137 00:06:08,280 --> 00:06:10,950 inside the block that says if then 138 00:06:12,270 --> 00:06:13,890 so there will be three blocks of 139 00:06:13,890 --> 00:06:15,750 if and each one will have a 140 00:06:15,750 --> 00:06:19,410 different condition for instance higher than two 141 00:06:19,410 --> 00:06:22,170 hundred lower than two hundred and higher 142 00:06:22,170 --> 00:06:24,810 than one hundred and lower than one 143 00:06:24,810 --> 00:06:28,230 hundred centimeters and for each condition one 144 00:06:28,230 --> 00:06:30,180 of the LED these will be shining 145 00:06:30,180 --> 00:06:31,530 it will shine for a short period 146 00:06:31,530 --> 00:06:31,980 of time 147 00:06:33,150 --> 00:06:34,860 so we will wait for this period 148 00:06:34,860 --> 00:06:35,970 of time while it's shining 149 00:06:38,100 --> 00:06:39,750 and then it will be off again 150 00:06:43,860 --> 00:06:46,080 okay so I have described the structure 151 00:06:46,080 --> 00:06:48,240 of the program to you if you 152 00:06:48,240 --> 00:06:50,130 don't understand it you can just watch 153 00:06:50,130 --> 00:06:51,240 the video again 154 00:06:52,530 --> 00:06:53,880 I think you now have everything you 155 00:06:53,880 --> 00:06:55,260 need to know how to complete the 156 00:06:55,260 --> 00:06:56,100 exercise 157 00:06:57,360 --> 00:06:59,940 so you will have to program something 158 00:06:59,940 --> 00:07:01,770 that will behave the same way the 159 00:07:01,770 --> 00:07:04,980 real arduino board is behaving now with 160 00:07:05,820 --> 00:07:08,850 the object in different conditions of how 161 00:07:08,850 --> 00:07:10,380 far away it is to the sensor 162 00:07:10,740 --> 00:07:13,650 in centimeters so try this and watch 163 00:07:13,650 --> 00:07:15,330 the video again if you need to 164 00:07:16,050 --> 00:07:16,530 good luck