Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Arduino.h"
- #include "DRV8833.h"
- //Motors Group A
- //const int m1_a=33;
- //const int m1_b=32;
- //const int m1_pwm=25;
- //Motors Group B
- //const int m2_a=27;
- //const int m2_b=26;
- //const int m2_pwm=14;
- // setting PWM properties
- const int freq = 5000;
- const int resolution = 8;
- const int channel_1 = 0;
- const int channel_2 = 1;
- const int channel_3 = 3;
- const int channel_4 = 4;
- DRV8833::DRV8833(int _m1_a, int _m1_b, int _m2_a, int _m2_b) {
- m1_a=_m1_a;
- m1_b=_m1_b;
- m2_a=_m2_a;
- m2_b=_m2_b;
- pinMode(m1_a,OUTPUT);
- pinMode(m1_b,OUTPUT);
- pinMode(m2_a,OUTPUT);
- pinMode(m2_b,OUTPUT);
- // configure LED PWM functionalitites
- ledcSetup(channel_1, freq, resolution);
- ledcSetup(channel_2, freq, resolution);
- ledcSetup(channel_3, freq, resolution);
- ledcSetup(channel_4, freq, resolution);
- // attach the channel to the GPIO to be controlled
- ledcAttachPin(m1_a, channel_1);
- ledcAttachPin(m1_b, channel_2);
- ledcAttachPin(m2_a, channel_3);
- ledcAttachPin(m2_b, channel_4);
- }
- void DRV8833::turn_left(int spd){
- move(0,spd,1);
- move(1,spd,0);
- delay(20);
- stop();
- }
- void DRV8833::turn_right(int spd){
- //move(0,spd,0);
- move(1,spd,1);
- delay(20);
- stop();
- }
- void DRV8833::left_1wd(int spd){
- move(0,spd,0);
- //move(1,spd,0);
- delay(20);
- stop();
- }
- void DRV8833::right_1wd(int spd){
- //move(0,spd,1);
- move(1,spd,0);
- delay(20);
- stop();
- }
- void DRV8833::forward(int spd){
- move(0,spd,0);
- move(1,spd,0);
- delay(20);
- stop();
- }
- void DRV8833::backward(int spd){
- move(0,spd,1);
- move(1,spd,1);
- delay(20);
- stop();
- }
- void DRV8833::move(int motor, int speed, int direction){
- //Move specific motor at speed and direction
- //motor: 0 for B 1 for A
- //speed: 0 is off, and 255 is full speed
- //direction: 0 clockwise, 1 counter-clockwise
- if (motor == 1){
- if (direction == 1) {
- digitalWrite(m1_a, LOW);
- //digitalWrite(m1_b, HIGH);
- ledcWrite(channel_2, speed);
- //ledcDetachPin(pin);
- } else {
- //digitalWrite(m1_a, HIGH);
- digitalWrite(m1_b, LOW);
- ledcWrite(channel_1, speed);
- //ledcDetachPin(pin);
- }
- } else {
- if (direction == 1) {
- //digitalWrite(m2_a, HIGH);
- digitalWrite(m2_b, LOW);
- ledcWrite(channel_3, speed);
- //ledcDetachPin(pin);
- } else {
- digitalWrite(m2_a, LOW);
- //digitalWrite(m2_b, HIGH);
- ledcWrite(channel_4, speed);
- //ledcDetachPin(pin);
- }
- }
- }
- void DRV8833::stop(){
- digitalWrite(m1_a,LOW);
- digitalWrite(m1_b,LOW);
- digitalWrite(m2_a,LOW);
- digitalWrite(m2_b,LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement