2019-10-21 08:06:37 +02:00
|
|
|
#include "motor.h"
|
2019-11-13 16:26:03 +01:00
|
|
|
#include "vars.h"
|
2019-11-11 22:26:34 +01:00
|
|
|
#include <Arduino.h>
|
2019-10-21 08:06:37 +02:00
|
|
|
|
2019-11-11 22:26:34 +01:00
|
|
|
Motor::Motor(int a, int b, int pwm, int angle_){
|
|
|
|
this->pinA = a;
|
|
|
|
this->pinB = b;
|
|
|
|
this->pinPwm = pwm;
|
|
|
|
this->angle = angle_;
|
|
|
|
|
|
|
|
pinMode(pinA, OUTPUT);
|
|
|
|
pinMode(pinB, OUTPUT);
|
|
|
|
pinMode(pinPwm, OUTPUT);
|
2019-10-21 08:06:37 +02:00
|
|
|
}
|
|
|
|
|
2019-11-13 16:26:03 +01:00
|
|
|
Motor::Motor(){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-11 22:26:34 +01:00
|
|
|
void Motor::drive(int speed){
|
|
|
|
byte VAL_INA, VAL_INB;
|
|
|
|
if (speed == 0) {
|
|
|
|
// no brake ma motore inerte corto a massa e vel=0 contro freno
|
|
|
|
// dove corto a VCC e vel=max
|
|
|
|
VAL_INA = 0;
|
|
|
|
VAL_INB = 0;
|
|
|
|
} else if (speed > 0) {
|
|
|
|
// clockwise
|
|
|
|
VAL_INA = 1;
|
|
|
|
VAL_INB = 0;
|
|
|
|
} else if (speed < 0) {
|
|
|
|
// counterclockwise
|
|
|
|
VAL_INA = 0;
|
|
|
|
VAL_INB = 1;
|
|
|
|
speed *= -1;
|
|
|
|
}
|
|
|
|
digitalWrite(pinA, VAL_INA);
|
|
|
|
digitalWrite(pinB, VAL_INB);
|
|
|
|
analogWrite(pinPwm, speed);
|
2019-10-21 08:06:37 +02:00
|
|
|
}
|