Best Practice for Arduino

Is there a link for guidelines to make your code more efficient?

For example reading a switch state, if the switch state is low then an output is set low and vice versa. I only really need to change the output state if the switch state has changed.

I dont need to set the output state low every loop if I use a state flag then I can do this - is it just bad practice to keep declaring an output that is already LOW, LOW or does it actually waste processor resources? Would it take less resources to only change the output state if the switch state has changed from the last time the output changed?

Where's your code? Trying to follow your description without the source code is rather difficult.

Repeatedly calling a pin LOW or HIGH is usually not a problem in simple code.
But it is possible for it to create a bug so I prefer to use a state flag.
It's cleaner, safer and only cost's a byte.

I would like to hear some expert opinions on this also.

EDIT: It also makes debugging using the Serial monitor easier to follow. That is what bugged me the most when not using a state flag(yeah pun intended)

trewjohn2001:
I dont need to set the output state low every loop if I use a state flag then I can do this - is it just bad practice to keep declaring an output that is already LOW, LOW or does it actually waste processor resources? Would it take less resources to only change the output state if the switch state has changed from the last time the output changed?

I think what you're doing is most efficient. Consider the alternative. In order to identify a change in state, you need to update a history variable, update a variable that stores the current state, execute an if statement to determine a difference between the two variables, and then update the output if the if statement returns true.

That sounds a little more demanding on the processor than just updating the output. Then again, none of this takes up much processing power. You're really talking nickels and dimes here.

It also depends if you're programming defensively or not; it may well be beneficial to read and write every program cycle.

Thanks that what I´m not sure of. My sketch has escalated a bit and there is some delay as I am reading and processing CAN BUS data and switch inputs so I want it to be as efficient as possible to minimise loop time.

And its probably good for me to be writing in the correct way. I am pretty new to Arduino and programming.

Hutkikz:
Repeatedly calling a pin LOW or HIGH is usually not a problem in simple code.
But it is possible for it to create a bug so I prefer to use a state flag.
It's cleaner, safer and only cost's a byte.

I would like to hear some expert opinions on this also.

EDIT: It also makes debugging using the Serial monitor easier to follow. That is what bugged me the most when not using a state flag(yeah pun intended)

Exactly - when I add in a Serial Debug and I see a constant stream of write output lows it makes me think its a waste of resources. I don´t include the serial print in the working sketch.

trewjohn2001:
waste of resources.

Waste of WHAT resources? The Atmega 328 will be exceuting 16 million instructions per second no matter what.

Doing unnecessary stuff that slows the performance of your program to an unacceptable level is not a good idea - but it is hard to categorize it as "wasting resources" - maybe "wasting an opportunity" ?

...R

Robin2:
Waste of WHAT resources? The Atmega 328 will be exceuting 16 million instructions per second no matter what.

Doing unnecessary stuff that slows the performance of your program to an unacceptable level is not a good idea - but it is hard to categorize it as "wasting resources" - maybe "wasting an opportunity" ?

...R

However you want to put it- its time/resources that could be better spent looking for a change of state on a button or an alarm channel. My sketch isn't exactly complicated but as far as I can tell from measuring millis at the start and end of the loop it still takes 25m/s to complete.

trewjohn2001:
it still takes 25m/s to complete.

With my previous comment I was trying to probe the related questions {a} do you need to do something more often than once every 25ms (if you do then the code is not suitable) and {b} what else might you do if it only took (say) 17ms?

...R

25ms

How do you know this?

.

Larry - I measured millis at the start of the sketch and then at the end and displayed the difference on an OLED - admittedly that in itself makes the sketch run "slower" so its not a real test but as I haven't yet finished the sketch and there are more features to add its a good indication of where I am up to.

Robin - at the moment I don´t have a problem with the sketch. I am wondering if I am doing things the "right way". I know the Arduino has its limitations - I don´t want to waste cycles doing something the wrong way.

25ms is a very long time.
Do you have blocking such as delay() coded in your sketch?

.

Hi Larry I'm not using delays. I think a lot is down to the Can Bus library I am using and the data rates from the ECU I have.

Previously I was using an Arduino UNO to read and process the CAN bus data and pass it on to another Arduino undo but using a mega I can more or less do everything on one board. The 25ms isn't ideal but I can live with it - I only have a couple of extra features to add now.

trewjohn2001:
at the moment I don´t have a problem with the sketch. ---SNIP--- I don´t want to waste cycles

If it works I don't see that there is any waste.

How can you expect a critique of your code when you have not posted it?

...R

Hi Robin,

As of yet my sketch isn't finished - its a work in progress -I don´t think I can attach files here and its too long to post into the thread - if you click on my profile you can see I´ve uploaded it to the projects forum.

Yes you can attach files. You need to use the REPLY option rather than Quick Reply.

...R

trewjohn2001:
Thanks that what I´m not sure of. My sketch has escalated a bit and there is some delay as I am reading and processing CAN BUS data and switch inputs so I want it to be as efficient as possible to minimise loop time.

And its probably good for me to be writing in the correct way. I am pretty new to Arduino and programming.

There is a tab on the titlebar, "Learning" that has helpful Learning opportunities in examples. But, you may find such an approach "disjointed" to a degree. You could study from Adafruit and Sparkfun and may find that helpful. There are a few "do not do" items posted randomly around the internet, maybe someone has collected them - I do not know.

But the underpending of Arduino is C++ and raw C in low-level routines. All of the Arduino language is essentially routines from a C/C++ library. So, to be efficient in Arduino is to be efficient in C/C++. Just remember to kerp in mind that your SRAM and flash memory are limited.

Ray

OK attached is the sketch.

SPEEDO_CAN_BUS_2016_all_outputs.ino (56.2 KB)

Thanks Ray. I´m curious about the repeating of digital writes. I do need to look into C/C++ routines a little more.