Fun with Arduino 04 Readable & Maintainable Code , #define

We come to write the code for that. This is the hardware that we need to try it out. We have a push button connected to pin 2, no special reason why this pin 2 but just have to select a pin, and then we have a LED and a resistor connected to pin 8. If you do not yet have a push button, you can simply use a wire that you connect manually to simulate the push button and if you dont, have a full range of resistors at home, yet any other value close to 330 will do as long as the Led lights up so that we can see it work well, we can use the code that we prepared in video 2 and we had the internal the onboard led on which is hardwired to pin 13 and we use the push button to switch that on and off. So, actually, if we would change 13 to 8, the new pin number for our external LED, then we would be already but thats, not what we are going to do. Why not because this code, already with this very short code, we need to make a change at three locations now suppose that we have an Arduino thats doing a lot of stuff hundred lines of code, and then we would like to make a change of a pin. Number then we would have to scan through all that code to find all the lines where we need to make that change, and that is what quality, Diaz and error prone.

We could easily forget one. So the goal for this little video here is that we are going to write more readable and more maintainable code and then the second goal is we are going to play as if that one LED is not street lights on our model railway layout. So its probably not one led Madrid, maybe 20 or 40, even in the next video were going to see how to connect those, but right now were going to simulate a night cycle. If we press the button, then the street lights switch on and automatically after say. Three minutes they switch off again, okay. So the defining statement is a very nice statement that we can use for our first goal: readable and maintainable code. What does it do? It simply exchanges one text with another text, so if I would write define push button, pin two then everywhere in my code, where I use the word push button pin the Arduino will read, and that is a very convenient to make maintainable code and readable code. So lets do that this is the code that we prepared in video number two and instead of going to change the thirteens into AIDS, we are going to add a couple of defines to exist code more readable. Let me do that. We had a push button and use a underscore with that. That is one of the conventions in code world. It was on pain too, and I did not need to make another space.

Another define is for the street lights on my model railway layout, and that was well. We are now at pin 13. So let me just change this code. With these new defines first and then see how maintainable it is everywhere. In my code, man, it said previously been – I am going to change that now, with the readable word, push button and everywhere it said 13 Im going to change that into this readable, word street light, pin and see what happens. My code is readable by itself. So why would I use these commands? We are only repeating the obvious I can do with much less commands in this code because yeah, my code itself already tells what these things are. The code is obviously a few lines longer, but the big pluses its readable – and it is maintainable because see what happens now. If I want to make this change, pin 13 has to become pin 8. All I need to do now is change my define and in one go Im ready. That is very nice. Of course, why have I written this street light, pin and push button pin in all capitals? Well, that is just one of those code. Conventions or style guard confirm between, say C programmers theres a whole a whole range of those conventions, and, if maybe you are more interested in reading them, follow the links that are in the text that goes with this video. Okay, now lets go to our second goal: how to have the lights stay on for three minutes? Well, obviously, we need to change something here in our if statement.

Let me first take this one line down and then proper coding and use some indents, and I can take away the else, because I want a couple of things to happen. When I push or if I push the button, then I want the lights to go. Hi. Yes and then I want these three minutes to happen. A quick and dirty way to do that is use, delay statement. The delay statement works with milliseconds. Three minutes is three times: 60 is 180 seconds. 180 seconds is 180 thousand milliseconds, so this looks good, but with an if statement followed by multiple lines of code, we will need an extra set of these curled brackets. So let me add those two and then we should have the code that works. So lets read it. If the push button goes low, yeah it was attached to ground. Then my first switch on the lights. Then I simply do nothing for three minutes and then I switch off the lights well, this should work one more thing that I like to do. I do not like this 180 thousand over here. I I want to have the another define here that says street lights. All time yeah its a long road, but why not its good readable? And that is the text that I am simply going to adhere to my define so that I can change it easily copy paste, yeah, and that was one eight zero thousand. So now I have a configurable time.

If I want to change this time, I can easily do it well, I want to make the first change right now already, because I am going to test this code and I dont want to wait for 3 minutes during my testing. I only want to wait for say 3 seconds how much is three seconds: three thousand milliseconds. Oh, this is one where I could use a text to take a command to explain what is happening here. The units are a millisecond, so 3 minutes, Im going to say is one eight zero thousand. So then anyone knows that this was happening in this line. This is a readable code, its maintainable code and lets now test. Okay, its time to try it out Im going to press here this upload, an icon in the top left and then in the bottom right. We see the progress bar and on the Arduino, we should see the lights blinking that the code is being uploaded. Well, its finished over here I have again my bump destination device push button, and over here we have an red LED connected. So if I press the button, my night cycle starts yet the LED lit up, and it does that for exactly three seconds so that works. Quite fine. Now lets make a quick change Im going to change three thousand into 6000. That is a one second operation Im going to upload new code again, and we will see the lights on the Arduino blink again.

The code is there now Im going to press the button again and the LED this one over here should light up for six seconds. That was quite a fast change. This is maintainable code. Well, this was it now, of course, the question is: how can I get not only one LED connected to pin 8, but I have 20 straight lines with powerful white LEDs in LEDs in them.

Share.
Exit mobile version