1 00:00:00,270 --> 00:00:07,440 Hello. In this exercise we're going 2 00:00:07,440 --> 00:00:09,750 to continue studying how the computer can 3 00:00:09,750 --> 00:00:12,360 send and receive information from the arduino 4 00:00:12,360 --> 00:00:14,940 board here on the screen you can 5 00:00:14,940 --> 00:00:16,590 see the circuit we're going to use 6 00:00:16,830 --> 00:00:19,080 we have four LED connected to the 7 00:00:19,080 --> 00:00:20,160 usual connectors 8 00:00:20,280 --> 00:00:23,160 thirteen twelve eleven and ten we are 9 00:00:23,160 --> 00:00:25,830 going to use digital outputs and every 10 00:00:25,830 --> 00:00:27,990 LED is connected to a two hundred 11 00:00:27,990 --> 00:00:32,040 twenty ohms protection resistor so nothing new 12 00:00:32,040 --> 00:00:33,630 for the moment we've done that before 13 00:00:34,920 --> 00:00:36,300 let's go to the code and have 14 00:00:36,300 --> 00:00:36,690 a look 15 00:00:38,490 --> 00:00:43,110 this block here read serial monitor is 16 00:00:43,290 --> 00:00:45,000 a new one this is the purple 17 00:00:45,000 --> 00:00:47,100 block which means it's from the input 18 00:00:47,100 --> 00:00:50,130 library and it means that the arduino 19 00:00:50,130 --> 00:00:52,560 board is reading from the serial connection 20 00:00:52,620 --> 00:00:54,690 from the wire that is connected from 21 00:00:54,690 --> 00:00:56,640 the usb that is connected to the 22 00:00:56,640 --> 00:00:57,210 computer 23 00:00:58,230 --> 00:01:00,540 so we will have a variable that 24 00:01:00,540 --> 00:01:01,230 is called 25 00:01:02,940 --> 00:01:06,150 serial, "serie", because I just want 26 00:01:06,450 --> 00:01:08,310 to choose a clear name so we 27 00:01:08,310 --> 00:01:09,930 take the value or the number from 28 00:01:09,930 --> 00:01:12,240 the usb wire and we store it 29 00:01:12,270 --> 00:01:15,300 inside the variable and now I'm going 30 00:01:15,300 --> 00:01:17,250 to use this variable to control the 31 00:01:17,250 --> 00:01:20,430 arduino board here for instance as an 32 00:01:20,430 --> 00:01:22,560 example if this variable 33 00:01:22,560 --> 00:01:25,830 equals one then thirteen will be high 34 00:01:26,580 --> 00:01:28,080 so it means that the red LED 35 00:01:28,080 --> 00:01:30,480 will shine so let's have a look 36 00:01:30,630 --> 00:01:31,950 and start the simulation 37 00:01:35,160 --> 00:01:37,530 nothing happens while remember that the serial 38 00:01:37,560 --> 00:01:39,930 monitor is down here and to send 39 00:01:39,930 --> 00:01:41,520 variables with a number we have to 40 00:01:41,520 --> 00:01:43,380 type down here in this box 41 00:01:44,430 --> 00:01:46,200 so if I type one and I 42 00:01:46,200 --> 00:01:46,920 press enter 43 00:01:48,150 --> 00:01:51,180 nothing happens because there's a problem 44 00:01:52,470 --> 00:01:54,210 this block is not the right one 45 00:01:54,210 --> 00:01:55,980 if you want to read integral numbers 46 00:01:55,980 --> 00:01:57,480 there's a problem with the 47 00:01:57,480 --> 00:02:00,240 simulation but it's not a big problem 48 00:02:00,600 --> 00:02:02,340 we can solve it by changing from 49 00:02:02,340 --> 00:02:03,810 blocks to text 50 00:02:07,200 --> 00:02:08,880 now notice that when we want to 51 00:02:08,880 --> 00:02:11,070 change from blocks to code we will 52 00:02:11,070 --> 00:02:12,600 not be able to come back to 53 00:02:12,600 --> 00:02:14,850 the blocks so when we change we 54 00:02:14,850 --> 00:02:16,140 just have to be certain that that's 55 00:02:16,140 --> 00:02:18,570 what we want but if it is 56 00:02:18,570 --> 00:02:19,530 we click continue 57 00:02:24,570 --> 00:02:26,340 and then here we will have our 58 00:02:26,340 --> 00:02:26,730 code 59 00:02:31,440 --> 00:02:34,050 so right here this is the function 60 00:02:34,050 --> 00:02:37,350 we are using to read serial equals 61 00:02:37,440 --> 00:02:39,750 this function so here instead of using 62 00:02:40,140 --> 00:02:42,780 serial read we are going to use 63 00:02:42,780 --> 00:02:46,980 a new function and type in it's 64 00:02:46,980 --> 00:02:48,000 called serial 65 00:02:48,270 --> 00:02:49,920 parse int 66 00:02:51,750 --> 00:02:53,700 if you have any problem just typing 67 00:02:53,700 --> 00:02:55,080 in here try it a second or 68 00:02:55,080 --> 00:02:57,150 a third time because sometimes you can't 69 00:02:57,150 --> 00:02:58,950 get it to work the first time 70 00:02:58,980 --> 00:03:00,450 for some reason so keep trying 71 00:03:03,300 --> 00:03:04,980 so here we have the new code 72 00:03:05,160 --> 00:03:07,085 I have only changed the function we 73 00:03:07,197 --> 00:03:09,510 using to read the number and now 74 00:03:09,510 --> 00:03:11,490 I'm going to start the simulation again 75 00:03:13,260 --> 00:03:14,970 and now if I send number one 76 00:03:14,970 --> 00:03:15,720 down here 77 00:03:18,270 --> 00:03:19,320 the LED shines 78 00:03:20,580 --> 00:03:23,820 okay that seems really simple so your 79 00:03:23,820 --> 00:03:26,010 exercise is a little bit more complicated 80 00:03:26,010 --> 00:03:28,830 than this one you will repeat the 81 00:03:28,830 --> 00:03:31,560 code remember you can stop the video 82 00:03:31,560 --> 00:03:33,930 and go back if you need you 83 00:03:33,930 --> 00:03:35,760 will read the variable the same way 84 00:03:35,760 --> 00:03:37,830 and you will add several blocks for 85 00:03:37,830 --> 00:03:40,170 switching all of them on individually 86 00:03:41,130 --> 00:03:42,600 you will have four numbers to switch 87 00:03:42,600 --> 00:03:45,270 them on and all four numbers again 88 00:03:45,270 --> 00:03:46,260 to switch them off 89 00:03:49,590 --> 00:03:52,500 remember that the serial monitor is here 90 00:03:53,880 --> 00:03:54,300 okay 91 00:03:59,070 --> 00:04:01,830 down here so for example you can 92 00:04:01,830 --> 00:04:06,030 use one two three and four to 93 00:04:06,210 --> 00:04:07,140 switch them on 94 00:04:08,190 --> 00:04:10,560 and then maybe you can use five 95 00:04:10,560 --> 00:04:12,720 six seven and eight to turn them 96 00:04:12,720 --> 00:04:15,810 off whatever you want these are the 97 00:04:15,810 --> 00:04:18,000 orders that you're sending through the usb 98 00:04:18,779 --> 00:04:20,850 if you can instead of repeating the 99 00:04:20,850 --> 00:04:23,460 blocks you can just copy and paste 100 00:04:23,520 --> 00:04:25,050 this part here 101 00:04:26,220 --> 00:04:27,270 this is the part that you can 102 00:04:27,270 --> 00:04:30,360 duplicate to reproduce the different possibilities 103 00:04:32,310 --> 00:04:34,500 thirteen is the number of the connector 104 00:04:34,500 --> 00:04:37,080 here instead of high you can use 105 00:04:37,080 --> 00:04:40,830 low and one here means the number 106 00:04:40,830 --> 00:04:42,450 of the code we use to send 107 00:04:42,450 --> 00:04:44,880 information so you can copy and paste 108 00:04:44,880 --> 00:04:45,930 this part of the code if you 109 00:04:45,930 --> 00:04:46,470 want to 110 00:04:47,850 --> 00:04:48,810 you will have to go back to 111 00:04:48,810 --> 00:04:51,000 the blocks program a new circuit and 112 00:04:51,000 --> 00:04:53,490 then create a new code or you 113 00:04:53,490 --> 00:04:55,620 can copy and paste this part however 114 00:04:55,620 --> 00:04:56,550 many times you want 115 00:04:58,230 --> 00:04:59,610 but before I finish I want to 116 00:04:59,610 --> 00:05:01,800 show you the exercise in real life 117 00:05:02,460 --> 00:05:04,530 so here I have copied and pasted 118 00:05:04,680 --> 00:05:06,450 my code and I'm going to upload 119 00:05:06,450 --> 00:05:07,890 it to the computer and check that 120 00:05:07,890 --> 00:05:08,940 everything is correct 121 00:05:10,680 --> 00:05:14,640 so upload okay everything works now I 122 00:05:14,640 --> 00:05:17,430 upload it to the arduino board it's 123 00:05:17,430 --> 00:05:19,350 uploaded and I go to the serial 124 00:05:19,350 --> 00:05:22,260 monitor here so I'm going to send 125 00:05:22,350 --> 00:05:23,580 my commands 126 00:05:24,870 --> 00:05:27,090 the first one is thirteen just cause 127 00:05:27,090 --> 00:05:27,990 I'll start with that one 128 00:05:30,600 --> 00:05:33,060 and the first ltd is shining what 129 00:05:33,060 --> 00:05:34,350 happens if I type twelve 130 00:05:37,380 --> 00:05:39,420 now the green one is shining and 131 00:05:39,420 --> 00:05:40,380 if I type eleven 132 00:05:43,410 --> 00:05:46,230 the yellow one shines now if I 133 00:05:46,230 --> 00:05:48,060 type one hundred and thirteen 134 00:05:51,360 --> 00:05:52,560 the red one turns off 135 00:05:53,850 --> 00:05:55,290 one hundred and twelve 136 00:05:57,360 --> 00:06:00,150 the green ones off and one hundred 137 00:06:00,150 --> 00:06:00,810 and eleven 138 00:06:02,340 --> 00:06:04,500 the yellow one is off so the 139 00:06:04,500 --> 00:06:06,390 interesting thing about this here is that 140 00:06:06,390 --> 00:06:08,460 I can control the arduino board from 141 00:06:08,460 --> 00:06:10,710 my keyboard this is the most amazing 142 00:06:10,710 --> 00:06:12,210 thing the point of this and you 143 00:06:12,210 --> 00:06:13,500 can see that it works in real 144 00:06:13,500 --> 00:06:15,360 life so you will have to do 145 00:06:15,360 --> 00:06:18,510 something like this programming orders to switch 146 00:06:18,510 --> 00:06:20,580 all the LED on and then to 147 00:06:20,580 --> 00:06:21,870 switch all of them off 148 00:06:22,680 --> 00:06:24,600 if you have understood the exercise with 149 00:06:24,600 --> 00:06:26,460 one LED switching it on 150 00:06:26,910 --> 00:06:28,230 then you should be able to have 151 00:06:28,230 --> 00:06:30,690 enough information to replicate and finish the 152 00:06:30,690 --> 00:06:34,080 rest of the exercise okay good luck