Arduino Workshop - Chapter Three - Creating Functions

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 มี.ค. 2017
  • The full Arduino Workshop in step-by-step format can be found here core-electronics.com.au/tutor...
    In this section, we'll be learning about using and creating your own functions to write modular, reusable code.
    We're official retailers for Arduino in Australia - powered by makers, for makers! core-electronics.com.au/ardui...

ความคิดเห็น • 58

  • @Adrian-yz7oe
    @Adrian-yz7oe 2 ปีที่แล้ว +4

    I'm freaking loving this series!!!

  • @skeeveskeeve
    @skeeveskeeve 5 ปีที่แล้ว +13

    This whole series is super-helpful.

  • @chetronics
    @chetronics 2 ปีที่แล้ว +5

    Excellent information, and well structured! You are a great teacher.

  • @bunberrier
    @bunberrier 5 ปีที่แล้ว +2

    Excellent video, I am learning a lot from you. Thank you!!!!!!!!!

  • @lexasolopow
    @lexasolopow ปีที่แล้ว +1

    Quite combined lecture. If I get this I would be overwhelmed

  • @bryananthony3924
    @bryananthony3924 2 ปีที่แล้ว

    Thank for the help !, it was super easy to understand .

  • @Sbruce8887
    @Sbruce8887 7 ปีที่แล้ว +1

    Keep up the awesome work

  • @michaelcostello6991
    @michaelcostello6991 7 ปีที่แล้ว +10

    Good video. Thank you very much. Just one thing was a bit confusing. You jump very quickly between the code lines . Thanks again

  • @Didanihaaaa
    @Didanihaaaa 4 ปีที่แล้ว +1

    great tutorial.

  • @gilberthatangimna2591
    @gilberthatangimna2591 ปีที่แล้ว

    Thank you for your help

  • @danthemancasey
    @danthemancasey 6 ปีที่แล้ว +3

    Thank you for another informative video! Would like to ask why it is that you use the function "readSerial"? how can it be used instead of "Serial.read " , or where does "Serial.parseint" return a value for "readSerial"? I seem to be overlooking something... Otherwise thank you again for all your videos!

    • @rocketjumper2114
      @rocketjumper2114 4 ปีที่แล้ว

      readSerial is a class he made, not a function

    • @rish4883
      @rish4883 3 ปีที่แล้ว +2

      readSerial is a user defined function, which he created to read the input

  • @gfodale
    @gfodale 6 ปีที่แล้ว +8

    ~ 7:17 Why did you use Serial.parseInt() instead of Serial.flush() to clear the buffer?
    ~ 7:55 you have an int return type on your findSide() function, but you do not return anything... Return an int, or void the return type....

    • @rish4883
      @rish4883 3 ปีที่แล้ว +2

      he also had a result variable declared, could have returned the hypotenuse value and assigned it to the result variable

  • @aeromancystudios1295
    @aeromancystudios1295 4 ปีที่แล้ว +6

    Swamp introduction to functions! How's about you start with calling a function without variables first to light up an LED in the loop instead of adding special lines for parsing of serial monitor and scrolling up and down the sketch using math outputs like we are time travellers!

  • @drakethomas652
    @drakethomas652 4 ปีที่แล้ว +3

    I’ve noticed that you can’t have two “void loops” in one program, but I wanna know if I can still loop a program, but instead, call it “void temp”

  • @brucewoods9377
    @brucewoods9377 4 ปีที่แล้ว +5

    The way I see it. These “functions” are similar to BASIC Sub-Routines

  • @unicycleboy2
    @unicycleboy2 6 ปีที่แล้ว +5

    Why do you define "float result ; " in the beginning but not use it anywhere in the sketch?

    • @danthemancasey
      @danthemancasey 6 ปีที่แล้ว +2

      Lol... yeah seems to be a forgotten variable, of which the float "hypotenuse" displaced!

    • @rish4883
      @rish4883 3 ปีที่แล้ว

      he could have returned a float value in the findside function and assigned it to result variable

  • @albert4866
    @albert4866 4 ปีที่แล้ว

    Warum heißt das Programm"button-led-test"?

  • @biswajitpatra6993
    @biswajitpatra6993 2 ปีที่แล้ว +1

    you used int findside function but even if your function return type is in int you didn't used return in that function body ??

  • @vampyceiling6678
    @vampyceiling6678 3 ปีที่แล้ว

    Why do it 'readSerial' was not declared in this scope. I need help please, I copy the code but that happen

  • @noodler911
    @noodler911 4 ปีที่แล้ว +2

    How is it possible to create a function after the function is being called ?

    • @rish4883
      @rish4883 3 ปีที่แล้ว +1

      I guess it doesn't need to be defined before you call unless you are assigning it to a variable.
      in c++ ik that you can declare the function on top and the later define it later in the program, butnot sure how calling before defining/decalring works

  • @moh13666
    @moh13666 2 ปีที่แล้ว +2

    thank you for the lessons ..but math is not boring . math is The Best (:

  • @wishicouldarduino8880
    @wishicouldarduino8880 3 ปีที่แล้ว

    // motor runs forward at 50 percent speed what is something like this called?

  • @saadasonic2000
    @saadasonic2000 2 ปีที่แล้ว

    Nicee

  • @yamrajoli3834
    @yamrajoli3834 2 ปีที่แล้ว

    hello bro I need your help in the arduino program I have code but in this there is something that I did not understand
    can you please help me ?

  • @mohamadilhamsyafrizal5111
    @mohamadilhamsyafrizal5111 2 ปีที่แล้ว

    I want to ask, what if there are 2 sensors, if sensor 1 is on then sensor 2 must be off, how?

  • @christianlycheknudsen1735
    @christianlycheknudsen1735 3 ปีที่แล้ว +5

    Yo, nice vid, but pls get rid of the music. Its just annoying background noise that makes it harder to focus on what ur saying

  • @AzeezMuhammed-ro1ru
    @AzeezMuhammed-ro1ru 5 หลายเดือนก่อน

    Pls I don't really understand how u created the read serial function .I thought to create a function we use void +parameter for example why can't u say void readserial

  • @Wanderlust1342
    @Wanderlust1342 7 ปีที่แล้ว +10

    You jump very quickly between the code lines

  • @Javaman92
    @Javaman92 2 ปีที่แล้ว +1

    Could anyone explain this code?
    if(i < 1 || (i%1 != 0))
    I get less than one but, i remainder 1 is not equal to 0? Is that correct? What does that mean?

    • @jettanat1351
      @jettanat1351 2 ปีที่แล้ว +1

      it means i/1 need to have remainder =0 (for i to be an integer)

  • @hungryman54321
    @hungryman54321 4 ปีที่แล้ว

    Did you actually create a function here? ParseInt etc already exists doesn't it?

  • @KJ7JHN
    @KJ7JHN 2 ปีที่แล้ว

    could you please point me to a lerp function for arduino?? thanks.

    • @Core-Electronics
      @Core-Electronics  2 ปีที่แล้ว

      lerp() is implemented in Arduino C as map()

  • @grantscott1686
    @grantscott1686 3 ปีที่แล้ว +1

    something i've never understood and still don't is, what does the return type do specifically? Everyone in tutorials just say that void returns nothing and int returns int, etc. But what does that mean? Where is it returning to? I can't seem to understand what everyone's talking about when they discuss functions.

    • @HardwareUnknown
      @HardwareUnknown 3 ปีที่แล้ว +1

      Grant, take my words with a grain of salt as I'm not an Arduino expert, but here's how I think of it:
      In languages like Python, you can declare a variable such as X. You can set X to be an integer such as 3, a string such as "Hello", a list of items, etc. You can change the value of X to be any other type by re-assigning it, ie. X = 3, then writing X = "fish". That reassigns the value of X from 3 to "fish" AND the type of X from int to string, because the data type is inherent to the assigned value of 3 (int) or "fish (string), not to the variable X. X is just a container, so it takes on the type of whatever value it has been assigned.
      In Arduino, it's a bit different. Variables in Arduino have pre-assigned types (which you yourself set when you declare the variable) that essentially don't get changed. You declare 'int X = 3', thereby saying that X is type "int", and has a value of "3". So "int returns int" is saying that "by declaring the variable X to be of type 'int', you will get an integer back when you call X." This is simply a convention that Arduino requires. You can't just say "X = 3" without first assigning it the type "int". In Python you can do that, just two different programming languages' ways of going about things.
      It seems redundant to me to, and it confused me when I switched over from basic Python coding. Why do I need to manually assign a type to X? Why can't it just take on the type of the data I assign to it? Who knows... guess that is just how Arduino was coded in the first place, so it's a quirk I figured I may as well get used to. I know there's a reason the developers did that, I'm just not yet knowledgeable enough to know why. My takeaway is "Python variables can change types, Arduino variables must have formally declared and pre-assigned types."
      Now, onto "return". I can only speak to Python, once again, but I believe Arduino works similarly in the case of return.
      Let's say you write a function "readVibrationSensor". It gathers the data from a vibration sensor and then you "return" some value called "vibration_strength", ie. the strength of the vibration detected by the sensor. Later on you can assign the result of the function to some variable and then use that later. In other words, you want to do something with the data that the function collects, so at some point after writing the function, you call it. You might do that like this:
      long measurement = readVibrationSensor();
      What that line says is "assign the variable called 'measurement' the type 'long' (a long integer) and the value of whatever the function "readVibrationSensor()" gives us. The thing that "readVibrationSensor()" gives us is what you chose to "return" at the end of the readVibrationSensor() function. So, if the actual physical vibration sensor itself is able to return a range of values from 0 to 100, then during the readVibrationSensor() function call, the value "vibration_strength" might be assigned to 57. Once readVibrationSensor() runs through all its code, you then would "return" the variable "vibration_strength" using the statement above, which will assign the value of "measurement" to whatever the value of "vibration_strength" is.
      The thing to remember is that "vibration_strength" is only a variable INSIDE the function readVibrationSensor(). Outside of the function, we need to assign it another variable. You might have read about variable "scope". This is what it's referring to. The variable vibration_strength is a local variable inside readVibrationSensor(), so it can only be used inside that function UNLESS you return it, which "passes" that variable outside of the function to something else. At the end of the function call, that local variable is effectively destroyed. So in the end, you ran the readVibrationSensor() function and it assigned the local variable vibration_strength value to 57. You then return that value out of the function and assign it the new variable "measurement". Now measurement = 57, the function is totally complete, "vibration_strength" is destroyed, and "measurement" lives on in your program outside of the readVibrationSensor() function, now to serve a useful purpose somewhere else. Next time you run readVibrationSensor(), you'll get a new reading from the sensor, and vibration_strength will be assigned to that new value, which you could then return to the variable "measurement_2", as an example. That way you could get multiple readings over time.
      That's why it gets destroyed at the end, because it needs to become something new the next time you run the associated function. "Return" allows you to store those values for later.

    • @grantscott1686
      @grantscott1686 3 ปีที่แล้ว

      Hardware Unknown that makes sense. But how does the return type differ from the parameters/arguments? Don’t they essentially do the same thing? Like if I were to write a function called: bool isRunning(){} and one called: void isRunning(bool on){}. Would that not do the same thing? It’s very confusing lol.

    • @HardwareUnknown
      @HardwareUnknown 3 ปีที่แล้ว

      @@grantscott1686 It totally is, and it confuses me too. I assume we're not alone though, that nearly all new coders go through this. So hey, good news there!
      I believe that the wording "return type" is a bit of a misnomer here. We're thinking of it as "a separate data category (type) called 'return', similar to int, string, bool, etc." I think a more accurate phrasing would be "tell a function to run the return statement so we receive back a value... assign the type of THAT value using a return type." The concept of a return type sounds like it refers to a new data type, but in reality it's a descriptor. In shorter words, return type = "the type of the value that is returned".
      A return statement is simply that, a statement/command saying give us back the value we told the function to spit out. It's the computer verbiage needed to make that request, not actually a data type in and of itself, and that makes sense as the data type in question is whatever the function gives back. That could be a string ("hello"), an int (2), a bool (True), etc. The choice of words being so similar to the concept of an "int type", "string type", etc. is an unfortunate coincidence.
      This becomes confusing because, by convention, some coding languages require you to pre-define the data type of the value being returned, such as "bool" in your example "bool isRunning(){}". We've coded the final value being passed out of the isRunning function to be a bool! So why do we need to state that again at the start of the function call? Remember though, that in some languages you don't. Pre-defining of the type being returned is required in Arduino and some other languages, while not being required in Python. Python's returned values simply take on the type of whatever value was passed out of the function via the return statement. For example, if you say "return isFunctioning" and isFunctioning is the boolean True, the return type of isFunctioning is automatically "bool". If you instead coded isFunctioning to be a binary 1, then the return type of isFunctioning is automatically "int". You'd think that would be standard right? Me too. So why is the odd/redundant "pre-define-necessary" convention present in Arduino? That's a choice of the original coders of the C-based/Arduino languages that I don't yet understand. I'm sure there's a reason, but I can't yet figure it out. For now, it's just a convention I accept, and I hope to discover the reasoning behind later as I become more familiar with the languages.

    • @HardwareUnknown
      @HardwareUnknown 3 ปีที่แล้ว

      ​@@grantscott1686 Check this out: en.wikipedia.org/wiki/Return_type
      Just underneath the code block at the top of the page it says "the return type is int". That's a great and simple example. Hopefully it helps reinforce what I mentioned.

  • @ericthered9655
    @ericthered9655 3 ปีที่แล้ว

    To find a hypotenuse, you just go down to the river. But be careful - they are really mean.

  • @aa2359
    @aa2359 2 ปีที่แล้ว +1

    awesome skills just please slow down when speaking for us smooth brains

  • @chuckthebull
    @chuckthebull 3 ปีที่แล้ว +1

    You should have stopped the video and gone and got an ice-cream from that truck down the block playing that music..... but thanks!

  • @fares3651
    @fares3651 5 ปีที่แล้ว +1

    lol use the already existed Serial.read() function

  • @PhilBailey
    @PhilBailey 6 ปีที่แล้ว +3

    Music is distracting, otherwise decent video. Constantly felt like terrible Mario game over mode. Sorry

    • @chuckthebull
      @chuckthebull 3 ปีที่แล้ว

      I thought it was an icecream truck down the block

  • @bernym4047
    @bernym4047 ปีที่แล้ว +1

    a and b should not be integers as in the real world your triangle sides could measure fractions of a unit. E.G 3.75 meters and 5.2. meters.
    I will need to watch again and try to keep track of you scrolling up and down the code while you rattle on about what is happening.
    Not a good presentation as you seem intent on finishing in time to go and watch the next edition of 'Neighbours'.

  • @mollysmith2713
    @mollysmith2713 4 ปีที่แล้ว

    honestly, wtf.

  • @youpattube1
    @youpattube1 4 ปีที่แล้ว +4

    I don't know why you found it necessary to distract our attention from understanding your explanation by playing 6th grade mario bros tunes simultaneously.
    On reflection, maybe I do. You are somewhat dull witted, and you assume everyone else is too, and that you must try and make computer programming FUN.
    Next video wear a red nose.

    • @sanathshet6945
      @sanathshet6945 4 ปีที่แล้ว +2

      My god please shut up. I know the music is distracting for you but you don't have to act smart.

    • @darthvader52519
      @darthvader52519 ปีที่แล้ว

      clown detected 🤣🤣🤣🤣🤣🤣🤣🤣🤣

  • @WhoCaresWinsAgain
    @WhoCaresWinsAgain 4 ปีที่แล้ว

    Why the hell do you use the really annoying background "mario" type music...the video content is great but the music made me give up! ;-(