Code explanation needed please

Hi I searched but can not find an explanation on what this code block
mean or what it does. Also why when I use this it seem to take a long time to execute and the program becomes very slow. There are three blocks like this in the sketch.

int lookRight()
{
    myservo.write(0); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(90); 
    return distance;
}

This is not in the loop but lookright() is called in the loop.
Could some help me understand this and what would it be called.

Regards

Henry

It's a function that returns a int value.

Send the servo to 0 degrees.

Delay

Take a ping reading and store the ping readings into a variable named distance.

Delay again, so far a total delay of .6 seconds. And he wonders why it takes so long.

Send the servo to 90 degrees.

return the distance.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Thanks for the explanation. Would there be a name for a code block like this?
I used to program PIC's and there you would use "makro's". Is this the same thing.?
I am new to the forum and can not upload yet.

I found a sketch that used these routines and added them to my own sketch for a Bluetooth tank that was working fine. I found that the lookright() and loofleft() routine did not do anything but the readping() did work. But suddenly if I gave the go forward command the tank would do nothing for 3 to 4 seconds and only then the wheels would start spinning. I then went and exchanged the call for lookriht() with the code in this routine and the same with lookleft() and readping(). I deleted the routines and now it is working fine. When I give forward command the tank is responsive again and the servo and HC-SR04 is working as expected.

"function" - first sentence in reply #2

1 Like

Thanks aarg,

Is this very processing intensive? Why would it have such a negative impact?

There's 600ms worth of delay() in there...

If you are referring to the delay() calls, effectively yes. They tie up the processor for long periods of time during which nothing happens. By definition...

Delay is a NOP instruction. Means, basically, do nothing. Its not processor intensive at all because the CPU is told to do NOTHING with a NOP; .6 seconds of doing nothing.

However, the end result is the same, from the point of view of other tasks that need to run. That is why I qualified my statement with, "effectively".

Obviously, all processors are equally good at doing nothing. :slight_smile:

Yes I know about the delays.
I would imagine that this is only executed when it is called from the loop or is it executed the whole time.

I took the code inside "lookright() function" and replaced the the call for lookright() in the loop with exactly the same code as in the function and then deleted the function. Did the same with the other two functions and that solved the sluggishness.

A function call by itself, executes in only a few microseconds. So you clearly did something else that you're not aware of.

I would imagine that this is only executed when it is called from the loop or is it executed the whole time.

Functions only execute when they are called. How else could you control when they execute?

Thanks, I will try it again and if it is still a problem I will post the code.

Just for interest what would you call a block of code that starts with "void"

void moveStop() {
  motor1.run(RELEASE); 
  motor2.run(RELEASE);
  } 

A function returning a void datatype.

Finally found a good explanation:
https://www.arduino.cc/en/Reference/FunctionDeclaration

image

Dang, there is that word again, "Function".

Yep I used to call them subroutines in pic programming

Subroutines have no parameters or return values.

QBasic.

When you want to execute MySub, code this:

CALL MySub

And the subroutine will be executed. In languages like QBasic/QuickBasic
which have 'variable scoping', subroutines, by default, have their own
'local' variables. This is good, because it lets you code the subroutine
without being concerned about trashing a variable of the same name used
outside the subroutine. There are two mechanisms for the subroutine to
communicate with the rest of the program. One is by passing 'parameters' to
the subroutine when you call it. These parameters are defined in the
subroutine definition like this:

SUB MySub(P1,P2)

END SUB

Tom... :smiley: :+1: :coffee: :australia:

The exact words used are programming-language dependent; don't get hung up on them (but do look for the local conventions in the language you're using.)
Fortran has function and subroutine.
Pascal has function and Procedure.
C/C++ has only function (whether or not there is a return value.) (but doesn't use a keyword.)
I've used assembly languages with half-a-dozen different ways to invoke and/or pass arguments and/or get results.