513
edits
No edit summary |
|||
Line 8: | Line 8: | ||
These problems reach back to the beginning of programming and their solution is an art and a science at the same time. | These problems reach back to the beginning of programming and their solution is an art and a science at the same time.<br> | ||
It generally helps a lot to separate your program into single subunits that are relatively independent from each other. | It generally helps a lot to separate your program into single subunits that are relatively independent from each other. | ||
Line 28: | Line 28: | ||
This already saves us a lot of typing – | 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 we want to change the speed. | ||
Imagine a program in which similar function calls are distributed all over. | Imagine a program in which similar function calls are distributed all over. <br> | ||
In case of a hardware update you would have to find every single function call and change the parameters manually. | In case of a hardware update you would have to find every single function call and change the parameters manually. <br> | ||
This effort could surely be spent for more useful things.. | This effort could surely be spent for more useful things.. | ||
Line 43: | Line 43: | ||
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. | A Class predefines common qualities of a group of Objects. <br> | ||
It for instance determines that every motor has the possibility to exert a certain amount of throttle. | It for instance determines that every motor has the possibility to exert a certain amount of throttle. <br> | ||
The corresponding program could look like this: | The corresponding program could look like this: | ||
Line 51: | Line 51: | ||
Now the computer knows that there is a class called „Motor“. | Now the computer knows that there is a class called „Motor“.<br> | ||
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). | 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). | ||
Line 57: | Line 57: | ||
===Classes are Data-types=== | ===Classes are Data-types=== | ||
We can use our „Motor“ - class the same way we are using other data - types (int, long, float).< | 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 65: | Line 65: | ||
<br> | <br> | ||
<br> | |||
===Calling inner functions of a class (Methods)=== | ===Calling inner functions of a class (Methods)=== | ||
Imagine we want to drive the left motor with full throttle whereas the right motor should stop completely. | Imagine we want to drive the left motor with full throttle whereas the right motor should stop completely. <br> | ||
This can be expressed as follows: | This can be expressed as follows: | ||
Line 85: | Line 85: | ||
How do the Objects „leftMotor“ and „rightMotor“ know which pins they want to switch on and off? | How do the Objects „leftMotor“ and „rightMotor“ know which pins they want to switch on and off? | ||
Until now our „Motor“ class is quite abstract since it does not contain this information. | 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. | 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 93: | Line 93: | ||
How do we include the pin numbers into the Object if they are not visible from the outside? | How do we include the pin numbers into the Object if they are not visible from the outside?<br> | ||
We simply write a „setup“ Function for the class: | We simply write a „setup“ Function for the class: | ||
edits