513
edits
Line 108: | Line 108: | ||
// Defining a class for motor control | |||
class MotoMamaMotor { | class MotoMamaMotor { | ||
private: // the following definitions are only of „internal use“ for the class itself | |||
int forwardPin; //Every MotoMamaMotor type Object has its own forwardPin | |||
int reversePin; | |||
int throttlePin; | |||
forwardPin | |||
// | |||
// this Function allows to control the motor speed | public: // the following qualities and methods are visible from the outside. | ||
void setThrottle (int newThrottle){ | // This setup-Function has to be called before using the motor control | ||
if (newThrottle>0){ // should it spin forwards or backwards? | void setup(int newForwardPin, int newReversePin, int newThrottlePin) { | ||
// spin forwards | // we remember the pins for future use | ||
digitalWrite(reversePin,LOW); | forwardPin = newForwardPin; | ||
digitalWrite(forwardPin,HIGH); | reversePin = newReversePin; | ||
}else{ | throttlePin = newThrottlePin; | ||
// spin backwards | // and at the same time we initialize the outputs ! | ||
digitalWrite(forwardPin,LOW); | digitalWrite(throttlePin, LOW); // so that the motors don’t spin directly from the beginning .. | ||
digitalWrite(reversePin,HIGH); | pinMode(forwardPin, OUTPUT); | ||
} | pinMode(reversePin, OUTPUT); | ||
// adjust the speed: | pinMode(throttlePin, OUTPUT); | ||
analogWrite(throttlePin, newThrottle); | } | ||
} | |||
// this Function allows to control the motor speed | |||
void setThrottle (int newThrottle) { | |||
if (newThrottle > 0) { // should it spin forwards or backwards? | |||
// spin forwards | |||
digitalWrite(reversePin, LOW); | |||
digitalWrite(forwardPin, HIGH); | |||
} else { | |||
// spin backwards | |||
digitalWrite(forwardPin, LOW); | |||
digitalWrite(reversePin, HIGH); | |||
} | |||
// adjust the speed: | |||
analogWrite(throttlePin, newThrottle); | |||
} | |||
}; | }; | ||
// create two separate MotoMamaMotor type Objects (leftMotor, rightMotor). They can be used as normal variables. | // create two separate MotoMamaMotor type Objects (leftMotor, rightMotor). They can be used as normal variables. | ||
MotoMamaMotor leftMotor; | MotoMamaMotor leftMotor; | ||
MotoMamaMotor rightMotor; | MotoMamaMotor rightMotor; | ||
void setup(){ | void setup() { // this is our „main“ setup Function | ||
leftMotor.setup(3,4,5); | leftMotor.setup(3, 4, 5); // assign the pin numbers for each motor in the setup Function: setup(int newForwardPin, int newReversePin, int newThrottlePin) | ||
rightMotor.setup(6,7,8); | rightMotor.setup(6, 7, 8); | ||
}; | }; | ||
void loop(){ | void loop() { // this is our „main“ loop Function where concrete actions are executed. For example: | ||
// Start backwards, slow down and accelerate forwards | // Start backwards, slow down and accelerate forwards | ||
for (int i =-255, i<=255;i++){ | for (int i = -255, i <= 255; i++) { | ||
leftMotor.setThrottle(i); | leftMotor.setThrottle(i); | ||
rightMotor.setThrottle(i); | rightMotor.setThrottle(i); | ||
}; | |||
// Start forwards, slow down and accelerate backwards | |||
for (int i = 255, i >= -255; i--) { | |||
leftMotor.setThrottle(i); | |||
rightMotor.setThrottle(i); | |||
}; | |||
}; | }; | ||
==Organizing classes in separate files== | ==Organizing classes in separate files== |
edits