1 00:00:00,040 --> 00:00:05,400 Hello! In this exercise we are going to use the same circuit we used in the 2 00:00:05,400 --> 00:00:10,640 previous exercise. You can see that we have the voltage divider connected the 3 00:00:10,640 --> 00:00:19,620 same way we connected it the previous day. We are reading from 5 4 00:00:19,620 --> 00:00:25,600 volts to 0 volts using the A0 analog connector and we have the LED connected 5 00:00:25,600 --> 00:00:29,480 to the Arduino board, the usual way, but there is something different. Instead of 6 00:00:29,480 --> 00:00:37,359 of connecting it to number 13, the LED is connected to number 11. Why number 11? You 7 00:00:37,359 --> 00:00:47,899 can see that numbers 11, 10, 9, 6, 5 and 3 have a line in front of the number. It means 8 00:00:47,899 --> 00:00:54,240 that they are different, these connectors are different. It means that they can provide 9 00:00:54,240 --> 00:01:00,600 also, they can provide, I mean, digital values, the same way we have been doing during the 10 00:01:00,600 --> 00:01:05,980 previous days, high and low digital values, but these six connectors, the one with the 11 00:01:05,980 --> 00:01:11,340 line in front of the number, can also provide analog values. 12 00:01:11,540 --> 00:01:18,859 It means that we can send one volts, two volts, three volts, one and a half, two and a half, 13 00:01:18,859 --> 00:01:24,239 2.40, 3.60, 3.75, whatever. 14 00:01:25,079 --> 00:01:28,819 And if this LED is connected to 11, for instance, 15 00:01:28,819 --> 00:01:34,980 it means that we will be able to provide, I don't know, maybe 240, 320. 16 00:01:35,400 --> 00:01:42,299 It means that we will be able to control the intensity of the LED. 17 00:01:42,640 --> 00:01:47,439 We will be able to decide if the LED will shine brighter or less bright. 18 00:01:47,700 --> 00:01:47,819 Okay? 19 00:01:47,819 --> 00:01:54,819 So, let's have a look at the simulation, I will show the code later to you. 20 00:01:54,819 --> 00:02:04,819 If we are reading 0 using the A0 connector, 11 doesn't send anything to the LED, it doesn't 21 00:02:04,819 --> 00:02:05,819 change. 22 00:02:05,819 --> 00:02:11,340 If we move a little bit higher, it means that we are reading a number higher than 0, as 23 00:02:11,340 --> 00:02:14,099 you can see the LED is shining a little bit. 24 00:02:14,099 --> 00:02:21,860 If we move it and we read a number even higher, the LED will shine brighter, and the higher 25 00:02:21,860 --> 00:02:26,460 the number we read, the brighter it will shine, okay? 26 00:02:26,460 --> 00:02:29,860 This is the brightest value. 27 00:02:29,860 --> 00:02:33,759 We go back to 0, 5, 0, 5. 28 00:02:33,759 --> 00:02:39,840 You can see the ranges, you can see the range of values we are reading and sending to the 29 00:02:39,840 --> 00:02:43,659 LED. 30 00:02:43,659 --> 00:03:01,039 Before we have a look at the code, we need to consider, we need to take into account that A0, although it reads analog values, the Arduino board will work using integer numbers. 31 00:03:01,039 --> 00:03:06,039 You remember that the analog input reads numbers from 0 to 1023. 32 00:03:06,979 --> 00:03:08,599 We studied it in the previous exercise. 33 00:03:09,080 --> 00:03:12,539 It means that we are going to use 1024 values. 34 00:03:12,539 --> 00:03:15,740 number 0 is related to 0 volts 35 00:03:15,740 --> 00:03:18,939 number 1023 is related to 5 volts 36 00:03:18,939 --> 00:03:21,900 and for instance if we have 5012 37 00:03:21,900 --> 00:03:25,139 the one in the middle, we will be reading 38 00:03:25,139 --> 00:03:27,460 or using, reading in this case 39 00:03:27,460 --> 00:03:29,879 2.5 volts, the value in the middle, ok? 40 00:03:30,159 --> 00:03:33,479 there is a proportional relationship between the number we use 41 00:03:33,479 --> 00:03:35,360 and the voltage we are reading 42 00:03:35,360 --> 00:03:39,360 when we use these numbers as 43 00:03:39,360 --> 00:03:41,620 output values, we are going to use 44 00:03:41,620 --> 00:03:51,580 a lower range of values. We are going to use a range of 256 values. We will use numbers 45 00:03:51,580 --> 00:04:00,879 from 0 to 255. 0 will be related to 0 volts, 255 will be related to 5 volts, and the one 46 00:04:00,879 --> 00:04:07,900 in the middle, for instance I think 128, will be related to 2.5. Do you understand the idea? 47 00:04:07,900 --> 00:04:12,180 There is a proportional relationship between the integer number and the voltage. 48 00:04:13,120 --> 00:04:14,500 But it's the same idea. 49 00:04:14,639 --> 00:04:18,000 Although the range is four times shorter, it's the same idea. 50 00:04:19,019 --> 00:04:21,379 So how can we program this circuit? 51 00:04:21,519 --> 00:04:22,360 Really, really easy. 52 00:04:22,439 --> 00:04:23,579 I want to show the code to you. 53 00:04:23,879 --> 00:04:27,399 The first thing we need to do is creating a variable. 54 00:04:27,399 --> 00:04:29,959 In this case, our variable is called input. 55 00:04:30,439 --> 00:04:34,560 We read the analog input with this block, leer pasador analogico, 56 00:04:34,980 --> 00:04:36,459 the one that is connected to a zero, 57 00:04:36,459 --> 00:04:40,459 and we store it in the variable called input. 58 00:04:40,459 --> 00:04:44,459 And then, what do we do? We divide this 59 00:04:44,459 --> 00:04:48,459 variable by 4. Why? Because of the range we are going to send, 60 00:04:48,459 --> 00:04:52,459 we are going to use as output, is 4 times shorter 61 00:04:52,459 --> 00:04:56,459 than the range we are using to read 62 00:04:56,459 --> 00:05:00,459 values. And we use this range to send this 63 00:05:00,459 --> 00:05:04,459 value to 11. 11 is here. It sends the value to the LED 64 00:05:04,459 --> 00:05:07,959 and the LED shines according to the value we are sending. 65 00:05:08,139 --> 00:05:09,399 Zero, it doesn't shine. 66 00:05:10,120 --> 00:05:14,079 Here in the middle, it shines just a little bit, 67 00:05:14,379 --> 00:05:17,160 and if we move here, it shines a lot, okay? 68 00:05:17,860 --> 00:05:19,339 So, this is the circuit. 69 00:05:19,660 --> 00:05:22,319 This is the first thing you have to do, create the circuit. 70 00:05:22,660 --> 00:05:23,779 But this is not the exercise. 71 00:05:23,980 --> 00:05:25,199 This is just the first step. 72 00:05:25,699 --> 00:05:27,660 Your exercise is going to be a little bit different. 73 00:05:28,879 --> 00:05:30,160 Your exercise is here. 74 00:05:30,160 --> 00:05:34,879 is the RGB, we are going to use a new component 75 00:05:34,879 --> 00:05:38,779 that is called the RGB LED, this here 76 00:05:38,779 --> 00:05:44,360 is an LED with 77 00:05:44,360 --> 00:05:48,100 four pins, four connectors. One of them 78 00:05:48,100 --> 00:05:52,480 is called the red connector, one of them is called the blue connector and one of them is called 79 00:05:52,480 --> 00:05:56,379 the green connector. And this one is the cathode and is connected 80 00:05:56,379 --> 00:06:00,399 to the ground through the protection resistor, the one 81 00:06:00,399 --> 00:06:06,399 we usually use with a value of 220 ohms, ok? Don't forget it. 82 00:06:06,399 --> 00:06:11,399 So, it means that this LED will combine three colors, ok? 83 00:06:11,399 --> 00:06:18,399 So, we will read three different values using A0, A1 and A2, ok? 84 00:06:18,399 --> 00:06:25,399 Can you see that we have three voltage dividers connected the way we connect them? 85 00:06:25,399 --> 00:06:28,399 We will use three variables instead of having input. 86 00:06:28,399 --> 00:06:32,519 We will have input 1, input 2 and input 3, for instance. 87 00:06:32,519 --> 00:06:41,680 We will read three values the same way we read them before, and we will send three different 88 00:06:41,680 --> 00:06:45,459 analog outputs the same way we have sent them before. 89 00:06:45,459 --> 00:06:48,819 One for the red, one for the blue, and one for the green. 90 00:06:48,819 --> 00:06:50,939 So let's have a look at the simulation. 91 00:06:50,939 --> 00:06:53,319 You will understand it better with the simulation. 92 00:06:53,319 --> 00:06:56,379 If I move this one, this is the red control. 93 00:06:56,379 --> 00:07:03,680 It means that I'm sending 5 volts, now I'm sending 5 volts through the red wires. 94 00:07:03,680 --> 00:07:09,220 If I connect, if I use the second one to the maximum value, now I'm sending 5 volts using 95 00:07:09,220 --> 00:07:15,660 the blue wire and it will happen something similar with this one. 96 00:07:15,660 --> 00:07:20,959 Now it means that I'm sending 5 volts through the green wire, ok? 97 00:07:20,959 --> 00:07:25,600 So the important thing is that we can combine, for instance, green and blue, and what happens 98 00:07:25,600 --> 00:07:28,500 if we combine green and blue, we have this one that is 99 00:07:28,500 --> 00:07:33,519 a different color. What happens if we combine blue 100 00:07:33,519 --> 00:07:37,639 and red? We have purple, okay? So the idea 101 00:07:37,639 --> 00:07:40,560 is that every color can be 102 00:07:40,560 --> 00:07:45,360 produced using a combination of red, green, and blue. And the way to 103 00:07:45,360 --> 00:07:49,540 I'm not going to show the code to you, but it's really simple. It's something like repeating 104 00:07:49,540 --> 00:07:53,620 three times the code of the previous exercise. Using a variable, reading 105 00:07:53,620 --> 00:08:03,279 it reading a number from the connector, A0, A1, and A2, storing the number in the variable, 106 00:08:03,279 --> 00:08:09,339 dividing the variable by 4, and sending it to the output we want, 11, 10, and 9, okay? 107 00:08:09,339 --> 00:08:14,500 So if you understand the first exercise, and if you have the connections for this one, 108 00:08:14,500 --> 00:08:16,319 the exercise is really, really simple. 109 00:08:16,319 --> 00:08:23,819 This is the exercise, controlling the LED RGB using three voltage dividers, okay? 110 00:08:23,819 --> 00:08:24,819 Good luck.