Go Down

Topic: replace delay() (Read 2641 times) previous topic - next topic

retrolefty


Another idea.  There are a lot of patterns like this:

Code: [Select]

        digitalWrite(Leds[1], HIGH);
        digitalWrite(Leds[2], HIGH);
        digitalWrite(Leds[7], HIGH);
        digitalWrite(Leds[8], HIGH);


Would it be smaller to do this?

Code: [Select]

        setLeds("1278", HIGH);


In other words, would passing a string constant with the LEDs to set to a custom led-setting routine be cheaper than 4 separate calls?

Just a thought. 

-br




I guess if your goal is a smaller source file then functions like this might help, but I would assume that you would be just making the compiled code larger and probably slower. 'Pretty' source files at the expense of slower and larger object files sounds kind of backwards thinking to me? Discuss.

Lefty

robtillaart

Quote
'Pretty' source files at the expense of slower and larger object files sounds kind of backwards thinking to me? Discuss.


If memory and/or performance is not the problem readability for maintainability is a very important point.
I have seen too much "write only" code that could not be read by mere humans and it really is no fun to reverse engineer the meaning of it.
So use proper descriptive names  for variables ==> proper named vars need far less comments!
and use the proper types as far as possible, datatypes were invented (among others) for the reason to match ;)

So I don't like this
Code: [Select]
void setLeds(char * str, byte lohi)
{
  for (byte i=0; i <strlen(str)) digitalWrite(str[i]-'0', lohi);
}


but I prefer meaningful names and proper types.
Code: [Select]
void setLeds(byte pin[], byte size, byte lohi)
{
  for (byte i=0; i <size) digitalWrite(pin[i], lohi);
}

...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

PeterH

Passing in a set of pin numbers as a string seems like a backwards step to me since it requires code to construct and parse the string.

If that set of pins is used repeatedly as a set then it'd be tempting to define a global variable or constant with those numbers in and pass that to the function to set them all. If it's dynamic then it would be possible to use a variable length array, or varargs, to pass in the set of numbers.
I only provide help via the forum - please do not contact me for private consultancy.

Maybe I am doing it the wrong way, but: Why you don't use millis() instead?

I mean, when you set led1 ON, you do:
digitalWrite(led1,HIGH);
time1=millis();

and for example if you want it ON for 250 ms:
if (millis>time1+250){
digitalWrite (led1,LOW);}

This way the loop it's never stopped.
NO?

billroy

I was thinking of something as simple as this:
Code: [Select]


void setLeds(char *leds, byte value) {
while (*leds) {
digitalWrite(led[*leds++ - '0'], value);
}
}

setLeds("1457", onoff);


I think the feedback that there are bigger patterns you can factor in to common routines is probably more important, though.

-br

robtillaart

pointer math, definitely faster!

well done 
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

dc42


pointer math, definitely faster!  


At the assembly code level, yes. However, for several decades, optimising compilers have been recoding loops that access array elements sequentially as loops that increment pointers. So you won't necessarily see any improvement when you code in C/C++.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Go Up