1 00:00:00,110 --> 00:00:10,029 Hello. Until this moment we have studied digital inputs. Digital means that there are only two possibilities, high and low. 2 00:00:10,289 --> 00:00:21,690 For instance, when we press a button, if we were pressing it, the Arduino board read high or 5 volts. High is the same. 3 00:00:21,829 --> 00:00:27,250 When we were not pressing the button, the Arduino board read 0 or low. It's exactly the same. 4 00:00:27,250 --> 00:00:33,509 The same way, when we had an LED like this one connected to, for example, 13, like this one, 5 00:00:34,109 --> 00:00:40,649 if there was current here, the output was high and the LED was shining. 6 00:00:41,329 --> 00:00:49,189 If there was no output, the LED didn't shine, so it means low, high. 7 00:00:49,869 --> 00:00:53,469 The LED shines low, the LED doesn't shine. 8 00:00:53,469 --> 00:00:59,469 But today we are going to study something a little bit more complicated, just a little bit. 9 00:00:59,469 --> 00:01:07,469 We are going to study analog inputs. Analog means that instead of having 0 or 5, low or high, 10 00:01:07,469 --> 00:01:22,469 we have any voltage from 0 to 5. It means we can have 1, 2, 3, or maybe 1.5, or 2.5, or 2.25, or 3.40, whatever. 11 00:01:22,469 --> 00:01:30,650 every value from 0 to 5 can be read. How can we do that? We are going to use a 12 00:01:30,650 --> 00:01:36,150 voltage divider like this one. The voltage divider you have here in the 13 00:01:36,150 --> 00:01:43,209 picture and also here, it has three terminals. The one on the left, for 14 00:01:43,209 --> 00:01:49,209 instance, will be connected to five volts, okay, through the red wire. The one on the 15 00:01:49,209 --> 00:01:56,049 right will be connected to ground through the black wires, and the one in 16 00:01:56,049 --> 00:02:01,870 the middle is going to be the terminal variable, and it's 17 00:02:01,870 --> 00:02:08,090 going to be connected here, to this terminal called A0. Can you see that here 18 00:02:08,090 --> 00:02:13,710 on the bottom right corner of the Arduino board, can you see that 19 00:02:13,710 --> 00:02:19,949 that there are six analog inputs, analog-in, can you read it? It means that we are going to use these 20 00:02:21,150 --> 00:02:27,069 six connectors to read analog inputs. It means that using these connectors we will be able to 21 00:02:27,069 --> 00:02:35,789 read voltages from zero to five. And we are going to use voltage divider to read them this way. 22 00:02:36,669 --> 00:02:42,830 And we use the connector, the terminal in the middle. And here there is 23 00:02:42,830 --> 00:02:49,969 connected to A0. Okay? So, how does it work? The terminals on the left and on the 24 00:02:49,969 --> 00:02:55,710 right, they don't move. They are fixed. This one, for instance, will have 5, and 25 00:02:55,710 --> 00:03:00,590 this one on the right will have, for instance, 0. The one that moves is 26 00:03:00,590 --> 00:03:06,530 the one in the middle. In real life, we can, well, during the simulation, you 27 00:03:06,530 --> 00:03:11,569 will be able to click and drag, you will be able to move this one in 28 00:03:11,569 --> 00:03:17,569 I'm going to simulate it a little bit, just to show it that if I click and drag, it moves, ok? 29 00:03:17,569 --> 00:03:22,569 Anyway, let's go back to the explanation. This one in the middle will move. 30 00:03:22,569 --> 00:03:28,569 And the closer it is to this one, that is connected to the black wire, 31 00:03:28,569 --> 00:03:36,569 the closer it is to the ground, the closer this variable will be to 0. 32 00:03:36,569 --> 00:03:45,789 And the closer this terminal is to the terminal on the left, the voltage, the closer will be to 5 volts. 33 00:03:46,210 --> 00:03:50,509 So it means that clicking this one and moving, clicking and dragging this one, 34 00:03:50,810 --> 00:03:56,110 we will be sending different voltages to A0, from 0 to 5. 35 00:03:56,509 --> 00:03:56,770 Okay? 36 00:03:58,270 --> 00:04:00,409 So what's the purpose of the exercise? 37 00:04:01,150 --> 00:04:02,629 Let's have a look to the blocks. 38 00:04:02,789 --> 00:04:03,810 Here you have the blocks. 39 00:04:03,810 --> 00:04:04,949 I have already... 40 00:04:04,949 --> 00:04:05,909 Forget about this one. 41 00:04:05,909 --> 00:04:10,289 The last one, you don't need it. I will talk about it later. 42 00:04:10,569 --> 00:04:13,789 Forget about the last one. We are going to read 43 00:04:13,789 --> 00:04:16,810 this A0 connector 44 00:04:16,810 --> 00:04:21,569 using the purple 45 00:04:21,569 --> 00:04:26,329 block, leer pasado analógico A0. You can read from A0 to A5 46 00:04:26,329 --> 00:04:29,970 the six values that are here. Now we are going to read 47 00:04:29,970 --> 00:04:33,589 A0 and we are going to store it here in the input variable. 48 00:04:33,589 --> 00:04:35,709 we have already defined a variable 49 00:04:35,709 --> 00:04:37,230 ok 50 00:04:37,230 --> 00:04:39,009 and 51 00:04:39,009 --> 00:04:42,149 here, these four blocks 52 00:04:42,149 --> 00:04:44,569 I repeat, forget about this one 53 00:04:44,569 --> 00:04:46,629 these four blocks, what are they? 54 00:04:47,689 --> 00:04:48,269 I'm sorry 55 00:04:48,269 --> 00:04:50,769 these four blocks, what are they? 56 00:04:50,889 --> 00:04:52,949 I'm sure you can identify them as a blink 57 00:04:52,949 --> 00:04:54,389 13 high 58 00:04:54,389 --> 00:04:56,329 we wait, 13 low 59 00:04:56,329 --> 00:04:58,069 we wait, it's a blink 60 00:04:58,069 --> 00:04:58,889 ok 61 00:04:58,889 --> 00:05:01,889 but instead of having 62 00:05:01,889 --> 00:05:13,050 a fixed period of time we are using the variable we have read as a blinking 63 00:05:13,050 --> 00:05:20,910 period of time because the input doesn't read numbers from 0 to 5 volts. Obviously 64 00:05:20,910 --> 00:05:27,649 we are reading values from 0 to 5 volts but they will be translated from 0 to 65 00:05:27,649 --> 00:05:34,610 1023. Let's have a look at the simulation. I'm going to add this last block just to 66 00:05:34,610 --> 00:05:40,009 show these values here. I'm going to start the simulation. Can you see that 67 00:05:40,009 --> 00:05:51,550 the pointer is here? Now we are reading 348. If I move it closer to the right, 68 00:05:51,550 --> 00:05:56,589 when I'm touching the right terminal, we are reading 0. If I move a 69 00:05:56,589 --> 00:06:04,329 little bit higher, we will be reading 82. If I move a little bit higher, we will be 70 00:06:04,329 --> 00:06:12,930 reading 225. If I move a little bit closer to the red one, now we are reading 71 00:06:12,930 --> 00:06:21,009 614. And if I move a little bit closer, we are reading, let's wait a little bit, 72 00:06:21,009 --> 00:06:29,129 921. It means that we are reading values, but these values are from, instead of 73 00:06:29,129 --> 00:06:36,829 being from 0 to 5, they are read from 0 to 1023, okay? There's a 74 00:06:36,829 --> 00:06:42,250 proportional relationship, okay? This is because Arduino board uses 75 00:06:42,250 --> 00:06:47,529 integer numbers, that's all. So the idea is really simple. We are going, forget 76 00:06:47,529 --> 00:06:51,550 about this last block, I repeat it again. We are going to read a zero, we are going 77 00:06:51,550 --> 00:06:56,870 to start to store it in this variable and we are using the 78 00:06:56,870 --> 00:07:03,310 variable multiplied by two as a blinking period of time for the LED. The LED is 79 00:07:03,310 --> 00:07:08,889 connected the usual way. The LED here connected to 13 for instance, the ground 80 00:07:08,889 --> 00:07:14,509 connection here using black wires and the protection resistor, remember 220 81 00:07:14,509 --> 00:07:34,350 So, now I repeat, I'm going to forget out this block, now we are not going to use it anymore, I can erase it, and the idea is that this way we are controlling the period of time of the LED just using this analog input. 82 00:07:34,350 --> 00:07:51,850 Let's start the simulation again, and you will see that the closer this connector is to zero, the shorter the period of time will be, and it means the shorter the blinking period of time will be, the faster it will blink. 83 00:07:52,329 --> 00:07:59,290 If the number is higher, the period of time will be higher, and the blinking will be slower. 84 00:07:59,730 --> 00:08:00,050 Okay? 85 00:08:00,329 --> 00:08:00,850 Do you understand? 86 00:08:00,850 --> 00:08:03,029 So this is your first exercise. 87 00:08:03,029 --> 00:08:12,329 reproduce this circuit and program it. If you program it, you will have half of 88 00:08:12,329 --> 00:08:18,269 the mark. What do you want, what do you have to do to to have the second, I will 89 00:08:18,269 --> 00:08:22,829 stop the simulation, what do you need to do to have the second part of 90 00:08:22,829 --> 00:08:29,670 the mark, the second half of the mark? You will have to use a second 91 00:08:29,670 --> 00:08:34,909 potentiometer, here for instance, you will connect it the same way it has been 92 00:08:34,909 --> 00:08:41,830 connected, through a red wire to five volts. Maybe you will have to connect it 93 00:08:41,830 --> 00:08:47,429 in a different way, five volts here, a red wire here, I'm sorry, red wire here, 94 00:08:47,429 --> 00:08:54,429 here a red wire connected here, a black wire connected here, I'm sorry, the red 95 00:08:54,429 --> 00:09:06,009 one connected here and here, not black, red, the black wire here, a second 96 00:09:06,009 --> 00:09:11,610 connection to A1, and you will repeat this connection with a second LED 97 00:09:11,610 --> 00:09:17,649 connected using a second resistor, protection resistor, I'm sorry, and 98 00:09:17,649 --> 00:09:25,009 this one connected to 12, okay? Do you understand the idea? The same but complete 99 00:09:25,009 --> 00:09:30,669 the connection, I repeat, a second LED here, a second 220 ohms 100 00:09:30,669 --> 00:09:39,190 resistor here, and the wires. So you will use two LEDs and two voltage dividers 101 00:09:39,190 --> 00:09:44,490 and you will program it in a different way. Instead of controlling the blinking 102 00:09:44,490 --> 00:09:51,250 time, forget about it, you will use the control block, you will have two 103 00:09:51,250 --> 00:09:58,610 variables, input1 and input2 for instance, for instance input1, input1 104 00:09:58,610 --> 00:10:09,990 and a second one input2, and you will read them the same way we read the first 105 00:10:09,990 --> 00:10:20,129 one, the same with the second one, you need a second variable, that will be 106 00:10:20,129 --> 00:10:27,090 called input two, and you will read a one instead of a zero, okay? And here you will 107 00:10:27,090 --> 00:10:33,570 compare if, you know the if block, if, and you know how to compare, if, you know, 108 00:10:33,570 --> 00:10:41,850 higher, lower. If input 1 is higher than input 2, one of the LEDs will blink. If 109 00:10:41,850 --> 00:10:47,429 the other one is higher than the first one, it means the opposite condition, the 110 00:10:47,429 --> 00:10:53,250 the other LED will blink. So something like that. I'm not going to complete the 111 00:10:53,250 --> 00:10:59,429 program, you will complete it by yourselves. So if the first condition is 112 00:10:59,429 --> 00:11:07,289 true. If, for instance, input 0 is lower than input 1, for instance, 13 will shine. 113 00:11:07,289 --> 00:11:16,470 If 13 is higher than 12, 12 will shine. I don't know, I'm sorry, if A1 is higher 114 00:11:16,470 --> 00:11:22,529 than A0, I don't know, 12 will shine. Do you understand the idea? Here you will compare 115 00:11:22,529 --> 00:11:27,870 the two variables, input 1, input 2, input 1, input 2, and here you will decide 116 00:11:27,870 --> 00:11:34,230 which one is going to shine. Maybe number 13 here, maybe number 12 here. So you will 117 00:11:34,230 --> 00:11:38,389 complete this code. This code is incomplete. Don't try to simulate it. If 118 00:11:38,389 --> 00:11:42,210 you try to simulate it, there will be mistakes. Well, there are no mistakes but 119 00:11:42,210 --> 00:11:48,110 it doesn't work, okay? Nothing happens because it's not properly programmed, 120 00:11:48,110 --> 00:11:51,789 okay? So you will have to complete it. This is the second part of the exercise, 121 00:11:51,789 --> 00:11:57,149 okay? Okay, I think everything is clear. I think you have more than enough 122 00:11:57,149 --> 00:12:03,389 information to do the exercise, so just do it. Good luck!