GMU:Functions and Classes (Arduino): Difference between revisions

From Medien Wiki
No edit summary
Line 79: Line 79:
Motor rightMotor; //  declare another object of the type „Motor“ called „rightMotor“  
Motor rightMotor; //  declare another object of the type „Motor“ called „rightMotor“  
''
''
<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.  
Line 98: Line 98:




====Classes can contain other variables that can only be accessed „internally“====
===Classes can contain other variables that can only be accessed „internally“===




Line 145: Line 145:




====A complete example ====


===A complete example ===




In order to construct a complete example we only have to include the „speed“-Function (see description above):
In order to construct a complete example we only have to include the „speed“-Function (see description above):


''
// Defining a class for motor control  
// Defining a class for motor control  


Line 211: Line 213:
};
};
};
};
''






Organizing classes in separate files
===Organizing classes in separate files===


We can improve our program structure by putting classes in separate files.
We can improve our program structure by putting classes in separate files.
Line 232: Line 235:
Enter the first line:
Enter the first line:


''
#include „Arduino.h“
#include „Arduino.h“
''


This line „links“ the file to the Arduino-specific features.
This line „links“ the file to the Arduino-specific features.
Line 242: Line 249:
Include the following line in the top of your main file
Include the following line in the top of your main file


''
#include „MotorControl.h“
#include „MotorControl.h“
''


and write the rest of your program as usual.
and write the rest of your program as usual.




More details on the concept
 
===More details on the concept===


Classes can build up on each other (inheritance)
Classes can build up on each other (inheritance)