Condensing Code

Is there any way to take a group of code, and condense it into a single phrase to call instead of re-writing the whole code?

Yes - they're called functions or macros.

Do you want to make the code smaller or easier to read and understand ?

If the latter then put the code in a function with a sensible name and call the function when required.

An example of what you are trying to achieve would be helpful.

I would like to start it with simple code like the code for "go forward" or "left turn". When i put it together, and start to do things like "go forward then turn left then go forward". then it would be nice to just put a single command instead of twenty lines of code. I have watched a bunch of videos on it, but i don't really understand it. Thanks!

Your examples of go forward and left turn are good ones. Write the code that makes the hexapod go forward and put that code in a function with a meaningful name. Then, when you want to go forward call the function by using its name. You could perhaps make the function move the bot forward at different speeds and tell it what speed to use when calling the function.

Have you used any of the Arduino functions such as digitalWrite() ? The code for the function is built into the system but you could just as well write your own. So you could then make the bot go forwards like this

goForward(1);  //go forward at speed 1
goForward(2);  //go forward at speed 2

The function could also be written to move a given number of distance units

goForward(10);  //go forward 10 units
goForward(20);  //go forward 20 units

Whatever you want do do you have to write and test the code first. Have you written any code for your project ?

Have a look at Planning and Implementing a Program

...R

UKhelibob not yet, but I was planning on doing that this weekend. I just figured while I had some free time i could figure this out. I'll definitely post it when i do.

[edit: my main problem now is everything i see online is all math stuff. like ab or lw*h i need to do that with servos, but i cant find ANYTHING with that]

crispy_cat:
UKhelibob not yet, but I was planning on doing that this weekend. I just figured while I had some free time i could figure this out. I'll definitely post it when i do.

[edit: my main problem now is everything i see online is all math stuff. like ab or lw*h i need to do that with servos, but i cant find ANYTHING with that]

Have you noticed print() ? :slight_smile:

i need to do that with servos,

Do what exactly ?

Servos move to the position they are commanded to go to. This is normally 0 to 180 degrees but be cautious as not all servos can move 180 degrees. Be careful too that you buy servos and not continuous rotation "servos" which are not the same thing at all. To make your bot move you will need to command several servos to move to precise positions at the same time.

Be aware that this is not a trivial project that you are embarking on. The best thing that you could do is to work through the examples in the IDE. Which Arduino board do you have ? What power source will you be using for the servos ?

Click on the link I posted. I know how to use servos, i just can't find and examples of using servos with functions.
here, i'll post it again:
https://hackaday.io/project/16315-12-servo-hexapod
I've used servos in the past, a lot. I just have never used 12 together.

crispy_cat:
Click on the link I posted.

Sure. Go back and edit it to add link tags, so that it is clickable.

Can you please explain what you mean by

using servos with functions.

i just can't find and examples of using servos with functions.

Moving a servo, if you are using the Servo library, is a matter of calling a function. Whether you call that function from the loop() function or from another function that is called in loop() does not matter.

Do what was suggested earlier. Write the code to make the device move forward, in loop(). Then, rename loop() to moveForward(), and create a new loop() method. Make loop() do something else. Rename loop() to reflect what it now does, and build a new loop() function.

Repeat until you have all the functions you need. Then, just all them from loop(), in the proper order, and you device will be doing the hokey pokey before you know it.