A long time ago I was playing around with 7-seg displays and 595 chips to drive them. I came across something that Nick Gammon had done, which seemed rather brilliant. Nick had a (multiplexed) design for driving a 3-digit display. I extended this to 8-digit displays. Basically: There are (up to) 16 pins on the 7-seg displays; 8 for the digits and 8 for the segments. And on 2x595s there are 16 parallel output pins. So, what Nick did in his schematic, was to use what I refer to as 'anatomically correct' device symbols, (where the symbol pin positions match the physical device.) He placed these in his schematic and basically drew the shortest connections between pins. Brilliant. Most people would designate one of the 595s for 'digits' and the other for 'segments', (and probably also map them in order, e.g. data pin 0-7 to seg A-G+DP etc.) But it really doesn't make any difference to the circuit. Nick's way vastly simplifies the pcb layout, because you don't have a tangled mess of traces to sort out. Then in software, he simply 'mapped' the 16 595 pins to the corresponding 16 7-seg pins. This is actually a pretty powerful principle which can be employed for other things beyond 7-seg displays. It can be a bit confusing, but you only need to do it once. I have some of my own code if you're interested.
Nick Gammon (no relation) is indeed a guru for all things Arduino so I'm not surprised he came up with a novel solution. Great that you extended his idea. The best way of sharing such code / info is via GitHub (like I do), then everyone can see your endeavours.
Do you know how elephants hide in a cherry tree, Ralph? They paint their toes nails red! Have you ever seen an elephant in a cherry tree? No? It works. Great video Ralph. You're on the top of the leaderboard.
Thankyou once again for a clear explanation. As a non programmer, who knows bits about all parts of this stuff, it really ties it together in an ordered and efficient way. Well done!
Wow, I've just read the comments sections from a couple of your videos and I'm absolutely gobsmacked at how some people are arrogant and disrespectful of your work, if they want to view videos for super experts as they claim they are, why make such terrible comments, just say this channel is not for me and move on. Personally although I have been "hobby" coding for over 50 years, Fortran77, Cobol, Basic on the Sinclair Spectrum, MS Basic, Visual Basic etc, but never cpp, I like the level you are aiming at, so please keep doing what you are doing. Now to the issue that made me write this. For me the video stopped one minute early, or perhaps could be the subject of another video. How do I install MY library?, I've done searches of my folders and find arduino and platformio libraries all over the place. I want MY libraries to be global for me so if I update them the new version will be used everywhere. I see the need for version control for third party libraries but not mine. The more I look into it, the more a don't understand, so perhaps a video on libraries how they differ from Arduino ide and platformio (I've converted to platformio) how to use "project" libraries and how to use global libraries (as in Ardunio). Sorry for the long post, get well and stay well. Sev.
Yes, Sev, some comments can be less than constructive, for sure! Onto your question. In your 'sketches' folder is another folder called 'libraries'. In _that_ folder are all your library folders. Create a new library folder there and pop in your library files (.h and .cpp files) and they will be global and found by the Arduino IDE/compiler. So there's no "installation" as such, just a copy process. Ensure you only have the library accessible to the Arduino IDE in ONE place or it will possibly pick up the wrong version.
Yes, it's surprising how quickly we can develop mini libraries just for the sensors/objects we need, to abstract the nitty-gritty from the elegant wrapper code we should be writing. Glad this helped!
Thanks Ralph, I'm an avid follower of all your videos, great work. However, I'm a bit confused about how you arrived at the screen showing 3 tabs open at the top (minute 5:09). How do you actually begin to write your library ? I assume it's not from within the Arduino IDE, as that saves any sketches automatically with .ino extensions ? Confused is me.
Dear confused 😲 Yes, you can indeed use the Arduino IDE. On the top row with the single .ino tab, there is (on the right hand side) a drop down three dots (using Arduino 2.0.2) from which you can select "New Tab". Give it (the new file) a name, such as "MyLibrary.cpp" and it will place it in the same folder as your .ino file. It will also open it when you next open the .ino file too, so helpful! Does this answer your query, Kevin?
Great video :) I've been learning C++, coming from Java. Something that is weird is when you have classes as members of another class, you are forced to create a parameterless constructor. When the class is declared as member it instantiaties. Then the constructor of the class it is in, will have to again call the proper parameterized constructor. Actually had to modify several existing classes adding empty constructors. A bit annoying.
@@RalphBacon Yuck @ Python. Actually Java is really good; very clean code and typed unlike Python. I do like C++ but it's weird having to call empty constructor which leaves class in an unusable state until it is called again with the proper parameterized constructor.
Did the sketch output a DP, I can't remember? All the DP bit does is add an additional "1" bit at the end (LSB) for the 8th segment. I'll check that it's working when I next use it.
Must admit Ralph, this one I'm struggling with and over my head as a newbie to micro electronics. Bookmarked until I'm further along with my 'vertical' learning curve, hehe .... Constructing your own library really makes sense though, and you've produced a uniquely comprehensive, excellent video as always .. 🇬🇧
You can do it! Even if your "library" contained just one function to print "Hello World" then you would have a library. But I can understand that it might be somewhat overwhelming for a self-confessed newbie... all in your own time!
You appear to have a bug in your value checking in updateValue method. When inputting a value to display in hex, you're checking if the value is greater than 256. Should be 255 since 256 is not valid for only 2 hex digits.
I would have written the library as a generic library to drive a 47HC595. In the set up routine I would have data for pins used, display type, non-display just I/O, number of attached devices- so you can link more than one. etc, etc But the important point that should be made is when you update the library the new one should be backwards compatible. I've given up using Arduino libraries due to this. Having to find a version of the library that works with the code you're looking at, as the latest one just does not work. and the author does not reference the version of the library used.
I hear you about backward compatibility (or lack of it). Many of my Arduino sketches failed on recompile after a few years due to this. That's why I use VSC (PlatformIO) so I can use specific versions of a library, if required. Or even keep a snapshot of the actual library with the project. Makes life a lot easier.
Morning Ralph. You may (or probably may not) remember I sub'd to your channel many years ago, and a few years after that I contacted you after a clear out of my overwhelming volume of YT sub'd channels, kept yours in my list. (In case you're curious, I contacted you regarding a central heating control question). Anyway, you are still very much in my list and I just watched this one and must say, very nicely explained sir. Classes are a topic that confuse many people, myself included, and you make the process very clear. Keep up the great work.. PS: I am currently working with M5Stack and LilyGo T-Display devices - both of which you may find extremely useful in your line of work. Currently connecting a T-Embed device with built in rotary encoder via WiFi to my SunSDR2DX Amateur Radio transceiver to control it via the manufacturers TCT Protocol. All super stuff! John (GW3JVB)
Sounds very interesting (not to mention, complex) stuff you're working on, John. And, yes, I do remember you, I'm very happy you're still here on my Arduinite journey! 👍
Thank you very much for that video about libraries. Since I am a somebody who is just applying them, it helps me a lot to better understand how they work. 👍👍👍
Thank you for this very good tuto ! I have a question : just suppose you have 2 models of arduino (mega and esp32 for instance) and 2 types of displays (LCD and OLED for instance). Please, how will you dispatch code between libraries and main file in order to manage all the 4 cases of configuration ?
You would need definitions that determine which part of the library you need. This is done all the time by those that create libraries for 20 different screen for example. So the developer (you & me) would edit one of the files to uncomment the correct screen type definition (eg ILI9341) and within the library header there would be #ifdef statements that only spring into life when a particular variable has been declared. This ensures that you your sketch does not contain all the code for 19 different screen types that you will never use. All the code that does not get exposed as part of the #ifdef statement will never get compiled. Here's a Real World extract from one the libraries I use: // Only define one driver, the other ones must be commented out #define ILI9341_DRIVER //#define ST7735_DRIVER // Define additional parameters below for this display //#define ILI9163_DRIVER // Define additional parameters below for this display //#define S6D02A1_DRIVER //#define RPI_ILI9486_DRIVER // 20MHz maximum SPI //#define HX8357D_DRIVER //#define ILI9481_DRIVER //#define ILI9486_DRIVER //#define ILI9488_DRIVER
Thank you very much for the beautiful explanation. I have a question Can you show a video on the difference between accessing a class through dynamic memory and static memory when we address the class as you wrote and when do we address the class through the word new in arduino And when we write the destructor when we opened dynamic memory in the Arduino program .
A tricky subject at the best of times, Danny. The question I would ask is Why on earth are you worried about Heap vs Stack memory allocation? Using the "new" keyword puts the object on the (larger) heap memory but then you must explicitly delete the object to free up the memory (or you get a memory leak). Using smart pointers goes a long way to prevent this but even so... Not using "new" keyword (the default way, I guess) means the item goes on the stack (limited memory, agreed) but you don't have to manually manage it; it will be automatically removed (the memory freed) when the object or variable goes out of scope (eg a function finishes). Unless you have a _a very good reason_ to use "new", don't. Some good reasons might be: * you have a large object or array that would cause a stack overflow (or otherwise impair the operation of the rest of the code because there is insufficient stack memory * you want a pointer to the object returned (but note my comment about smart pointers) * you need to resize a variable (eg array) and therefore it can't really go on the stack which only allows fixed sized variables (apart from vectors which does all the hard work behind the scenes) Unless you want memory leaks, don't directly call "new" and "delete" from your sketch. Wrap such objects in a class so that the "delete" function is automatically called when it goes out of scope. As I say, a tricky subject at best of times.
@@RalphBacon Thank you very much for your answer, I really appreciate the demonstrations that teach Arduino programming through the programming language C++
On Microcontroller: Dynamic memory bad. Avoid if you don't know what you do. Danger of Memory Fragmentation. (of couse use new and delete if you want to try stuff out but know why stuff can go bad and should be avoided) (at least dynamic memory bad on these small ATmega with only 2KB RAM) but i guess some arduino libraries use dynamic memory to like allocate a buffer and never free the space in the runtime ...
In your other "create library" video you added a keywords.txt file. Is a created keyword in danger of conflicting with the same keyword created by a #include (another abstraction) or will compilers identify and warn about the conflict?
The keywords file doesn't warn you of anything, not even spelling mistakes. 😲 It's just eye-candy for the IDE you are using (but useful eye candy, nonetheless). Compiler ignores it completely. I don't think one list of keywords will conflict with other libraries because the keywords.txt file is in the library folder to which it pertains.
Yes! Today I watched your movie for the first time in the highest resolution, before the playback was hacking and lagging all the time. I got an old written off ThinkPad X1 Carbon 7th i7/16GB from work.
@@RalphBacon I used the Swedish Luxor ABC 80. Ha, ha not. I used an old iMac running Win 7 in Bootcamp. I have used it for many years, 10+, but now it had to retire. The difference is staggering in performance.
I found this video interesting, thanks. I realize this is kind of off-topic but if I was trying to save every byte of memory possible, then for example would you get 8 copies of "_digits" if you had 8 copies of the class or would the linker optimize it? (For say a DRO for CNC where you might need 1 for each of 7 axis + more for other stuff.)
Not off topic at all, actually, Joseph. Yes, in that use case you would repeat everything in the "private" section. However, there are ways round that to ensure each instance uses a "static" version of the "_digits" (known as a singleton) but that's not beginner-friendly stuff. Better for beginners to use a few more bytes and get it working.
I want to thank you for all of your help. I still don't grasp everything yet, but you have explained everything very well. I will just have to watch this a few more times. I just recently bought a esp32-S3 T-Display. I can access it, but it can't find my libraries DIR. I made one major mistake when pasting the git json url in the preferences window. When I pasted the url into the window, I accidentally deleted the previous json urls. Did that make the Arduino forget where my libraries DIR is.. or worse, did it delete the original libraries DIR? it was originally in a path starting with a dot followed by arduino15 Is there a master list of json urls for the arduino IDE preferences window? (board definitions)??
Your Arduino _libraries_ folder usually resides in the same folder as all your sketches. Eg C: > Document > Arduino Sketches > libraries Have a look to see whether you can find it in the trash otherwise, from where you can restore it. The "master list" of json URLs doesn't really exist but mine looks like this: ``` arduino.esp8266.com/stable/package_esp8266com_index.json dan.drown.org/stm32duino/package_STM32duino_index.json digistump.com/package_digistump_index.json drazzy.com/package_drazzy.com_index.json adafruit.github.io/arduino-board-index/package_adafruit_index.json dl.espressif.com/dl/package_esp32_index.json github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json mcudude.github.io/MicroCore/package_MCUdude_MicroCore_index.json mcudude.github.io/MightyCore/package_MCUdude_MightyCore_index.json mcudude.github.io/MiniCore/package_MCUdude_MiniCore_index.json raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json raw.githubusercontent.com/dbuezas/lgt8fx/master/package_lgt8fx_index.json raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json raw.githubusercontent.com/mikaelpatel/Cosa/master/package_cosa_index.json raw.githubusercontent.com/sleemanj/optiboot/master/dists/package_gogo_diy_attiny_index.json raw.githubusercontent.com/sparkfun/Arduino_Boards/master/IDE_Board_Manager/package_sparkfun_index.json ``` There could be some duplicates and experimental devices in that list, so you should be selective!
I have now watched a good number of your videos. Initially, I found your presentation to be a bit distracting (lots of hand motion). However, I eventually came to appreciate the professionalism and quality of the information enough to ignore the hands. 🙂 This one was the icing on the cake and made the decision easy. New Subscriber!
Is the use of classes in Arduino libraries mandatory? Is it possible to use #include files as collections of standalone functions, just like normal beginners? Is this video a trick to lure unsuspecting Arduiners into the wonderful world of OOP? 😁 Excellent presentation, btw. Thanks!
Dang, you discovered my subterfuge! But seriously... The #include file can contain anything that syntactically fits into the place in your sketch (as though you had typed the contents of the #include file right there). So it can include functions that are logically grouped, perhaps. I showed how to organise code better in videos #BB9 and #BB10 which includes this technique. th-cam.com/video/PWMOb85OGY0/w-d-xo.html th-cam.com/video/P0PYqu83ScY/w-d-xo.html So, long answer to a short question: you don't need to include a class in an #include file!
that Bacon, he's a ''slick'' one!! that Bacon, U can use his Technics on about anything! that Bacon, add L&T. & u've got a meal! that Bacon,🖖. cause I'm not genius!🤣
I am still trying to wrap my head around the lack of alternative coding languages and compilers for Arduino thats been out for 10 years now. I hated C and hate C++ as there is a lot of unnecessary fidgeting to write a simple program. There are far better languages out there that would make Arduino accessible, there is no reason why people can't use languages that they are used to, example, I left Delphi C behind in 1995, just like C++ it is cumbersome and pedantic, you have to do a lot of work for such a simple task that it makes Arduino and it's ilk fail the rapid development test. I have been campaigning in the Arduion forums for years, had 7 or 8 accounts deleted by them as they just don't want to listen to good reasoning to make Arduion more accessible, they instead want people to spend hours upon hours of writing redundant elements that should be automatic like an array... JavaScript : jsArray = [1,2,3,4,5,6]; C++ : int cppArray [6] = {1,2,3,4,5,6}; If you suggest that there is a JavaScript (the easiest language to learn) then you get all sorts of comments about how it is not suitable or "Powerful enough"£ when these muppets fail to understand that the front end is only as powerful as the compiler. It is my considered opinion that a JavaScript (not Java) front end and compiler would make Arduino more accessible as more people know JavaScript than C++ I have given up on C++ and I await a more user friendly front end that has a compiler that does all the housekeeping and compiles in to something that the Atmel chip can use. Maybe when Arduino wake up to this fact that they will be making their product more accessible, they will soon see a huge surge in demand for it, they don't seem to see that many people trying to learn C++ struggle because its not an easy language to use and you have to take care of memory allocations, pointers and flushing buffers and mopping up after you're done. Atmel would also do themselves a big favour by putting the whole memory space available for use rather than having to specifically and write special code to access the higher memory spaces, why make things hard?
Hi, thanks for your video but you didn't explain the most important part of the process. I am not very interested in the internals of your class. What I am interested in is learning things like these: 1. Where do you get and put the arduino.h file to include in your class code? And the corresponding library? 2. Once complete, how do you install your library in the Arduino environment so we can use it in our sketches? 3. What really do you install in the Arduino environment? Is it the source code of your class? Is it the compiled version of your code? 4. I see you were using Visual Studio to make your class. Is the compiled code of Visual Studio C++ compiler compatible with Arduino compiler? 5. Why do you code your library in Visual Studio and not in the Arduino editor? Does it have to be like that? You cannot write libraries in the Arduino editor and environment?
Oh no! You must have been sleeping, João! I explained all this in the video. Look: 1. The .h file (and the associated .cpp) are created as new files by using the 3-dots button in the IDE. They are all placed in the same folder automatically as your sketch, whilst you work on them. 2. Once you have it (the library) ready for wider consumption, create a new library folder (ideally of the same name, as I mentioned in the video) in the _libraries_ folder which you will find in your Arduino sketches folder. Arduino Sketches -> libraries -> MyLibrary -> myLibrary.h & myLibrary.cpp 3. Nothing is _installed_ as such. Just the .h and .cpp files in their folder in the libraries folder. Then you just #include in whatever sketch you want! The library is compiled for each sketch that needs it and is included in the final code for the target device. 4. Visual Studio Code (PlatformIO) uses the same gcc-avr compiler as the Arduino IDE (and now both IDEs look more similar, too, since Arduino IDE 2.0.2). Did I miss anything? Let me know! And good luck!
The c++ compiler would call a library something that is compiled into a library and stuff but arduino says: a library is a source code that is programmed in a way that you can put it in the arduino library folder so you can use it as an arduino library.
Of course you are correct Ralph, and I mean to do this some time, but I've just not quite wrapped my head around github yet. In the meantime, here's a googledrive link to some stuff: drive.google.com/drive/folders/1Ba3jRfbYRE5geXDQd1XrxaZvgdlixG03?usp=share_link I've included KiCad files, so everyone can see what I mean about the anatomically correct symbols, and also some demo Arduino code. This is pretty old stuff, but I've just tested it to make sure everything still works. It does. Cheers, Richard
"Anatomically correct", you say? Ha ha! Yes, I see the design goal here but if I might suggest that you could shrink the PCB to less than half its size if you put the chips either _under_ each display or on the other side of this _double-sided_ board? Just a suggestion. And if you use the SMD version of the PCB pin headers I showed then you could put the pins on the "other side" two too, and thus have a very clean board with little more than the display visible on one side. Thanks for sharing this, I like the code style, very clean, well documented too.
@@RalphBacon Thanks and yes, of course. This was a long time ago and was just a proof of concept even then. The reason for using DIP components btw, was to place sockets on the board and swap out common anode for cathode, to prove that this worked. But today I'd use SMD and probably place the ICs on the other side of the board. I'd also add transistors and not drive the displays directly from the mcu.
This is amazing as about 10 days ago I was curious about making an Arduino library for a Modbus esp32 project board with so - so how to information to find and get working. "For Me?" For the learning curve this video will help: $23 LILYGO T-CAN485 ESP32 Development Board CAN Modbus RTU RS485 IoT Engineer Control Module with cellphone free RemoteXY. 😎 Thanks a lot.
A long time ago I was playing around with 7-seg displays and 595 chips to drive them. I came across something that Nick Gammon had done, which seemed rather brilliant. Nick had a (multiplexed) design for driving a 3-digit display. I extended this to 8-digit displays. Basically:
There are (up to) 16 pins on the 7-seg displays; 8 for the digits and 8 for the segments. And on 2x595s there are 16 parallel output pins. So, what Nick did in his schematic, was to use what I refer to as 'anatomically correct' device symbols, (where the symbol pin positions match the physical device.) He placed these in his schematic and basically drew the shortest connections between pins. Brilliant. Most people would designate one of the 595s for 'digits' and the other for 'segments', (and probably also map them in order, e.g. data pin 0-7 to seg A-G+DP etc.) But it really doesn't make any difference to the circuit. Nick's way vastly simplifies the pcb layout, because you don't have a tangled mess of traces to sort out. Then in software, he simply 'mapped' the 16 595 pins to the corresponding 16 7-seg pins.
This is actually a pretty powerful principle which can be employed for other things beyond 7-seg displays. It can be a bit confusing, but you only need to do it once.
I have some of my own code if you're interested.
Nick Gammon (no relation) is indeed a guru for all things Arduino so I'm not surprised he came up with a novel solution. Great that you extended his idea.
The best way of sharing such code / info is via GitHub (like I do), then everyone can see your endeavours.
Just wrote my first library after seeing this video a few times. Never understood it before, but now it starts to make sense. Thank Mr Bacon.
I'm delighted you did this! Congrats! Start small and then you can increase the complexity of your library (class). 👍
Just found your channel and binging the content! Great work
Yes, there's lots here so we'll see you again, as an expert, in about 6 months?
Do you know how elephants hide in a cherry tree, Ralph? They paint their toes nails red! Have you ever seen an elephant in a cherry tree? No? It works. Great video Ralph. You're on the top of the leaderboard.
And on the topic of OOP code, how do you eat an OOP elephant? One byte at a time, of course. 😁
Thankyou once again for a clear explanation.
As a non programmer, who knows bits about all parts of this stuff, it really ties it together in an ordered and efficient way. Well done!
Glad it's helping you!
This is very helpful. Just what I was looking for. Thanks for the clear explanation Ralph.
You're very welcome!
Always had trouble understanding C++ objects they now make a lot more sense. Many Thanks Ralph
Objects are shrouded in mystery but for no good reason.
Excellent Ralph, I have been wanting to get more familiar with libraries but do not know C++. This is a very good starting point though.
Glad you are finding it useful.
Wow, I've just read the comments sections from a couple of your videos and I'm absolutely gobsmacked at how some people are arrogant and disrespectful of your work, if they want to view videos for super experts as they claim they are, why make such terrible comments, just say this channel is not for me and move on. Personally although I have been "hobby" coding for over 50 years, Fortran77, Cobol, Basic on the Sinclair Spectrum, MS Basic, Visual Basic etc, but never cpp, I like the level you are aiming at, so please keep doing what you are doing. Now to the issue that made me write this. For me the video stopped one minute early, or perhaps could be the subject of another video. How do I install MY library?, I've done searches of my folders and find arduino and platformio libraries all over the place. I want MY libraries to be global for me so if I update them the new version will be used everywhere. I see the need for version control for third party libraries but not mine. The more I look into it, the more a don't understand, so perhaps a video on libraries how they differ from Arduino ide and platformio (I've converted to platformio) how to use "project" libraries and how to use global libraries (as in Ardunio). Sorry for the long post, get well and stay well. Sev.
Yes, Sev, some comments can be less than constructive, for sure!
Onto your question. In your 'sketches' folder is another folder called 'libraries'. In _that_ folder are all your library folders. Create a new library folder there and pop in your library files (.h and .cpp files) and they will be global and found by the Arduino IDE/compiler.
So there's no "installation" as such, just a copy process. Ensure you only have the library accessible to the Arduino IDE in ONE place or it will possibly pick up the wrong version.
Great intro... for particular detail but also for general understanding and building (my) own library. My screen name gives away why I think so.
Yes, it's surprising how quickly we can develop mini libraries just for the sensors/objects we need, to abstract the nitty-gritty from the elegant wrapper code we should be writing. Glad this helped!
Great explanation Ralph, I will need to run through this a few times !...cheers.
Glad it was helpful!
Friday is my "neardy!" day...always look for your video... Thank you!
Yay! Thank you! Hang on, you saying I'm a nerd, Pierre?
@@RalphBacon I would not say something to offend you of course that is why I said "my nerdy day"😀
Thanks Ralph, I'm an avid follower of all your videos, great work. However, I'm a bit confused about how you arrived at the screen showing 3 tabs open at the top (minute 5:09).
How do you actually begin to write your library ? I assume it's not from within the Arduino IDE, as that saves any sketches automatically with .ino extensions ? Confused is me.
Dear confused 😲 Yes, you can indeed use the Arduino IDE.
On the top row with the single .ino tab, there is (on the right hand side) a drop down three dots (using Arduino 2.0.2) from which you can select "New Tab".
Give it (the new file) a name, such as "MyLibrary.cpp" and it will place it in the same folder as your .ino file. It will also open it when you next open the .ino file too, so helpful!
Does this answer your query, Kevin?
@@RalphBacon Thanks Ralph. A brilliant and concise answer providing immediate clarity. I never looked behind those 3 dots before.
Great video :) I've been learning C++, coming from Java. Something that is weird is when you have classes as members of another class, you are forced to create a parameterless constructor. When the class is declared as member it instantiaties. Then the constructor of the class it is in, will have to again call the proper parameterized constructor. Actually had to modify several existing classes adding empty constructors. A bit annoying.
You mean Java is no longer the blue-eyed boy on the block? 😲😁 You'll be coding in Python next 🤮
@@RalphBacon Yuck @ Python. Actually Java is really good; very clean code and typed unlike Python. I do like C++ but it's weird having to call empty constructor which leaves class in an unusable state until it is called again with the proper parameterized constructor.
Very well done!
But: where is the decimal point when running the sketch?
Did the sketch output a DP, I can't remember? All the DP bit does is add an additional "1" bit at the end (LSB) for the 8th segment. I'll check that it's working when I next use it.
Instructive as always Ralph! Thank you kindly!
Glad it was helpful!
Classes and arduino libraries are beginning to make sense
I'm glad, because the power of object oriented programming cannot be underestimated. But start with baby steps first!
Must admit Ralph, this one I'm struggling with and over my head as a newbie to micro electronics. Bookmarked until I'm further along with my 'vertical' learning curve, hehe .... Constructing your own library really makes sense though, and you've produced a uniquely comprehensive, excellent video as always .. 🇬🇧
You can do it! Even if your "library" contained just one function to print "Hello World" then you would have a library. But I can understand that it might be somewhat overwhelming for a self-confessed newbie... all in your own time!
You appear to have a bug in your value checking in updateValue method.
When inputting a value to display in hex, you're checking if the value is greater than 256. Should be 255 since 256 is not valid for only 2 hex digits.
Really?!? Obvs a typo and I hope I haven't confused anyone! Well spotted!
I would have written the library as a generic library to drive a 47HC595.
In the set up routine I would have data for pins used, display type, non-display just I/O, number of attached devices- so you can link more than one. etc, etc
But the important point that should be made is when you update the library the new one should be backwards compatible.
I've given up using Arduino libraries due to this. Having to find a version of the library that works with the code you're looking at, as the latest one just does not work. and the author does not reference the version of the library used.
I hear you about backward compatibility (or lack of it). Many of my Arduino sketches failed on recompile after a few years due to this.
That's why I use VSC (PlatformIO) so I can use specific versions of a library, if required. Or even keep a snapshot of the actual library with the project. Makes life a lot easier.
@@RalphBacon How do you add a library that you have created to VSC / PlatformIO?
A very useful demo thank you for that.
Glad it was helpful!
Morning Ralph. You may (or probably may not) remember I sub'd to your channel many years ago, and a few years after that I contacted you after a clear out of my overwhelming volume of YT sub'd channels, kept yours in my list. (In case you're curious, I contacted you regarding a central heating control question). Anyway, you are still very much in my list and I just watched this one and must say, very nicely explained sir. Classes are a topic that confuse many people, myself included, and you make the process very clear. Keep up the great work.. PS: I am currently working with M5Stack and LilyGo T-Display devices - both of which you may find extremely useful in your line of work. Currently connecting a T-Embed device with built in rotary encoder via WiFi to my SunSDR2DX Amateur Radio transceiver to control it via the manufacturers TCT Protocol. All super stuff! John (GW3JVB)
Sounds very interesting (not to mention, complex) stuff you're working on, John. And, yes, I do remember you, I'm very happy you're still here on my Arduinite journey! 👍
Thank you very much for that video about libraries. Since I am a somebody who is just applying them, it helps me a lot to better understand how they work. 👍👍👍
You're very welcome!
Thank you for this very good tuto !
I have a question : just suppose you have 2 models of arduino (mega and esp32 for instance) and 2 types of displays (LCD and OLED for instance). Please, how will you dispatch code between libraries and main file in order to manage all the 4 cases of configuration ?
You would need definitions that determine which part of the library you need. This is done all the time by those that create libraries for 20 different screen for example.
So the developer (you & me) would edit one of the files to uncomment the correct screen type definition (eg ILI9341) and within the library header there would be #ifdef statements that only spring into life when a particular variable has been declared.
This ensures that you your sketch does not contain all the code for 19 different screen types that you will never use. All the code that does not get exposed as part of the #ifdef statement will never get compiled.
Here's a Real World extract from one the libraries I use:
// Only define one driver, the other ones must be commented out
#define ILI9341_DRIVER
//#define ST7735_DRIVER // Define additional parameters below for this display
//#define ILI9163_DRIVER // Define additional parameters below for this display
//#define S6D02A1_DRIVER
//#define RPI_ILI9486_DRIVER // 20MHz maximum SPI
//#define HX8357D_DRIVER
//#define ILI9481_DRIVER
//#define ILI9486_DRIVER
//#define ILI9488_DRIVER
Thank you very much for the beautiful explanation. I have a question Can you show a video on the difference between accessing a class through dynamic memory and static memory when we address the class as you wrote and when do we address the class through the word new in arduino And when we write the destructor when we opened dynamic memory in the Arduino program .
A tricky subject at the best of times, Danny. The question I would ask is Why on earth are you worried about Heap vs Stack memory allocation?
Using the "new" keyword puts the object on the (larger) heap memory but then you must explicitly delete the object to free up the memory (or you get a memory leak). Using smart pointers goes a long way to prevent this but even so...
Not using "new" keyword (the default way, I guess) means the item goes on the stack (limited memory, agreed) but you don't have to manually manage it; it will be automatically removed (the memory freed) when the object or variable goes out of scope (eg a function finishes).
Unless you have a _a very good reason_ to use "new", don't. Some good reasons might be:
* you have a large object or array that would cause a stack overflow (or otherwise impair the operation of the rest of the code because there is insufficient stack memory
* you want a pointer to the object returned (but note my comment about smart pointers)
* you need to resize a variable (eg array) and therefore it can't really go on the stack which only allows fixed sized variables (apart from vectors which does all the hard work behind the scenes)
Unless you want memory leaks, don't directly call "new" and "delete" from your sketch. Wrap such objects in a class so that the "delete" function is automatically called when it goes out of scope.
As I say, a tricky subject at best of times.
@@RalphBacon Thank you very much for your answer, I really appreciate the demonstrations that teach Arduino programming through the programming language C++
On Microcontroller: Dynamic memory bad. Avoid if you don't know what you do. Danger of Memory Fragmentation.
(of couse use new and delete if you want to try stuff out but know why stuff can go bad and should be avoided)
(at least dynamic memory bad on these small ATmega with only 2KB RAM) but i guess some arduino libraries use dynamic memory to like allocate a buffer and never free the space in the runtime ...
Think function overloading using a template or literals for each port to speed up the code a little would work?
Function overloading won't _speed up_ the code, but it would make it easier for a developer (me & you) to just pick a named function and use it.
@@RalphBacon haha I'm but a grasshopper my friend. I've never written production code. I just read alot of books and make silly apps haha
In your other "create library" video you added a keywords.txt file. Is a created keyword in danger of conflicting with the same keyword created by a #include (another abstraction) or will compilers identify and warn about the conflict?
The keywords file doesn't warn you of anything, not even spelling mistakes. 😲
It's just eye-candy for the IDE you are using (but useful eye candy, nonetheless). Compiler ignores it completely.
I don't think one list of keywords will conflict with other libraries because the keywords.txt file is in the library folder to which it pertains.
Totally productive and useful video. Thank you.
Glad you enjoyed it!
Another Great video, I've wanted to look into to libraries and this was a great starter. Thanks.
Glad you enjoyed it!
Yes! Today I watched your movie for the first time in the highest resolution, before the playback was hacking and lagging all the time. I got an old written off ThinkPad X1 Carbon 7th i7/16GB from work.
It's "only" HD not 4K! What were you using before, a Sinclair Spectrum? 😲😁🤣 But I'm happy you can now see it in full definition.
@@RalphBacon I used the Swedish Luxor ABC 80. Ha, ha not. I used an old iMac running Win 7 in Bootcamp. I have used it for many years, 10+, but now it had to retire. The difference is staggering in performance.
I found this video interesting, thanks.
I realize this is kind of off-topic but if I was trying to save every byte of memory possible, then for example would you get 8 copies of "_digits" if you had 8 copies of the class or would the linker optimize it? (For say a DRO for CNC where you might need 1 for each of 7 axis + more for other stuff.)
Not off topic at all, actually, Joseph.
Yes, in that use case you would repeat everything in the "private" section. However, there are ways round that to ensure each instance uses a "static" version of the "_digits" (known as a singleton) but that's not beginner-friendly stuff.
Better for beginners to use a few more bytes and get it working.
great explanations thank you
Glad you found it useful! 👍
Thanks for the interesting topic on libraries, got me thinking how to change my project.
Glad it was helpful! Deffo worth experimenting with a simple library first and see how you get on (spoiler: brilliantly).
I want to thank you for all of your help. I still don't grasp everything yet, but you have explained everything very well. I will just have to watch this a few more times.
I just recently bought a esp32-S3 T-Display. I can access it, but it can't find my libraries DIR.
I made one major mistake when pasting the git json url in the preferences window.
When I pasted the url into the window, I accidentally deleted the previous json urls.
Did that make the Arduino forget where my libraries DIR is.. or worse, did it delete the original libraries DIR? it was originally in a path starting with a dot followed by arduino15
Is there a master list of json urls for the arduino IDE preferences window?
(board definitions)??
Your Arduino _libraries_ folder usually resides in the same folder as all your sketches. Eg C: > Document > Arduino Sketches > libraries
Have a look to see whether you can find it in the trash otherwise, from where you can restore it.
The "master list" of json URLs doesn't really exist but mine looks like this:
```
arduino.esp8266.com/stable/package_esp8266com_index.json
dan.drown.org/stm32duino/package_STM32duino_index.json
digistump.com/package_digistump_index.json
drazzy.com/package_drazzy.com_index.json
adafruit.github.io/arduino-board-index/package_adafruit_index.json
dl.espressif.com/dl/package_esp32_index.json
github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
mcudude.github.io/MicroCore/package_MCUdude_MicroCore_index.json
mcudude.github.io/MightyCore/package_MCUdude_MightyCore_index.json
mcudude.github.io/MiniCore/package_MCUdude_MiniCore_index.json
raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json
raw.githubusercontent.com/dbuezas/lgt8fx/master/package_lgt8fx_index.json
raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
raw.githubusercontent.com/mikaelpatel/Cosa/master/package_cosa_index.json
raw.githubusercontent.com/sleemanj/optiboot/master/dists/package_gogo_diy_attiny_index.json
raw.githubusercontent.com/sparkfun/Arduino_Boards/master/IDE_Board_Manager/package_sparkfun_index.json
```
There could be some duplicates and experimental devices in that list, so you should be selective!
You explain it really well thank you very much ! 👍
You are welcome!
Thanks Ralph!
Nice explanation...
Glad you liked it!
Your lessons are always the best!
Wow, thanks, Jim!
I got into c some years back, never got into c++. Perhaps it's time to give it a go...
If you know C you can migrate to C++ because it still allows a lot of C constructs and methods.
I have now watched a good number of your videos. Initially, I found your presentation to be a bit distracting (lots of hand motion). However, I eventually came to appreciate the professionalism and quality of the information enough to ignore the hands. 🙂
This one was the icing on the cake and made the decision easy. New Subscriber!
Thanks for the sub, Basil. 👍I'll try and watch (and control) the hand waving in future (I just get carried away 😁).
Hey mr. Bacon, thank you so much for this video and for explaining how to build a library, much appreciated
You're very welcome!
Very hoopie-cool, Ralph!
An Arduino that's millennia faster than Deep Thought! 😎
Indeed it is!
Can you please consider doing a few Rust on Arduino videos.
The question I have to ask is, " How many of my viewers program in Rust? On an Arduino? Or want to?" I'm pretty sure it must be very low.
@@RalphBacon I agree. Its very interesting but not that popular under Arduino users.
Very nice explanation thank you.
Hey, Max, Merry Christmas! 🧑🎄Thanks for your kind words!
I find it funny when at the end of your sponsorship you say "try them out now." Well then if they do that, they'll have to stop watching the video!
I will have to change that to say "Try them out soon!" 😁
This made me consider re-writing large chunks of my line follower robot code as a robot library. Hmmm!
Yes, do it. Perhaps more than one library (separation of concerns). 👍
Very useful !!. Thanks a lot!
Glad it was helpful!
Yoo, b-word!
Glad to see Mr White moved on from methamphetamine production to embedded programming
Well, [re-reads post for umpteenth time trying to understand it], yes, absolutely! 😁😉😲
Is the use of classes in Arduino libraries mandatory? Is it possible to use #include files as collections of standalone functions, just like normal beginners? Is this video a trick to lure unsuspecting Arduiners into the wonderful world of OOP? 😁
Excellent presentation, btw. Thanks!
Dang, you discovered my subterfuge! But seriously...
The #include file can contain anything that syntactically fits into the place in your sketch (as though you had typed the contents of the #include file right there).
So it can include functions that are logically grouped, perhaps. I showed how to organise code better in videos #BB9 and #BB10 which includes this technique.
th-cam.com/video/PWMOb85OGY0/w-d-xo.html
th-cam.com/video/P0PYqu83ScY/w-d-xo.html
So, long answer to a short question: you don't need to include a class in an #include file!
REALLY helpful. Liked and subscribed.
Awesome, thank you!
Good work, thanks man.
You're welcome!
Thanks Ralph.
You're most welcome!
thank you again Mr. Bacon, luv your face.
My old face, I think you mean, Bill. 👴
Thank you
You're welcome!
Very good, thanks
My pleasure!
I was with you when you said "yes Indeed" after that it all went OOP's 😟
That's because you're just an old Woofer Fred. ;-)
@@TheEmbeddedHobbyist Meow!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
So you got past "Welcome Back" and "Yes, indeed". 🎉🎊🎈 I suppose the "instantiation of a blueprint class into an object" was just one step too far? 😟
@@RalphBacon damn big words as well very Perspicacious of you 🙂
maybe Bambam can explain it to you ...
after unbearable thinking , i found a name to propose for the device , "the debugun" (the debugging has began )
Wow! You should definitely patent that name, quickly!
Amazing
Thank you! Cheers!
that Bacon, he's a ''slick'' one!! that Bacon, U can use his Technics on about anything! that Bacon, add L&T. & u've got a meal!
that Bacon,🖖. cause I'm not genius!🤣
You got that right! Mmm, BLT (especially when the Bacon is sizzling) 🥓😋
I am still trying to wrap my head around the lack of alternative coding languages and compilers for Arduino thats been out for 10 years now.
I hated C and hate C++ as there is a lot of unnecessary fidgeting to write a simple program. There are far better languages out there that would make Arduino accessible, there is no reason why people can't use languages that they are used to, example, I left Delphi C behind in 1995, just like C++ it is cumbersome and pedantic, you have to do a lot of work for such a simple task that it makes Arduino and it's ilk fail the rapid development test.
I have been campaigning in the Arduion forums for years, had 7 or 8 accounts deleted by them as they just don't want to listen to good reasoning to make Arduion more accessible, they instead want people to spend hours upon hours of writing redundant elements that should be automatic like an array...
JavaScript :
jsArray = [1,2,3,4,5,6];
C++ :
int cppArray [6] = {1,2,3,4,5,6};
If you suggest that there is a JavaScript (the easiest language to learn) then you get all sorts of comments about how it is not suitable or "Powerful enough"£ when these muppets fail to understand that the front end is only as powerful as the compiler.
It is my considered opinion that a JavaScript (not Java) front end and compiler would make Arduino more accessible as more people know JavaScript than C++
I have given up on C++ and I await a more user friendly front end that has a compiler that does all the housekeeping and compiles in to something that the Atmel chip can use. Maybe when Arduino wake up to this fact that they will be making their product more accessible, they will soon see a huge surge in demand for it, they don't seem to see that many people trying to learn C++ struggle because its not an easy language to use and you have to take care of memory allocations, pointers and flushing buffers and mopping up after you're done.
Atmel would also do themselves a big favour by putting the whole memory space available for use rather than having to specifically and write special code to access the higher memory spaces, why make things hard?
In which case you'll be pleasantly surprised that the Arduino IDE now supports microPython, albeit in the early stages yet. See bit.ly/3VzTRu5
Arduino with ATMega328p: 2KB RAM ... but you're welcome to write a python interpeter for it. cause i like python, too ...
And everybody agrees with me that if i say: "javascript sucks" that it is not a number.
th-cam.com/video/qvFG8J7SUDE/w-d-xo.html
@@RalphBacon It's kinda limited (as opposed to CPython in Linux or Windows).
Hi, thanks for your video but you didn't explain the most important part of the process. I am not very interested in the internals of your class. What I am interested in is learning things like these:
1. Where do you get and put the arduino.h file to include in your class code? And the corresponding library?
2. Once complete, how do you install your library in the Arduino environment so we can use it in our sketches?
3. What really do you install in the Arduino environment? Is it the source code of your class? Is it the compiled version of your code?
4. I see you were using Visual Studio to make your class. Is the compiled code of Visual Studio C++ compiler compatible with Arduino compiler?
5. Why do you code your library in Visual Studio and not in the Arduino editor? Does it have to be like that? You cannot write libraries in the Arduino editor and environment?
Oh no! You must have been sleeping, João! I explained all this in the video. Look:
1. The .h file (and the associated .cpp) are created as new files by using the 3-dots button in the IDE. They are all placed in the same folder automatically as your sketch, whilst you work on them.
2. Once you have it (the library) ready for wider consumption, create a new library folder (ideally of the same name, as I mentioned in the video) in the _libraries_ folder which you will find in your Arduino sketches folder.
Arduino Sketches -> libraries -> MyLibrary -> myLibrary.h & myLibrary.cpp
3. Nothing is _installed_ as such. Just the .h and .cpp files in their folder in the libraries folder. Then you just #include in whatever sketch you want! The library is compiled for each sketch that needs it and is included in the final code for the target device.
4. Visual Studio Code (PlatformIO) uses the same gcc-avr compiler as the Arduino IDE (and now both IDEs look more similar, too, since Arduino IDE 2.0.2).
Did I miss anything? Let me know! And good luck!
@@RalphBacon Sorry for being asleep......
Meanwhile I discovered another video of yours where you explain all that.
Thanks a lot for the help!
The c++ compiler would call a library something that is compiled into a library and stuff but arduino says: a library is a source code that is programmed in a way that you can put it in the arduino library folder so you can use it as an arduino library.
🥰🥰🥰
First again, like a coiled spring, Yogesh!
I like Bacon.... Thanks Ralph
with Eggs of course 🙂
You are welcome RFdave!
My brain hurts.
Funnily enough so does mine. I blame that bottle of Scotch from last night, what's your excuse? 😁
@@RalphBacon My lack of neurons to remember things of importance. And Ballantine's.
Of course you are correct Ralph, and I mean to do this some time, but I've just not quite wrapped my head around github yet. In the meantime, here's a googledrive link to some stuff:
drive.google.com/drive/folders/1Ba3jRfbYRE5geXDQd1XrxaZvgdlixG03?usp=share_link
I've included KiCad files, so everyone can see what I mean about the anatomically correct symbols, and also some demo Arduino code. This is pretty old stuff, but I've just tested it to make sure everything still works. It does.
Cheers, Richard
"Anatomically correct", you say?
Ha ha! Yes, I see the design goal here but if I might suggest that you could shrink the PCB to less than half its size if you put the chips either _under_ each display or on the other side of this _double-sided_ board? Just a suggestion.
And if you use the SMD version of the PCB pin headers I showed then you could put the pins on the "other side" two too, and thus have a very clean board with little more than the display visible on one side.
Thanks for sharing this, I like the code style, very clean, well documented too.
@@RalphBacon Thanks and yes, of course. This was a long time ago and was just a proof of concept even then. The reason for using DIP components btw, was to place sockets on the board and swap out common anode for cathode, to prove that this worked. But today I'd use SMD and probably place the ICs on the other side of the board. I'd also add transistors and not drive the displays directly from the mcu.
This is amazing as about 10 days ago I was curious about making an Arduino library for a Modbus esp32 project board with so - so how to information to find and get working. "For Me?" For the learning curve this video will help: $23 LILYGO T-CAN485 ESP32 Development Board CAN Modbus RTU RS485 IoT Engineer Control Module with cellphone free RemoteXY. 😎 Thanks a lot.
I'm glad it arrived at the right time for you!
@@RalphBacon Thanks.
Thanks Ralph.
Most welcome!