Creating void functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay welcome back so right now what I want to do is show you how to do something like just create a simple function all right right now we have put everything in the loop which is fine but ultimately as you as your program grows and grows and grows this is going to become cumbersome and difficult to follow so it's always a good idea to create functions that you can call upon within your loop so to do that what we need to do in this case I want to create another void function it's not going to return a value later on I'll show you how to do a non-void function a non-void function will actually return a value so for example if I wanted to change the analog radian of my sensor into for example distance I might want to use a non-void function so I can call upon it with any one of my sensors and it'll convert it into actual distance which might be easier for you to use all the functions have basically the same structure so what I'm gonna do is actually I'm just gonna copy this set up one because I have a hard time packing and I may get rid of all the junk inside and let's call this one read sensors all right and I'm gonna take this little chunk of code right here which is our reading of the sensors I'm gonna paste it right there I'm gonna take this the name of it put it right there my semicolon I'm gonna use ctrl T to clean up my code and so now in the loop the first thing it does is read sensors and it jumps up to the read sensors function and it collects the data into the two variables by reading the pins a 0 and a 9 so what we should see is actually no difference in the output we'll just you cleaner code and lo and behold everything works perfect let's create another one this one actually will be handy I'm going to do a print sensors and I'll say it's handy because you'll see that printing is pretty slow so when it comes to robot operation if I can I might want to use the print commands or the print function while doing Diagnostics but there's really no reason to print anything while my robot is actually out in the field someplace because it won't be anybody to listen so it makes it super easy to turn on and off the print so if I go ahead and take this now that whole block alright so now you can see that our loop has been reduced down to essentially two functional lines and I'll load it up we shouldn't see any difference in the operation okay same exact operation but we've reduced our code made it a little cleaner a little easier and more importantly if I want to make my code go quickly because I've done my Diagnostics I've got everything set up no I have to do is by the way I just hit ctrl /to do that with a keystroke I just ran out comment out this line and of course you won't see anything happening over here because it's not going to be printing but it's just gonna be reading as fast as it possibly can without the delay of printing I'm gonna go ahead and call that back and I'm gonna recompile and once again we're back I hope they're yeah we're back in operation everything is exactly what it was but it's a lot cleaner so I will see you guys again shortly with the next video
Info
Channel: Brian Patton
Views: 20,855
Rating: 4.9746032 out of 5
Keywords: pattonrobotics.com, Patton Robotics, robot, education, online classes, virtual classroom, Arduino, electronics, Programming, tutorial, Robot Kit, 3D print, Teensy
Id: n4K0suUkfx0
Channel Id: undefined
Length: 4min 34sec (274 seconds)
Published: Thu Feb 25 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.