513
edits
No edit summary |
|||
Line 4: | Line 4: | ||
* clarity: the bigger the program gets the harder it is to keep the general overview | * clarity: the bigger the program gets the harder it is to keep the general overview | ||
* decoupling: you only want to change a certain part of the program without influencing all the rest | * decoupling: you only want to change a certain part of the program without influencing all the rest of it | ||
* recyclability: there are several similar parts in your robot for which you would like to reuse particular sections of your code | * recyclability: there are several similar parts in your robot for which you would like to reuse particular sections of your code | ||
Line 16: | Line 16: | ||
The [http://arduino.cc/de/Reference/FunctionDeclaration function] is an enormous useful tool that can help you structuring your code. Functions allow you to sum up several operations under the same command. This command can be called again and again without knowing about or duplicating the included code. | The [http://arduino.cc/de/Reference/FunctionDeclaration function] is an enormous useful tool that can help you structuring your code. Functions allow you to sum up several operations under the same command. This command can be called again and again without knowing about or duplicating the included code. | ||
Here is an example for a function | Here is an example for a function that sets the speed of a motor which is connected to an H-bridge: | ||
Line 29: | Line 29: | ||
This already saves us a lot of typing –<br> | This already saves us a lot of typing –<br> | ||
But one thing still seems quite laborious: we have to know the exact pin numbers every time we want to change the speed. | But one thing still seems quite laborious: we have to know the exact pin numbers every time when we want to change the speed. | ||
Imagine a program in which similar function calls are distributed all over. <br> | Imagine a program in which similar function calls are distributed all over. <br> | ||
Line 39: | Line 39: | ||
<br> | <br> | ||
==Possible | ==Possible Solution: Data and Code in One Package – "Classes" and "Objects"== | ||
C++ (and also Arduino) provides a specific feature that can solve our problem: Classes and Objects | C++ (and also Arduino) provides a specific feature that can solve our problem: Classes and Objects. | ||
A Class predefines common qualities of a group of Objects. <br> | A Class predefines common qualities of a group of Objects. <br> | ||
Line 51: | Line 51: | ||
Now the computer knows that there is a class called | Now the computer knows that there is a class called "Motor".<br> | ||
This class (class) provides a function called | This class (class) provides a function called "setThrottle" that is called as an argument by using a number (int) and that does not return anything (void). | ||
===Classes are Data | ===Classes are Data Types=== | ||
We can use our | We can use our "Motor" - class the same way we are using other data - types (int, long, float).<br> | ||
The following lines use that quality in order to implement two motors: | The following lines use that quality in order to implement two motors: | ||
<br> | <br> | ||
Line 80: | Line 80: | ||
<br> | <br> | ||
===Classes can contain other variables that can only be accessed | ===Classes can contain other variables that can only be accessed "internally"=== | ||
How do the Objects | How do the Objects "leftMotor" and "rightMotor" know which pins they want to switch on and off? | ||
Until now our | Until now our "Motor" class is quite abstract since it does not contain this information. <br> | ||
A class that should conduct practical actions needs further variables (concrete pin numbers) and lines of code that describe what to do exactly.<br> | A class that should conduct practical actions needs further variables (concrete pin numbers) and lines of code that describe what to do exactly.<br> | ||
We can simply include both things in the class definition: | We can simply include both things in the class definition: | ||
Line 94: | Line 94: | ||
How do we include the pin numbers into the Object if they are not visible from the outside?<br> | How do we include the pin numbers into the Object if they are not visible from the outside?<br> | ||
We simply write a | We simply write a "setup" Function for the class: | ||
Line 102: | Line 102: | ||
<br> | <br> | ||
==A | ==A Complete Example == | ||
In order to construct a complete example we only have to include the | In order to construct a complete example we only have to include the "speed"-Function (see description above): | ||
Line 113: | Line 113: | ||
<br> | <br> | ||
==Organizing | ==Organizing Classes in Separate Files== | ||
Line 120: | Line 120: | ||
1. <br> | 1. <br> | ||
Click on the arrow in the right corner in order to add a new file.<br> | Click on the arrow in the right corner in order to add a new file.<br> | ||
Select | Select "new Tab". | ||
Line 127: | Line 127: | ||
2.<br> | 2.<br> | ||
Enter a filename at the bottom of the window. <br> | Enter a filename at the bottom of the window. <br> | ||
It must end with | It must end with ".h" – In our case it could be "motorControl.h". | ||
3.<br> | 3.<br> | ||
Line 139: | Line 139: | ||
4.<br> | 4.<br> | ||
Go back to your main file.<br> | Go back to your main file.<br> | ||
Now you have to | Now you have to "link" the new file to your main file in order to use the features of our class.<br> | ||
Include the following line in the top of your main file | Include the following line in the top of your main file | ||
Line 153: | Line 153: | ||
In the beginning we promised that it is possible to replace different motor drivers without changing the rest of the program.<br> | In the beginning we promised that it is possible to replace different motor drivers without changing the rest of the program.<br> | ||
Classes can indeed include other Objects in the form of | Classes can indeed include other Objects in the form of "[https://en.wikipedia.org/wiki/Member_variable member variables]". | ||
C++ allows class declarations and the code for included Functions to be be placed in different files. <br> | C++ allows class declarations and the code for included Functions to be be placed in different files. <br> |
edits