#71 How to create an Arduino Library - easy!

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 มิ.ย. 2024
  • New! ralphbacon.blog
    We all use libraries when writing Arduino sketches but how do you go about creating your own, even if they are for your OWN use just to organise your code better?
    I'll talk you through a complete example, end to end, that shows you exactly how to create a library that simplifies the remaining code in your sketch, thus allowing you to concentrate on what you are now developing rather than the mature, stable code functions you've already written.
    Once you've done it once you'll see how useful it can be even with quite modest sketches. And, of course, if you have to tweak your library code then there's nothing stopping you doing that later either.
    The demo sketch, plus the libraries we created during the video can be found here:
    ----------------------------------------------------------
    Sample demo library code in GitHub
    ----------------------------------------------------------
    *** Remember to put the library files (.cpp, .h and keywords.txt) into a folder
    *** called "TestLibrary" in the libraries folder under your Arduino sketch folder.
    *** Then put the sketch file (.ino) in a folder in your normal Arduino sketch folder.
    Just click the download button on this page to get a zipped file of the above.
    bit.ly/ArduinoLibrary
    ---------------------------------------------------------------------------
    Here are the links that I mentioned in the video:
    ---------------------------------------------------------------------------
    All about Arduino IDE keywords
    spencer.bliven.us/index.php/2...
    The Arduino Library specification
    github.com/arduino/Arduino/wi...
    ---------------------------------------------------------------------------
    And finally...
    ---------------------------------------------------------------------------
    If you like this video please give it a thumbs up, share it and if you're not already subscribed please do so :)
    My channel is here:
    ------------------------------------------------------------------
    / ralphbacon
    ------------------------------------------------------------------
    How can I remember this? Memory tip: "See" Ralph Bacon, geddit?
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @grahammidgley401
    @grahammidgley401 7 ปีที่แล้ว +9

    Hi Ralph
    Many thanks for such an informative video, I have a question, is it possible to open the *.h and *.cpp files in the Arduino IDE, sorry if I missed something in your video which explains this I have watched it three times. You stated in the video that we could play around with these files and if we made a mess of them we just download them again, from that I thought you meant we can alter things in all three of the files.
    Many thanks
    Graham

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว +16

      Hi Graham! THREE times you say? You probably know it word for word by now! Yes, you most certainly CAN open the .h (header) and .cpp (c plus plus) files using the Arduino IDE. There are some caveats:
      1. To CREATE a new file (which will reside in the same folder as your sketch) click the small triangle at right hand side of the toolbar (containing the sketch tab). You will then be prompted to enter a name (eg myheader.h). The prompt appears at the bottom of the code window (!)
      2. To OPEN an existing library file, copy both the .h and .cpp files to your sketch folder, then open your sketch. The additional files will appear as separate tabs next to your sketch's tab.
      3. To include a library file in your sketch, when the library file header resides in the same folder as your sketch, use the format #include "mylibrary.h" *not* #include as the latter format is used when you want the Arduino IDE to search your libraries folder (and elsewhere) to find a library.
      4. If you muck up the .h or .cpp files you can just re-copy them over to your sketch folder again whilst you experiment. If you enhance the library, and want to keep the changes MOVE the files back to the libraries folder (but do take a backup of the original files first, just append .old to the filename or something). Then refer to that library in the usual way with the standard #include again.
      I guess I should have clarified this in the video better, but maybe this comment will be read by gazillions of viewers too.
      Thanks for posting this question, it will benefit others wondering this.

    • @grahammidgley401
      @grahammidgley401 7 ปีที่แล้ว +6

      Hi Ralph! Many thanks for the quick reply, Item 2 answers my question and works fine, I now have all three files together so I can play with them.
      Please keep up the great work, very interesting content, I am also going to watch more of you videos.
      Regards
      Graham

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

      @@RalphBacon Well, I don't know about gazillions... but I've definitely read it. Comments can hold vast amounts of additional information and are worth reading.

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

      @@RalphBacon Thanks for the informative video.
      As for #2, it would seem that you're opening a copy of the library that you're using via , not the actual library that it's compiling? That there's not an easier way to view libraries with the IDE is a bit of an annoyance (not your fault, obviously). Seems like Notepad++ or similar is a good way to take a peak at them to figure out what functions etc are available, but that's not perfect either.

  • @TheNeonbuffalo
    @TheNeonbuffalo 5 ปีที่แล้ว

    Wow thank you so much. Some people are complaining about how long this video is but its was great because you not only created a library live in front of us but also explained the background of whats happening. Great Video!!

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

      Complaining? About MY video lengths! Actually, Aldrick, I gave up trying to make shorter videos some time ago and now just make them whatever length they need to be, usually around the 30-minute mark. It's about the right length if we're going to discuss stuff in the correct level of detail, don't you think? Well, it seems you do! Thanks for posting!

  • @mibra7920
    @mibra7920 6 ปีที่แล้ว

    I do programming for past 4 years and this was like the best tutorial which Ive ever seen. Thanks!

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

      High praise indeed, Mibra, thank you for that and for posting here.

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

    arduino noob here... after months and months and months of studying, watching videos... its the first time i see how to collapse sets of codes... my gosh, so many tutorials and this is the only one that explains that! thank you!

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

      Glad I could help! Funny what you find at unexpected moments!

  • @timothycox2842
    @timothycox2842 7 ปีที่แล้ว

    Hi Ralph,
    Your videos are wonderful. Very instructive and very easy to understand. I have written my first library using your video and just wanted to say thanks and keep it up!

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว

      Thank you very much Timothy, I'm glad I'm being of some use! Like most things, once you're shown how (and, possibly, why) then the mystery disappears and you think "Is that it then?" I hope you create many more libraries (I must admit to cheating whilst developing and just stuffing items into a .h file until I'm ready but don't tell anyone!). Thanks for posting, much appreciated.

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

    I never thought I would understand such a complicated subject but you made it so understandable. Thank you so much! You have a special 'teaching gift'...

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

      Glad it was helpful!

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

    I very much like the way you teach. It is as if I was sitting down with a friend or colleague who wants me to understand something, but wants it to be a pleasant experience.

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

      Nice of you to say, thank you 😉

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

    Great informative video. This has helped me to no end.
    Thank you 😀👍🏻

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

      Thank you, Harry, for your supportive words. I'm glad it's helped you (and hopefully others too!). Once you've done it a couple of times it gets quite automatic to move stuff into a library. Good to hear from you.

  • @cschmitz
    @cschmitz 7 ปีที่แล้ว

    Thanks for this walkthrough, Ralph. I'm coming from other programming languages (primarily web dev), and this was a great overview of the idiosyncrasies of c++ when it comes to object oriented code and including local libraries.
    I had been reading through articles and posts that explain the same thing, but none of them were as clear and well presented as this video :)
    Thanks for taking the time to make this and for posting a link to the code. It's much appreciated :D

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

      Well, thank you, cschmitz, for your kind words! I'm glad to have helped. I, too, have taken many paths in coding, from µProcessor assembler through various BASIC dialects, C#, JavaScript, and C++. I think once you have a programmer's way of thinking then the language is secondary to what you want to do, so you (as a web developer) are well placed to mastering this very quickly. Thanks for posting and good luck in creating your own libraries!

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

    Thank you. I really liked the way you explained everything without rushing :)

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

      Well thank you Goldichan92, I appreciate that you took the time and trouble to post that. Unless my Arduinite viewers tell me how I'm doing I have to guess which is not the best way to build up an audience. So thanks again, and keep watching! And don't forget, if you like what you see, consider subscribing! :)

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

    Ralph, I came across this video (#71) a few months back, and am now proud to say that I have written my first Arduino library. I've a lot of work to do on the code, but I have built a class with methods, and it is likable into a users sketch. I am now working to configure it to allow easy importing into the IDE and compliance with Github standards.
    So many thanks for your inspiring and instructive videos.

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

      Fantastic! And by writing the library your understanding of Object Oriented Programming has taken a giant leap for Dale-kind! Really happy about this!

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

      @@RalphBacon Yes and thanks to your tutelage, I have now written and tested a Python class. This could become addictive 😁

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

    thanks for this lesson with detail explanations on each lines and resons. I picked up many knowledge not known before as I was trying to make my own library.

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

      Glad it was helpful!

  • @ch0rd2
    @ch0rd2 7 ปีที่แล้ว

    Many thanks Ralph, have just written my first library. Excellent resource!

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว

      Awesome, Bernebeer! Congratulations on having done this. Now you have started you can progress to more complex libraries. Perhaps one day I will be using a library that you created, who knows. Thanks for letting me know, great post.

  • @JamesHardiman01
    @JamesHardiman01 5 ปีที่แล้ว

    Totally brilliant ! Thank you so much for explaining that ... and at a good pace: neither too fast or too slow, and in an accent that my ancient ears can cope with!

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      You are most welcome James Hardiman, I'm glad you like the video. Nice to hear from you.

  • @esserfranz-juergen5886
    @esserfranz-juergen5886 6 ปีที่แล้ว

    Hello Ralph, very many thanks for the great overview within 30'. Well done ! - All the Best, Franz-Juergen

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Glad you liked it, Franz-Juergen, ziemlich einfach, gel? Now that you have some knowledge you can create your own libraries, but start slowly, as there are some 'gotchas' as they get more complicated. Thanks for posting.

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

    You are a king. In 1.5x it is the perfect introduction to Arduino. Thank you so much

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

      I suppose I could speak slower, that would confuse you! Glad you liked it, thanks for posting.

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

    Beautiful, non ambiguous, simple example... Clear and pleasant to listen presentation...

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

      Glad you liked it!

  • @pekkagronfors7304
    @pekkagronfors7304 7 ปีที่แล้ว

    Great stuff. Revisited this video today when I actually needed the information.

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

      Don't tell anyone, Pekka, but I do the same when I need some (good) advice, heh heh! I guess as long as you know there is a video with information here then it's perfect that you returned and watched it when you needed it. Thanks for getting in touch, good to hear from you.

  • @davidstonier-gibson5852
    @davidstonier-gibson5852 4 ปีที่แล้ว

    Thank you Ralph. I hesitated to start a 38 minute video tutorial, but it was worth it just for the calm Pommy accent and absence of duffa duffa music! I am an old veteran since the 70s of programming assembler on bare metal for embedded realtime controls, and only just in retirement wrapping my head around C/CPP. Some of the syntax conventions really "get" me, and I have to remind myself I am dealing with a very simplistic one-pass compiler that needs lots of cues in order to work.
    The work flow from multiple tabs / project folder to files in the library folder was very helpful.

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

      Glad you found it useful, David. All my videos are a bit long but watch them at 1.25 speed and it shaves many minutes off the length. And I don't sound like a chipmunk on helium either!

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

    Thank you so very much. One of the best tutorials on C++ code that I have seen. It helped me a lot. You are the best.

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

      You're very welcome!

  • @gabrielefugazzi579
    @gabrielefugazzi579 6 ปีที่แล้ว

    Very nice tutorial! The explanation speed was perfect since it allowed me to think deeply about what you were saying. I subscribed to your channel too. Thanks a lot!

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Thanks for your post, Gabriele Fugazzi, I'm glad you enjoyed the video. Good to hear from you.

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

    So glad I found this. Thank you for your efforts.

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

      Glad you liked it, Dick, thanks for posting.

  • @emiliozelione7113
    @emiliozelione7113 6 ปีที่แล้ว

    Hi Ralph. This video helps me a lot. Congratulations!!!

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Thanks for your post, emilio zelione, good to hear from you.

  • @VeryMuchBlessed
    @VeryMuchBlessed 6 ปีที่แล้ว

    Very helpful information for sure. Thanks much for taking the time to explain this.

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Thanks for that, gene guru1, nice to hear from you.

  • @9brian99
    @9brian99 3 ปีที่แล้ว

    Thanks for a great tutorial it has helped me on my Arduino journey as it’s time to really understand what some libraries do and the place to start is making your own.

  • @davisnicholas9123
    @davisnicholas9123 6 ปีที่แล้ว

    Great video helped me a lot with my project! Keep up the good work!

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      I'm pleased to hear that Davis, excellent! Thanks for posting.

  • @BOBANDERSON5150
    @BOBANDERSON5150 6 ปีที่แล้ว

    Thanks again for another very useful tutorial. Can't wait to write my own library in my next project.

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

      Yes do try, Bob (Yoda: there is do or not do, there is no try), it's not difficult but start small and work your way up! Thanks for posting here, nice to hear from you. (I hope you got the Yoda reference)

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

      “PATIENCE YOU MUST HAVE my young padawan”

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

    Hi Ralph
    Thanks for this clear explanation ! I created my first library files and ... it works !! :)

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

      You sound surprised! Yes, once you have written one library you can progress and learn all about classes, objects, private, protected and public functions and variables... the list goes on. Well done!

  • @Eurus721
    @Eurus721 7 ปีที่แล้ว

    Oh I'm going to use this info for sure. Thank you sir

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว

      Excellent, Ricky, start small and work up. And look at other libraries to see whether there are any other techniques you might use. Great to hear from you.

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

    Incredible explanation and pace

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

      Glad you liked it!

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

    Thank you for your time and effort in making this video, i was able to follow along in real time recreating the sketch while modifying various attributes of it. Very good tutorial highly recommended.

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

      Thanks Chris! You did exactly what I hoped other viewers would do. It's only by actually doing that you learn things. Thanks very much for posting.

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

      @@RalphBacon Not a trouble I always leave a like. Don't suppose you fancy making a video about using other libraries with a library.
      Got square eyes trying to figure out how to include the wtv20sd16p library in my library. 😅

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

      You should be able to just #include the library.h you want within your library in the standard way. What's the problem?

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

      @@RalphBacon I did figure it out in the end but it's surprising how scattered the info is, I couldn't figure out where to instance the object correctly to use it throughout the library.
      I actually found two methods one used pointers which I no longer use but Google hot to use a library in a library and there is no definitive guide or tutorial on the do's and don'ts thanks for your reply though.

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

    Thanks for a very clear and informative video. I had wondered what the cpp and .h files were for - now I know. Not sure I like OOP, it just adds another level of complexity.
    Many thanks for sharing this.
    Best wishes
    Arthur

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว

      Hi Arthur! I'm glad you enjoyed the video, but what's all this about "adding complexity"? :)
      OK, it IS something different but simple classes in Arduino can lead to better programming. Mmm, perhaps I need to do an introductory video into classes (well, OOP) for Arduino! Maybe. Lots of other stuff to cover first.
      Anyway, glad you liked it and thanks for posting, great to hear from you.

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

      I have subscribed and looking forward to more videos - I have a lot to learn!

  • @ZeedijkMike
    @ZeedijkMike 5 ปีที่แล้ว

    Very nice tutorial. Good content explained in a nice and relaxed way.
    You found yourself a new subscriber.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Thanks for your post, Zeedijk Mike, thanks for subscribing, too. Good to hear from you.

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

    Thanks Ralph. A straightforward explanation that has stopped me binning my Uno! I have 18 switch x inputs to pick up and three sensors

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

      Glad it helped!

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

      Binning you uno! I am deeply offended. Joking, but glad it helped.

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

    Extremely interesting and useful. Wish I had found it a couple of months ago when I was just getting started with Arduino. I'll have to watch it a couple of more times as I'm a slow learner.

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

      I have to watch my own videos sometimes to see what I said!

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

    great and clear lesson ... needed to refresh classes... need to refresh everything to be honest.:) thanks a lot Mr R.B.

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

    Thank you very much for your clear and rich explanation.

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

      Glad it was helpful!

  • @johnmellor6065
    @johnmellor6065 7 ปีที่แล้ว

    brilliant tutorial , explained so well thanks

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว

      Hello Arduinite! I'm currently on vacation 5,000 miles away from home so I'll have to be brief with these comments until my return, mid-July.
      Glad you found it useful, John, I like comments which contain the words "brilliant tutorial"! It's not so hard once you get to grips with the fundamentals. Thanks for posting.

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

    Superb explain on how to make a library. Thank you sir.

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

      You are most welcome!

  • @demetriosdemetriou1186
    @demetriosdemetriou1186 5 ปีที่แล้ว

    I QUEST ALWAYS BENNY IS HELPING OUT! VERY GOOD PRESENTATION RALPH

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Thanks for your post, demetrios demetriou, good to hear from you. Benny is pleased too!

  • @deangreenhough3479
    @deangreenhough3479 6 ปีที่แล้ว

    Really good work and explanation, you have a new subscriber, thank you

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Thanks, Dean, glad you found it useful and thanks for subscribing!

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

    Omg this is so good... I learned a lot today for me to try write a code to sniff the LIN bus protocol. going to use this way of clean setup and also make it easy for me to reference to other codes available. now I can connect the dots a lot better.

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

      Glad it was helpful!

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

    The most high quality thing, thank you

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

      Glad you think so!

  • @Rose-ec6he
    @Rose-ec6he 3 ปีที่แล้ว

    Fantastic explanation! Thank you very much.

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

      Glad it was helpful!

  • @Mr-tl4sg
    @Mr-tl4sg 5 ปีที่แล้ว

    Excellent video, simple and easy to follow. My wife also liked your cat. Regards

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

      I feel sure I would also like your wife, as anyone who is a cat-lover is a friend of mine (and Benny). Glad yo liked the video, thanks for posting.

  • @alanpowell328
    @alanpowell328 7 ปีที่แล้ว

    Ralph, great tutorial. I now have some idea what I'm looking at when I poke around in library files. More like this please - a debug library would be ideal.
    Regards Alan

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว +3

      Hey Alan! I was going to include my debug routine in the library video but found that my code was "too advanced" for the compiler to figure out how to use it! Seriously! It's a limitation of the compiler but as always, there are workarounds but it just made it a bit messy and not really for a "first time" video - but I will include it (or upload it) with a future video.
      At least you now have a better understanding of how the libraries hang together and can appreciate the work some of the authors have gone to just to give us Arduinites an easy way of interfacing with bits of hardware. And of course, you could create your own libraries just to make things easier to work with. A nice cold Saturday afternoon project, perhaps?
      Thanks for the feedback and suggestion, great to hear from you.

  • @danielmelendrez1616
    @danielmelendrez1616 5 ปีที่แล้ว

    Amazing stuff! Subscribed. Cheers!

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Thanks, Daniel, glad you liked the video and thanks for the sub!

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

    Thank you for this great video .I must watch again

  • @PhilipHawthorne
    @PhilipHawthorne 6 ปีที่แล้ว

    Ralph many thanks for a very clear explanation of how to create an Arduino library. Apart from tidying up the sketch I would say the other main reason to create a library is for code that you tend to find yourself using in several projects. Once it's been debugged then you can forget about the dirty details and simply use it every time you need those methods. BTW I never knew about the IDE being able to collapse or 'fold' code so thanks also for that insight!

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      You've grasped the real use of a library completely, Philip. TBH even if you don't use the code elsewhere it's still good to 'modularise' the sketch this way as you quite rightly say you can forget about it. Glad you now know about code folding, it's quite useful. Thanks for your supportive words, appreciated.

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

    Hi Ralph!
    Thank you for this amazing vidro, made it realy easy!

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

      You're very welcome!

  • @danm7596
    @danm7596 5 ปีที่แล้ว

    Excellent video. Thank you!

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      You are most welcome Dan M, I'm glad you like the video. Nice to hear from you.

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

    Thank you, sir. You helped me a lot.

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

      I'm happy about that, Ryan, keep going!

  • @kees-ft1yb
    @kees-ft1yb 5 ปีที่แล้ว

    And again a great video. I learn a lot and it saves a lot of problems

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Glad you liked it, kees 7777, it's not so difficult once you're shown how to do it.

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

    Very nice explanation, thanks!

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

      Glad you liked it, thanks for posting.

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

    Great tutorial, thanks very much!

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

      Glad you liked it Austin! And you're most welcome. Thanks so much for posting.

  • @JamesHardiman01
    @JamesHardiman01 5 ปีที่แล้ว

    Oh, and ... thanks to Benny for helping you. Here in Cirencester Sammy and Morgan were helping me to absorb the knowledge.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Benny says that you are most welcome too, and sends his regards to Sammy & Morgan who are probably experts in Arduino C++ now (cats learn fast apparently). 😺

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

    very helpful. I have done a lot of programming but never really took the time to sit down and do that library stuff (eventhough I started a few times) .
    Will do again with help of your video.
    Oops, Sorry, I forgot I already commented on this video. Must be my age. Anyway, extra proof I like the video

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว

      I think you have just proven that you REALLY like this video! I'm happy with that! Constructing library classes is not magic, voodoo or only for "experienced" programmers. It's a series of steps and concepts that can make your programs "better" (oh, dear, red rag to a bull I know). Best thing is to try it with a really, really simple function (as per my demo) and get that working - then build on that. Thanks for reiterating your love of this video, I hope others feel the same way :)

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

      indeed. I am no stranger to making 'classes' but wrapping it in the
      library files is something I just haven't taken enough time for yet to
      be able to do it fluently. Enjoy yr weekend

    • @tyeth
      @tyeth 7 ปีที่แล้ว

      Yep, DRY, good work.

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

    Brilliant Video. Thank you so very much.

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

      Glad it was helpful!

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

    very helpful!
    I also had forgotten the semi-colon at the end of the class declaration. Luckily, it was the first thing I randomly landed on when I was flipping through the video to check if it was any good.

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

      I'm glad it helped, Jason, thanks for posting.

  • @jstro-hobbytech
    @jstro-hobbytech 2 ปีที่แล้ว

    I forgot how good this video is. I think the grammar of classes are what make them seem intimidating. The textbook jargon like instantiate, constructors, deconstructors, self.label type stuff. But if people let go of the jargon like you're showing then people understand it better. When I started, and I'm no expert but the 'self.' Stuff got me confused the worst because all the constructor variables were just stated above. Haha

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

      When I created this video, Joey, I was very careful to _not_ mention anything OOP-related as I knew it would put viewers off! Talking about polymorphic inheritance might be a bit off-putting (just a wild guess). 😁

  • @iceberg789
    @iceberg789 6 ปีที่แล้ว

    best tutorial on libs. thanks.

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Thanks for your post, iceberg789, good to hear from you.

  • @digihz_data
    @digihz_data 5 ปีที่แล้ว

    Love this about making libraries.
    Plz make another video of making libraries.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Thanks for your post, Hans Sandberg, I'll think about making another. Good to hear from you.

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

      @@RalphBacon this is definitely the best arduino library tutorial so far on TH-cam and it's nice to see you answering peoples question on your older videos. i'll keep eye on for second part. Love from Finland👍

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

    Thank you for this!

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

      You are most welcome Mario, perhaps you'll use your new knowledge in your magic shows? Plenty of other videos to choose from too, you never know what you might pic up. Thanks for posting, it was most appreciated.

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

    Nice job explaining what can be somewhat daunting for those learning. One thing, when coding in C/C++ you should also include a destructor in the class, but not sure if necessary on an MCU which will most likely loop forever anyway.

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

      Destructors can be included if there is ever a need to destroy the object, good point, thanks for mentioning it.

  • @ravindratalekar4965
    @ravindratalekar4965 7 ปีที่แล้ว

    Great explanation!

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว

      Hey Ravindra, I'm glad you liked the explanation. Always good to hear that my videos are giving my Arduinite viewers some value.

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

    You are the man, I could not figure out how to use the serial port in my cpp file. I tried everything until you showed the method to - begin serial. Like you said, there just isn't any useful online documentation available for this - JOLLY GOOD SHOW !

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

      Glad this video helped you, Jeff, nice to hear from you.

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

    Oh! Good man for showing me the option to collapse code!

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

      Happy to help! It's the little things that can make a big difference!

  • @EliasSilva-ff7us
    @EliasSilva-ff7us 3 ปีที่แล้ว

    This helped me a lot.

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

      Great! Glad it helped!

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

    Very helpful, thanks!

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

      Glad it was helpful!

  • @baraahalabi
    @baraahalabi 6 ปีที่แล้ว

    Thank you so much for sharing this great information ......
    but I have a question... how can I include some sketch examples within the library?
    and again thank you so much !

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Hey Baraa, glad you liked the video. But I don't quite understand your question. Do you mean how to include your library into a new sketch? Or something else? If you could clarify that would be great.

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

    Thank you for sharing!

  • @philbx1
    @philbx1 7 ปีที่แล้ว

    Thanks much Ralph!
    Here's where I get into stall mode.
    For instance if I wanted to output an instantiation message to another device apart from a serial port or output to another library function ie. write to an SD card then things get confusing.
    I know that your code is just an example and that any 'portable' library wouldn't open a serial port regardless, but further
    thinking brings on many complex options.
    Great example nonetheless!

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว

      Hi ya Phil, interesting comment there!
      If you wanted to output serial data to another device, when we have already used it for our debugging window (Serial Monitor), you could always use the Software Serial library which allows any pin to use serial data. I've used it many times and it works well and stops you being restricted to pins 0 & 1.
      But as you say, this was but an example showing how to put a library together (rather than concentrating on the functionality) and I'm glad you liked the example. Good to hear from you.

  • @eonang9751
    @eonang9751 5 ปีที่แล้ว

    Thanks for this amazing video!!! your explanation is very easy to understand for me as a beginner!
    But i have a question about what if you include a library that isn't within the same files, the memory will get lesser or still the same?

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

      Although everything gets COMPILED when the LINKER decides what to include then anything not required is dropped so your output file remains small. Clever, yes?

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

    Thanks Ralph!

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

      Thanks for that, NeXus, nice to hear from you.

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

    Thanks Ralph. Great info! I'm used to C, not C++, but I will definitely switch over. Then I can put my common useful functions in a library instead of re-copying them into various ino sketches(and which one has the latest modified version?). I saw that I can also create a subfolder in ../libraries/TestLibrary/examples/LibraryExample1/LibraryExample1.ino so that it is visible from the Arduino ide files/examples.
    GREAT STUFF!
    Made my day!

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

      If you know C you will pick up C++ very quickly (but you will also confuse the two I'm sure)! I'm glad this video helped you though!

  • @Brumby0
    @Brumby0 6 ปีที่แล้ว

    Excellent, thanks!

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Glad you liked it, Brumby0, and do feel free to watch other videos on this channel! Thanks for posting!

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

    Beautiful Benny...

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

      Thank you kindly. Benny has now been gone for 8 months or so and very greatly missed.

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

      @@RalphBacon Oh.... so sorry...

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

    thak you
    you are the best in youtube

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

      Very kind of you to say!

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

    Great tutorial! Would you be able to do a tutorial on how to deploy library’s so that others can use them and find them in the library manager?

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

      Great suggestion! I'll have to look at that, Douglas, as I've never released a library into the wild other than on my GitHub.

  • @das250250
    @das250250 5 ปีที่แล้ว

    Thanks for this , it has given me a chance to try and debunk some previous written software ( ble for arduinos) . A question , how do we best debunk what are the available commands ( methods ) within a defined class and their uses ?

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Debugging code for an Arduino is somewhat crude. It involves using the Serial.print() method to write out to the Serial Monitor (aka debugging window) messages that might help you understand what is going on or wrong. And judicious use of the delay() statement to slow things down a bit so we can see what is happening in slow motion. Newer versions of some µControllers (including the MKR boards from Arduino) allow hardware debugging but that's for the future - for now you're stuck with Serial.print(), I'm afraid.

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

    But first I must feed Benny!!! LMAO :) Love it

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

      If you are new to my channel, Joel, you may not realise that Benny writes most of the C++ code. If he does deign to let me have a go he then monitors everything I write with a beady eye. He is a hard taskmaster.
      Of course, if you have watched more of my videos than you already know this and the fact that he sometimes sneaks into my workshop at night and corrects all my syntax mistakes, recompiles everything and uploads it all to the µcontroller board I'm using. I'd be lost without him.

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

      RIP Benny

  • @wotesi
    @wotesi 5 ปีที่แล้ว

    Dear Ralph,
    Great video, thanks! Explained exactly what I am trying to create at the moment. And I still have a questions.. One thing I did probably wrong is that I put all the functions inside *.h file, did not made and *.cpp file. It somehow works, but let me change it and see it working again. In my project (remote control tank) I am going to use GPS module and GSM module (and some others as well), so I really do not need that additional code into my main code, just a results, e.g. GPS coordinates, so library is excellent solution.

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      Technically speaking, Valters, putting all your code in the header file (whilst punishable by watching the BLINK program for several hours) _should_ work. But, as you are aware, it's just not done that way so do split them as you have outlined - and let us know how you get on.

  • @mattnguyen9441
    @mattnguyen9441 5 ปีที่แล้ว

    Hi, great video. I have a question. In my main code, I have things such as #define MOTOR_1 13, etc for my motor driver. Where do those go or how do I put those into my header files?

    • @RalphBacon
      @RalphBacon  5 ปีที่แล้ว

      You must understand how the compiler treats those #define lines. They are purely *textual substitution* and have no meaning beyond that. If you #define LED 4 then whenever the compiler finds a strange word called LED in your code it checks to see if you have defined it and then substitutes, exactly, what you have written, in this case 4. That's it.
      So you can continue to define these in your header or cpp files just as before. As the library is compiled (as part of an #include statement in another sketch) it finds all your #define lines and compiles the library as though it were part of the original code.
      Watch out for #define statements that might be used by the user (that will confuse the hell out of the compiler). Put a leading underscore or some other identifying character(s) as part of your defines. So in the above example you might use #define __LED_ 4 and then you use __LED_ instead of just LED in case your library consumer uses the LED define (a common enough word). Understood? Or clear as mud?

  • @avejst
    @avejst 6 ปีที่แล้ว

    Thanks for sharing 😀👍

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Thanks for your post, Asger Vestbjerg, good to hear from you.

  • @KW-ei3pi
    @KW-ei3pi 11 หลายเดือนก่อน

    Ralph: How do you find out what functions a library includes and what there parameters are? I want to use the AccelStepper library but I don't know what functions it includes or how to use them. Thanks

    • @RalphBacon
      @RalphBacon  11 หลายเดือนก่อน

      Most libraries are on GitHub. The GitHub for a particular library should have documentation (or a pointer to the Wiki for it). This will tell you everything you need to know.
      Secondly, if you look at the library, in particular the library headers (files ending in ".h") these will provide the PUBLIC functions you can call. From there you can find the relevant function in the associated c++ file (ending in ".cpp") and read it, to see whether it's documented enough.
      Also the GitHub will probably contain an "Examples" folder with some simple get-you-going sketches. Another source of gold to beginners.

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

    Thank you very much! :)

  • @ucmetoo1
    @ucmetoo1 6 ปีที่แล้ว

    It has been many years since I have done it, but if you break the source code file into different files according to the different methods in the class, GCC will include only the functions called in the final binary. It will do this process during the linking process. An example is the standard libraries. If you examine the libraries they are an agglomeration of separate object files per standard function.

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Yes, you're right of course Uzi, the compiler/linker is intelligent enough, thank goodness, to only include code that is actually called! Just as well, given the limited space in an Arduino Uno. For us Arduinites that have created their own 'Utilities' library (a collection of 'useful methods') it means that not all the library is included each time, so it doesn't really matter how many methods you place in it (within reason, we don't want a huge monolith of unrelated functions!).

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

      Again relying on memory, The procedure of separating the functions or methods is tedious and you have to pay attention to the separate source files, edssentially treating them as a separate project each.

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

    great video , thanks

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

      Glad you enjoyed it!

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

      I've subscribed to follow you. Thanks 🙏

  • @SkottTomas
    @SkottTomas 6 ปีที่แล้ว

    Hi Ralph
    Thanks for doing all these great videoes! They are simple to understand and easy to use. I have refered some of my students to use them - hope that's ok? 😊
    Have you tried to hook more pcf8574 up with i/o's? How do you easily scan let's say 4 units?
    BR
    Tomas

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Glad you like these videos, Thomas, and certainly direct your students to them. But if they fail whatever course it is that they are studying I take no responsibility! For the PCF8574 look at my video #49 MORE PINS PLEASE! PCF8574 Arduino Pin Extender (Easy), I'm spoiling you now!

    • @SkottTomas
      @SkottTomas 6 ปีที่แล้ว

      You would't? ☺️ Of course it's their own responsibility. I teach electronics on technical college.
      Yes, I have seen the #49 video. It's really easy to understand and use, but what I'm asking is, if you also have an easy way to handle inputs and outputs when daisy chaining more pcf8574's? I do have a way of doing so, but perhaps your way was easier?

  • @abinavanantharaman1555
    @abinavanantharaman1555 7 ปีที่แล้ว

    Heyy Ralph!! I want to include LiquidCrystal.h lib in my custom library. I am not able to understand how should I go about. Where should i write #include and where should i create my object 'lcd'??

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

      Hello Arduinite! I'm currently on vacation 5,000 miles away from home so I'll have to be brief with these comments until my return, mid-July.
      Write the 'include' statement at the top of your .h library file. If you want to use the LCD library ONLY in your own library then create the lcd object in exactly the same way as you would with a standard Arduino sketch. If you want the user to be able to access the lcd object then you must use the EXTERN keyword which I'll explain in a future video (you can Google it for now!). If you get stuck let me know exactly what you are trying to achieve and I'll explain further on my return, Interesting question you asked here, Abinav!

  • @daguerref1
    @daguerref1 6 ปีที่แล้ว

    Thanks you!, very clean explication i like your style, only a note that could be tricky: in the #ifndef statement we can't use the same name of the library, this could be confussing because the declaration of the files .h .cpp and the constructor needs to be namend equal.

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Indeed, you are correct, the common practice is to use a name in capitals or with underscores after the name or both - you get the idea! Pity that I used a lower case two character name called tl (for test library) but then again this was just a demo)! Thanks for the advice, good to hear from you.

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

    Hi Ralph
    I`ve enjoyed your many videos over the years, thank you.
    I have a question how to create a custom library in PlatformIO, there seem to be various issues with this, having read various forums and trying to implement your code within PlatformIO making this way above my pay grade intellectually and was hoping for an insight to point me in the right direction.
    Many thanks
    And keep up the good work
    Andrew

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

      Creating a custom library in PlatformIO is very similar to when using the Arduino IDE, Andrew.
      You still need the ".cpp" file and the ".h" header files which you can create in the "lib" folder and #include "lib.h" (whatever you called it) into your "main.cpp" sketch (which resides in the "src" folder, of course).
      Then, to test the library out (probably as you're writing it) compile your "main.cpp" sketch and see what errors you get! Best to do this incrementally, by defining some (public) functions in the header file of your library and put in some placeholders into the supporting .cpp file that do nothing except display a message that you've called that function.
      The good thing about PlatformIO is that you can use the "lib" folder for libraries that never, ever change (or that you don't ever want to update for a particular sketch) or you can use the "include" folder but tell the PlatformIO system that you will only accept updates up to a certain revision point, such as accepting all revisions to version 2 but you won't accept new version 3 for this particular sketch. This ensures that future enhancements to a library won't break your sketch some distant time in the future (which happens all the time in the Arduino IDE).
      However, all that is once your library is written and tested, so have a go with that part first.

  • @arduinoforbeginners4680
    @arduinoforbeginners4680 6 ปีที่แล้ว

    feel you share, I very much like!

    • @RalphBacon
      @RalphBacon  6 ปีที่แล้ว

      Thanks for your post, Arduino for beginners.

  • @jstro-hobbytech
    @jstro-hobbytech 2 ปีที่แล้ว

    Great video Ralph. Oop for newbs is a hard topic to teach. When I went to university they were just defining the standard for oop in c/c++ so I missed out because I stopped coding from 20 years. I had a fun time learning all the arduino basics because it all came back to me. It woke something in me so I took an advanced pointers course, data management and then I took the course for oop and made like ten fake entities then wrote code based on the classed I created. Haha
    You make the complex make sense very well my friend.

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

      It sounds like your interest in OOP programming is still strong. Those developers that put in the effort to learn how to make a class will make better programs for sure.

    • @jstro-hobbytech
      @jstro-hobbytech 2 ปีที่แล้ว

      @@RalphBacon what language do you use at work? I think I asked you this before. I apologize if I did.

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

      At work I used C# in a dot Net full-stack environment: JavaScript, Dapper, SQL server, HTML... the list goes on.

    • @jstro-hobbytech
      @jstro-hobbytech 2 ปีที่แล้ว

      @@RalphBacon you build end to end software for other companies ehh

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

      Actually, the last software house I worked in for five years built software for their own company but it was used (implemented) throughout the UK. 22,000 clients. Tricky stuff!

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

    Thanks for the video Ralph - yes I know it's mature. Lovely to see Benny again. I think the reason you cant put Serial.begin in a constructor is that Serial is a static class, it's a single resource. That's to say you can't really create another Serial for each instance of your class. I created a Ping)) class with OTT features including limiting the time pulseIn will wait based on a user decided maximum distance of interest. I wanted to check the maths with a debug method; in that method I simply use if (Serial) { print lovely stuff}.

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

      A good workaround you have there. Another solution is to try a singleton for the Serial.begin but I suspect that might not work either. Probably just better is the user sets up the Serial interface anyway, as it might be Serial1 on another device.

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

    Thanks Ralph - very informative. Do you know if it is possible to inherit a more complex type like HttpClient and create a custom library wrapping HttpClients functions in the class?

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

      Yes, absolutely, Robert.
      Include the library you want to wrap, and in the class constructor inherit the underlying class.
      For example, if you wanted to inherit the "Print" class so you could "print" characters to a new LCD screen you could inherit that (but remember to define all underlying virtual members):
      #include
      class myLCDPrinter: public Print {
      public:
      virtual size_t write(uint8_t);
      etc
      using Print :: write;
      }
      This then gives you access to ALL the underlying print functions (you don't need to define each one individually). Your class must now declare this method:
      size_t myLCDPrinter :: write(uint8_t value) {
      send(value);
      return true;
      }
      The method you write to do the "send" will be declared as "private" in your class:
      void myLCDPrinter::send(uint8_t value) {
      // your code here to do whatever you need to do to get the value
      // onto the LCD screen
      }
      As you already seem to know about inheritance I guess this is enough to get you started with the HttpClient!

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

      @@RalphBacon
      Thanks Ralph
      Yes and know on the inheritance most of my recent experience (last 15-20 years) is C# so syntactically a little different :-)
      I copied the HttpClient virutal methods into my header class (as you can see by the comments with the exception of endOfStream() and completed() they were all inherited from Print and Client, I'm not sure if I should have included them?)
      My header class looks like this.
      class SparkfunWifi : public HttpClient {
      public:
      // Contructor
      SparkfunWifi(Client& client, char* serverName, char* port, char* ssid, char* pass);

      // Methods
      /** Connect to the server and start to send a GET request.
      @return true if connection successful
      */
      bool connectWifi();
      /** Sends HTTP weather POST to weather.api.
      @return 200 if successful response from api.
      */
      int sendWeather();
      // Inherited from HttpClient which inherited them from Print
      // Note: 1st call to these indicates the user is sending the body, so if need
      // Note: be we should finish the header first
      virtual size_t write(uint8_t aByte) { if (iState < eRequestSent) { finishHeaders(); }; return iClient-> write(aByte); };
      virtual size_t write(const uint8_t *aBuffer, size_t aSize) { if (iState < eRequestSent) { finishHeaders(); }; return iClient->write(aBuffer, aSize); };
      // Inherited from Stream
      virtual int available();
      /** Read the next byte from the server.
      @return Byte read or -1 if there are no bytes available.
      */
      virtual int read();
      virtual int read(uint8_t *buf, size_t size);
      virtual int peek() { return iClient->peek(); };
      virtual void flush() { iClient->flush(); };
      // Inherited from Client
      virtual int connect(IPAddress ip, uint16_t port) { return iClient->connect(ip, port); };
      virtual int connect(const char *host, uint16_t port) { return iClient->connect(host, port); };
      virtual void stop();
      virtual uint8_t connected() { return iClient->connected(); };
      virtual operator bool() { return bool(iClient); };
      virtual uint32_t httpResponseTimeout() { return iHttpResponseTimeout; };
      virtual void setHttpResponseTimeout(uint32_t timeout) { iHttpResponseTimeout = timeout; };

      private:
      int _serverName;
      int _status;
      char* _ssid;
      char* _pass;
      };
      My .cpp constructor is as follows.
      // inlcludes
      #include "arduino.h"
      #include "SparkfunWifi.h"
      // constructor
      SparkfunWifi::SparkfunWifi(Client& client, char* serverName, int port, char* ssid, char* pass)
      : client(client), serverName(serverName), serverAddress(), serverPort(port),
      iConnectionClose(true), iSendDefaultRequestHeaders(true)
      {
      _ssid = ssid;
      _pass = pass;
      _status != WL_CONNECTED;
      }
      The error I get when I compile my sketch is:
      SparkfunWifi.cpp:6:1: error: prototype for 'SparkfunWifi::SparkfunWifi(arduino::Client&, char*, int, char*, char*)' does not match any in class 'SparkfunWifi'
      SparkfunWifi::SparkfunWifi(Client& client, char* serverName, int port, char* ssid, char* pass)
      ^~~~~~~~~~~~
      In file included from sketch\SparkfunWifi.cpp:3:0:
      SparkfunWifi.h:13:7: error: candidates are: SparkfunWifi::SparkfunWifi(SparkfunWifi&&)
      class SparkfunWifi : public HttpClient {
      ^~~~~~~~~~~~
      SparkfunWifi.h:13:7: error: SparkfunWifi::SparkfunWifi(const SparkfunWifi&)
      SparkfunWifi.h:24:5: error: SparkfunWifi::SparkfunWifi(arduino::Client&, char*, char*, char*, char*)
      SparkfunWifi(Client& client, char* serverName, char* port, char* ssid, char* pass);
      ^~~~~~~~~~~~
      sketch\SparkfunWifi.cpp: In member function 'int SparkfunWifi::sendWeather()':
      SparkfunWifi.cpp:48:5: error: 'client' was not declared in this scope
      client.beginRequest();
      ^~~~~~
      sketch\SparkfunWifi.cpp:48:5: note: suggested alternative: 'Client'
      client.beginRequest();
      ^~~~~~
      Client
      exit status 1
      [Error] Exit with code=1
      Thanks again for your assistance & I look forward to watching more of your videos.
      Cheers
      Rob

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

      Whether you need to include libraries is easy enough to test; just comment out the #include and see if you get a gazillion errors!
      In the line "SparkfunWifi::SparkfunWifi(Client& client" how is the Client being instantiated? Make sure you have the "Client" object in your class or reference it in some way. The compiler doesn't know this is HttpClient. Write a simple sketch to invoke all this first and you might see how to include it in your class. And use PlatformIO or Eclipse as they give you Intellisense that can _really_ help when doing this sort of thing.
      From one C# developer (.Net in my case) keep going. Sometimes it can take a while (and a few hours Googling) but eventually the solution will be found.

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

      @@RalphBacon Hi Ralph - I did persist and was able to get my custom class to compile - sort of - at least sorted out the inheritance side. I can get my library to send formatted JSON out the serial port and connect but when I try to serialize my JSON it the compiler complains when I uncomment serializeJsonPretty(doc, iClient); Thanks for all your help. I'll keep working on this last hurdle. Regards Rob

  • @Ed19601
    @Ed19601 7 ปีที่แล้ว

    Very informative

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว

      Glad you liked the video Ed. If you're into Arduinos you'll find many more videos on this channel that you may find useful. Thanks for the positive feedback, appreciated.

  •  3 ปีที่แล้ว

    Hi Ralph,
    thank you for explaining. Unfortunately, I have one question. What if I need (I don't know, how to say it correctly) some class variable, that is shared across all instances.
    For real example. I have a few moisture soil meters and I have decided, that I will write library for them. There are some instance variables, unique for each instance like "pump_relay_pin" or "moisture_limit". But I need one shared variable for the whole class. It's called "vcc_moisture_pin" and its value is same for all possible created instances, because it's shared pin, which turn vcc of all sensors on. It doesn't matter how many there are.
    So, I need this "class variable", which I can set directly via class name in main program, not via instance name. This variable I want to use only in library cpp file afterwards.
    Yes, I can use this variable as instance one, but I don't like it. I think, it is not programmerly correct.
    What I can use now
    H file
    class MyLib
    public:
    MyLib();
    byte vcc_pin;
    INO file
    MyLib Inst1;
    Inst1.vcc_pin = 2;
    What I want to use
    H file
    class MyLib
    byte vcc_pin; // I don't know, where to put this declaration
    public:
    MyLib();
    INO file
    MyLib.vcc_pin = 2;
    and then, every instance gets this vcc_pin variable with value 2, but I need to set this value in main program, not in library, because I want it versatile.
    Is there any option, how to do this?
    Could you please help me? Thanks a lot :)

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

      It sounds like you need to write a public function in the class that updates that variable (which should be private, not public). Another function to read it back. But that gives you that value for the class instance you are addressing, it's not global across all instances.
      You could accomplish that by creating another instance that you call globalMylib (or whatever) and use those functions I mentioned wherever you need to update or read it. What you really need is a singleton, or static class, but I only know this from C# not C++ - would have to check that out (or you could).

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

    Great video Ralph. This steps it up a gear for anyone who wants to understand object oriented programming. Once you 'Get It' the concepts are then only language specific. C++ is a bit unwieldy with its header files and you explained the concepts really well. The only thing , (and could be difficult for beginners), is Scope, the :: syntax.
    Really good Ralph, excellent mental workout following the code, cheers Alan

    • @RalphBacon
      @RalphBacon  7 ปีที่แล้ว

      Ha ha, I'm glad your brain got a good mental workout trying to follow my meanderings through class construction!
      But the point you make about 'getting' the concepts is spot on. Once you have that lightbulb moment all else is 'just typing'. I'm hoping enough people take my example, break it, fix it, break it again and basically get a good grasp on how libraries work and in doing so understand how classes work too in the (semi-) object oriented world of Arduino C++.
      Thanks for your kind words about me explaining it well. I don't normally watch my videos all the way through (after a couple of hours of editing I've usually had enough of my own voice) but I felt this time I had to make sure it all hung together properly and I think it does provide a good starting point for Arduinites expanding their wings, so to speak.
      Thanks for posting Alan, always good to get constructive feedback.

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

    do you reallt need to wite the "testLib::" dont the complier know that by itself? but need to write if its nothing else it want to point to. dont think i was wtiting it when i used visial studio.

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

      You do have to write the library name followed by two colons in front of every method in the library .cpp file. This is so the compiler knows it's part of the library class and not some other method/function you happen to be declaring at that point.