Optimizing Arduino Code: no setup(), no loop() ⛔

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
can you write your Arduino code without using setup and look at all yes this is possible in this video I'm going to show you how and more importantly why are you ready this is the Arduino standard blink program let's run it and you probably recognize this it's just linking this LED connected to pin 13 and if we look at the statistics we can see that this program uses almost 1000 bytes of memory now Arduino doesn't have a very big program storage memory and this is already 2% of the Arduino program memory just for something simple as blinking an LED so let's see how we can make it better first of all let's take a note of the program memory size so we had 9 to 4 bytes that's 2% and we also used 9 bytes of dynamic memory even though we haven't declared any variables here and that's because the Arduino library itself uses some internal variables to keep track of things like the time that passes so how can we improve this first of all we can replace this cost to pin mode and digital write with different code that manipulates those pins directly we can do this by looking at the Arduino reference in this sport manipulation page it tells us that we have port B that maps to Arduino digital pin 8 to 13 and it's controlled by three registers now we'll probably go deeper into this in another video but right now let me just show you the code that will turn this LED on without calling pin mode and digital right so that would be first of all we want to set DDR B to all ones there are a couple of ways to do this and we'll probably cover this hexadecimal notation in another video but for now just bear with me and remember that this is the equivalent of setting all the pins from 8 to 13 to output the second thing we want to do is to turn this led on so we can do it again with those port registers we can write port B equals FF again to set all the pins from a 230 into high and then we can set it to zero to clear all those pins and to turn the led off so this code isn't exactly equivalent of what we had before because it toggles multiple pins at once but let's give it a run and we can see that the LED still blinks but now the program is a little bit smaller like third of the size almost third of the size is gone and again let's take a note of the new memory size so that would be 6 4 6 by it's still 2% and we are still using 9 bytes of dynamic memory so that's a bit better but let me show you how we can do even better it turns out that in Arduino code you don't really need to write the set up and the loop functions if you take a look at the Arduino library code we can see that it declares a function called main and this function actually calls set up and look and also cause some other functions that are probably internal to the Arduino library and initializes things like the timers and all the other hardware in the Arduino so instead of using set up and loop we can just make our own main that will be run instead of this main function from the Arduino standard library let's do this so this is what we are going to do we are basically going to remove setup replace it with main and then take everything that we had in the loop and move it to main and run it with an infinite loop so while true run all these commands that we had in the loop ok and I think we are ready to test this new program now we'll have no setup no loop just one main that should do the same and if we run the code we can see that now the program is even smaller it just uses a bit less than 500 bytes but you might have not duh there is another problem the LED doesn't blink anymore and I will explain why this happens in a second but first let's do some bookkeeping and write the new size so it's still using nine bytes of dynamic memory and now we can answer the question why did the LEDs stop blinking like it's it turns on but it no longer blinks and the reason for that is that we created our own main so the Arduino library realization code didn't run and that causes delay to stop working because delay needs some timers that the Arduino library sets up and since we didn't call the Arduino library code it didn't set up those timers um what we are going to do to fix this is replacing delay with a simple for loop that will just do nothing and wait for the time to pass so we can do that by writing something like for long I from 0 to let's say big enough number I plus plus and basically do nothing now if we run this code let's run it and we can see that now the LED the LED is a little bit dimmer it still doesn't blink and we'll get to that in a second but we can see that now my our program is so much smaller it only uses 142 bytes and it also stopped using the dynamic memory so let's write this down so now we are only using 142 pi it's so dot zero percent and no dynamic memory that's a great saving and the reason we managed to reduce our program size so much is because we no longer use the delay function so the Arduino standard library is no longer compiled in whenever you use the delay function it also uses the Milly's functions and the other functions internally and now that we are known we're using it all this code is gone it's no longer inside our program so now when we run the program we can see that the LED hands-on but its Demers so basically it's turning on and off very rapidly and probably our delays are not working and the reason for that is that the compiler see that this code doesn't do anything it just a for loop that doesn't do anything so it basically optimizes the code and remove this code it doesn't really run this code and here is a little trick that we can do to make the compiler keep this code instead of writing do-nothing which is a comment and the compiler can't read that we can use a spatial construct that tells the compiler hey I want to keep this code in and we are doing it by call writing ASM and giving it an empty string now I'm not going to go into what ASM is and what it does but what you should know is that whenever you write this code the compiler can no longer reason about what is follow grid is doing and for this reason it won't optimize and remove it from the code let's try this oh wow so now the LED is blinking the program itself is now a bit bigger 176 and that's exactly because of the reason I told you before that these four loops were optimized were removed from the program so it was smaller and now they are in the program so it's a bit bigger but still much smaller than it was before there is another cool optimization we can do instead of writing to port B we can write to a different register called pin B and in this register toggles the pins of this port so instead of like telling it go hide and go low we just tell it toggle wait toggle and then wait and if we run this code we can now say that it still does the same but the program is smaller 158 bytes only so we have the same result with a much much smaller program so let's write it down so our final result is 158 by and that's how we reduce it to sixth of the size by removing set up removing loop and replacing delay with this do-nothing loop so that was all for today we covered some pretty advanced topics in Arduino programming so if you have any questions let me know in the comments and I will try to make videos that answer your questions and you can find all the links that I mentioned as well as the code from this video in the description below so until the next time see you and bye bye
Info
Channel: Wokwi
Views: 29,340
Rating: 4.9229479 out of 5
Keywords:
Id: -XPrSScamXc
Channel Id: undefined
Length: 9min 26sec (566 seconds)
Published: Sat Apr 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.