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