2017-01-17 17:13:02 +01:00
|
|
|
#include "Arduino.h"
|
|
|
|
#include "Motor.h"
|
|
|
|
|
|
|
|
int Motor::getBackwardsValue(){
|
|
|
|
if(this->forwardValue==0){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:15:39 +01:00
|
|
|
int Motor::getSpeed(int direction){
|
|
|
|
if(direction==LOW){
|
|
|
|
return 255-this->speedPwm;
|
|
|
|
}
|
|
|
|
return this->speedPwm;
|
|
|
|
}
|
|
|
|
|
2017-01-17 17:13:02 +01:00
|
|
|
Motor::Motor(int pwmPin, int directionPin,bool forwardValue, int speedPwm, int directionDelay){
|
|
|
|
this->pwmPin = pwmPin;
|
|
|
|
this->directionPin = directionPin;
|
|
|
|
this->forwardValue = forwardValue;
|
|
|
|
this->speedPwm = speedPwm;
|
|
|
|
this->directionDelay= directionDelay;
|
2017-01-18 22:15:39 +01:00
|
|
|
|
|
|
|
//Pins auf Output setzen
|
|
|
|
pinMode(pwmPin, OUTPUT );
|
|
|
|
pinMode(directionPin, OUTPUT );
|
|
|
|
digitalWrite(pwmPin, LOW );
|
|
|
|
digitalWrite(directionPin, LOW );
|
2017-01-17 17:13:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Motor::forward(){
|
|
|
|
//Motor vor Änderungen stoppen
|
|
|
|
this->stop();
|
|
|
|
digitalWrite(this->directionPin,this->forwardValue);
|
2017-01-18 22:15:39 +01:00
|
|
|
analogWrite(this->pwmPin, this->getSpeed(this->forwardValue));
|
2017-01-17 17:13:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Motor::backward(){
|
|
|
|
//Motor vor Änderungen stoppen
|
|
|
|
this->stop();
|
|
|
|
digitalWrite(this->directionPin,this->getBackwardsValue());
|
2017-01-18 22:15:39 +01:00
|
|
|
analogWrite(this->pwmPin, this->getSpeed(this->getBackwardsValue()));
|
2017-01-17 17:13:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Motor::stop(){
|
|
|
|
digitalWrite(this->directionPin, LOW);
|
|
|
|
digitalWrite(this->pwmPin, LOW);
|
|
|
|
//Abwarten bis Rad gestoppt wurde
|
|
|
|
delay(this->directionDelay);
|
|
|
|
}
|