Fun with Arduino 12 Analog Input, analog.Read() - Chane Range, map()

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to video 12 on film with Arduino we have our software user interface for the day/night light module working we can change the cycle time via keyboard input so let's now have a look how we can do this in a different way with hardware with a rotating knob and that rotating knob is placed on a variable resistor a potentiometer like this one if we connect the outer pins of the potentiometer to 0 and 5 volt then the middle pin of this variable resistor will give a variable voltage between 0 and 5 volt let's connect it to the Arduino to one of the analog pins and let's just select pin 8 0 the question is what value does this variable resistor need to have well the Arduino advises not to go beyond 10 K so let's do that let's take 10 K we can go lower but the only thing that happens is that we draw more current world that is not needed so let's try this out first let's have a look at the analog read command as such this very tiny program starts with enabling this serial interface and then in the loop we do nothing more than just read analog pin a0 we don't even store the value we immediately print it on screen and then have a little delay let's have a look what happens if we rotate the knob I have a potentiometer here on my little control panel and the serial monitor is running and tell you it currently reads is 228 29 to 30 so yeah there is a little bit of noise let's rotate the knob fully to the left the minimum value seems to be zero and we also have readings of one or two sorry yeah that noise that's really there rotate fully to the right we can see the numbers clamping and the maximum seems to be one thousand and twenty three and also here we do still have this little noise well that's something we just have to live with okay so we know the readings are between zero and one thousand twenty three how can we make a cycle time of those readings let's say between one and nine as a suggest a first start there is a very useful mathematic command for that and that is called the map command so what we what it does is it takes a value in this case the analog reading on pin a zero which is in a range between zero and one thousand twenty three and it calculates a new range between one and nine this map command does all the mathematics for us it it changes one range of values into another range of values of course this cycle time we need to declare at first so I made an unsigned integer let's have a look at this code all right serial monitor is running the value now is three and there's no noise and I'm going to rotate nope we are at two and one yeah the minimum nicely is one now let's go to the maximum want to see five six seven eight well one tiny little nine I saw their button it stays at eight that's not what we want we want one two nine there's something a little bit off apparently this instruction converts the highest value of the input range in this case 10 23 to the highest value of the output range and 1022 already is 8 and that's not what we want so we have to smuggle a little bit let's say that this becomes 10 and then I should guess that it will work well maybe just add one over you to a non-existing value but then I know for certain that 10 23 is 9 and some values below it are also still 9 let's try this out what serial interface is running again I can change the numbers here with the potentiometer 5 6 7 8 9 this looks good this is what we want okay let's move on now that we have to arrange the map function working let's add this analog read to decode that we had in video 11 what I did over here is at our new pot meter pin called a zero as a define so we can easily change it then I also introduced here a cycle time old we need it because we want to print this cycle time only once when it is changed like we have done the same trick here with the switch remember all right then in the set up we are gone for the first time read in our analog Polk meter because then we already have the value that it is at at the current moment so this statement simply does what we did in the in the previous section it reads it and it maps it to 1 to 10 yeah that it turns out to be 1 to 9 and then we also tell the cycle time old to become the same as the current cycle time so that it does not give printing then in the loop there we have it we had over here at the very top of the loop we had a section that read the keyboard input but now we of course are going to change that with our new cycle time analog read function with the map function in it and then of course if it has changed the only then we do print it so that's actually actually the same as with the keyboard input not too much changes let's have a look how this works ok so here after startup we see that it already has an initial value that is the reading of the button or order the pot meter in the setup section now I can rotate the knob and we see that it changes oops but it still has a bit of suffering from the noise that we saw before just at where it needs to change it starts to print a couple of times yeah because then it cannot decide is it one or two all right but it seems to be working let's switch on in the delight now yeah it has a cycle time and I can change it I do not see it yet that I changed it but I did yeah now I changed it four seconds and it is on the new cycle time so let me switch it off again then how can we get rid of that noise that we still are suffering from a little bit a very simple solution to get rid of that noise is when the cycle time has changed just simply add a tiny little delay 100 milliseconds will do it's short enough not to notice it or to be bothersome and it's long enough to get rid of this noise another change that I like to make is okay we do have an analogue grid that can go to 1023 why do we have the output mapped to one to nine we could make that a bit finer so I changed it over here to one till 30 and then multiplied by 10 what is the resist that now we get as an output range values of 10 20 30 40 seconds and so I can go up to 5 minutes but with 10 second steps so to speak of course I also change that have to change that over here in the first setup section so let me do that times telling that gives me 10 seconds steps let's have a test and see how this is going to work out right system started and lets me rotate that knob so we can see yeah 80 70 60 40 that works perfectly steps of 10 let me switch on the lights well when I now rotate the knob nothing happens and that is the same with the keyboard input the cause of this is that we are using a delay statement to generate our timer and during a delay statement the Arduino cannot do anything so if I would really like to see my user input then you know we have to wait till the cycle is over so which is right now let me see if it goes up to 300 yes it does so in the next video we are going to get rid of this delay statement such that we can continuously see when a user changes an input on the keyboard or on this rotating knob that it is also printed out immediately so you may be back there bye-bye
Info
Channel: Rudysmodelrailway
Views: 9,488
Rating: 4.9365077 out of 5
Keywords: arduino, uno, nano, software, sketch, analog, read, map, tutorial, starter, kit
Id: qWVIsaFukec
Channel Id: undefined
Length: 11min 3sec (663 seconds)
Published: Thu Feb 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.