Pages: 1 [2]   Go Down
Author Topic: replace delay()  (Read 2068 times)
0 Members and 1 Guest are viewing this topic.
Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Would it be smaller to do this?

Code:
        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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink

So I don't like this
Code:
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:
void setLeds(byte pin[], byte size, byte lohi)
{
  for (byte i=0; i <size) digitalWrite(pin[i], lohi);
}
...
Logged

Rob Tillaart

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12579
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was thinking of something as simple as this:
Code:

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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pointer math, definitely faster!

well done   
Logged

Rob Tillaart

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

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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++.
Logged

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.

Pages: 1 [2]   Go Up
Jump to: