Last active
August 7, 2021 20:04
-
-
Save ErickWendel/d24da3642b1299839297aa2be910f8fd to your computer and use it in GitHub Desktop.
Example of moving wheel (2 motor DC) sending PWM commands through an ESP32 and a PS3 Controller/Joystick
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <Ps3Controller.h> | |
// https://wouterdeschuyter.be/blog/configure-a-ps3-controller-to-automatically-connect-to-a-raspberry-pi | |
// http://www.squids.com.br/arduino/index.php/projetos-arduino/projetos-squids/intermediario/305-i08-como-controlar-motores-dc-com-o-driver-ponte-h-l9110-e-arduino | |
// Motor A | |
const uint8_t RIGHT_WHEEL_A1_A_DIR = 32; //M-A1-A | |
const uint8_t RIGHT_WHEEL_A1_B_PWM = 33; //M-A1-B // PWM | |
// Pinos de controle motor B. | |
const uint8_t LEFT_WHEEL_B1_A_DIR = 26; // M-B1-A | |
const uint8_t LEFT_WHEEL_B1_B_PWM = 25; // M-B1-B // PWM | |
// Setting PWM properties | |
const int freq = 50; | |
const int pwmChannel1 = 1; | |
const int pwmChannel2 = 2; | |
// Setting the resolution to 8Bits, gives you a duty cycle range [0 – 255]. While setting it to 10Bits, gives you a range of [ 0 – 1023 ]. And so on! | |
const int resolution = 8; | |
int dutyCycle = 200; | |
//variáveis | |
const int motorSpeed = 255; // armazenar o valor de velocidade atual | |
boolean stateSwitch = 0; // armazenar o status do switchPin (0 ou 1) | |
boolean stateTurn = 0; // armazenar rotação (0 ou1) | |
const char up = '1'; | |
const char down = '2'; | |
const char right = '3'; | |
const char left = '4'; | |
char direction = up; | |
void turnOff() | |
{ | |
#ifdef DEBUG | |
Serial.println("turning off..."); | |
#endif | |
digitalWrite(RIGHT_WHEEL_A1_A_DIR, LOW); | |
digitalWrite(LEFT_WHEEL_B1_A_DIR, LOW); | |
ledcWrite(pwmChannel1, LOW); | |
ledcWrite(pwmChannel2, LOW); | |
} | |
void goForward() | |
{ | |
#ifdef DEBUG | |
Serial.println("going forward..."); | |
#endif | |
digitalWrite(RIGHT_WHEEL_A1_A_DIR, LOW); | |
digitalWrite(LEFT_WHEEL_B1_A_DIR, LOW); | |
ledcWrite(pwmChannel1, motorSpeed); | |
ledcWrite(pwmChannel2, motorSpeed); | |
} | |
void goBack() | |
{ | |
#ifdef DEBUG | |
Serial.println("going back..."); | |
#endif | |
digitalWrite(RIGHT_WHEEL_A1_A_DIR, HIGH); | |
digitalWrite(LEFT_WHEEL_B1_A_DIR, HIGH); | |
// digitalWrite(RIGHT_WHEEL_A1_B_PWM, LOW); | |
// digitalWrite(LEFT_WHEEL_B1_B_PWM, LOW); | |
ledcWrite(pwmChannel1, LOW); | |
ledcWrite(pwmChannel2, LOW); | |
} | |
void keepDirection() | |
{ | |
if (direction == up) | |
{ | |
digitalWrite(LEFT_WHEEL_B1_A_DIR, LOW); | |
digitalWrite(RIGHT_WHEEL_A1_A_DIR, LOW); | |
} | |
if (direction == down) | |
{ | |
digitalWrite(LEFT_WHEEL_B1_A_DIR, HIGH); | |
digitalWrite(RIGHT_WHEEL_A1_A_DIR, HIGH); | |
} | |
} | |
void goRight() | |
{ | |
#ifdef DEBUG | |
Serial.print("going right..."); | |
Serial.println(direction); | |
#endif | |
keepDirection(); | |
if (direction == up) | |
{ | |
ledcWrite(pwmChannel1, 150); | |
ledcWrite(pwmChannel2, motorSpeed); | |
} | |
if (direction == down) | |
{ | |
ledcWrite(pwmChannel1, motorSpeed - 150); | |
ledcWrite(pwmChannel2, motorSpeed - motorSpeed); | |
} | |
} | |
void goLeft() | |
{ | |
#ifdef DEBUG | |
Serial.print("going left..."); | |
Serial.println(direction); | |
#endif | |
keepDirection(); | |
if (direction == up) | |
{ | |
ledcWrite(pwmChannel1, motorSpeed); | |
ledcWrite(pwmChannel2, 150); | |
} | |
if (direction == down) | |
{ | |
ledcWrite(pwmChannel1, motorSpeed - motorSpeed); | |
ledcWrite(pwmChannel2, motorSpeed - 150); | |
} | |
} | |
void moveWheel(char message) | |
{ | |
if (message == up) | |
{ | |
direction = message; | |
goForward(); | |
return; | |
} | |
if (message == down) | |
{ | |
direction = message; | |
goBack(); | |
return; | |
} | |
if (message == right) | |
{ | |
goRight(); | |
return; | |
} | |
if (message == left) | |
{ | |
goLeft(); | |
return; | |
} | |
turnOff(); | |
} | |
void setup() | |
{ | |
Serial.begin(9600); | |
// sets the pins as outputs: | |
pinMode(RIGHT_WHEEL_A1_A_DIR, OUTPUT); | |
pinMode(LEFT_WHEEL_B1_A_DIR, OUTPUT); | |
ledcSetup(pwmChannel1, freq, resolution); | |
ledcAttachPin(RIGHT_WHEEL_A1_B_PWM, pwmChannel1); | |
ledcSetup(pwmChannel2, freq, resolution); | |
ledcAttachPin(LEFT_WHEEL_B1_B_PWM, pwmChannel2); | |
Ps3.begin("8c:85:90:ad:88:f3"); | |
Serial.println("Ready"); | |
turnOff(); | |
} | |
void loop() | |
{ | |
if (!Ps3.isConnected()) | |
{ | |
Serial.println("Controller not connected!"); | |
delay(100); | |
return; | |
} | |
if (Ps3.event.button_down.select) | |
{ | |
Serial.println("select!"); | |
turnOff(); | |
delay(200); | |
} | |
if (Ps3.data.button.cross || | |
Ps3.event.button_down.up || | |
Ps3.event.button_down.start || | |
abs(Ps3.event.analog_changed.button.up)) | |
{ | |
Serial.println("up!"); | |
// Serial.println("Pressing the cross button"); | |
moveWheel(up); | |
delay(200); | |
} | |
if (Ps3.data.button.square || | |
Ps3.event.button_down.down || | |
abs(Ps3.event.analog_changed.button.down)) | |
{ | |
Serial.println("down!"); | |
// Serial.println("Pressing the square button"); | |
moveWheel(down); | |
delay(200); | |
} | |
if (Ps3.event.button_down.left || abs(Ps3.event.analog_changed.button.left)) | |
{ | |
Serial.println("left!"); | |
moveWheel(left); | |
delay(200); | |
} | |
if (Ps3.event.button_down.right || abs(Ps3.event.analog_changed.button.right)) | |
{ | |
Serial.println("right!"); | |
moveWheel(right); | |
delay(200); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment