I'm trying to run a few vibrator motors. Had no success with the arduino so I switched to an ESP32 for testing, but problems still exist.
I'm trying to run a two vibrators in sequence but it just won't work.
A total of 5 buzzers/vibrators, 4 buttons, Every button triggers their own motor. But BEFORE doing that, I want them to first make a shorter buzz on a specific, common motor, this motor is the same for all the button-presses.
Thing is that I can manage to flash LEDs in the sequences I want, and the second motors works as intended, just the first common motor that doesn't wanna play ball for some reason.
I am using DRV8833s, and just one single output to each motor, and they all share a common ground (Normally one would run one or two DC motors on one of these drivers, but since I'm not in need of direction control, I figured I should be able to do it in this way instead.
So Driver A runs motors 1-4 and a second Driver B is what is supposed to drive the common motor. I've tried interchanging them, running it at full power instead of PWM and a lot more, it just won't run.
So I have to assume that there is something wrong with my code (Not surprised in the least, but I think it's strange since flashing a common LED and then resume with flashing their respective LED is working good...
A potentiometer is hooked up as well in order to be afble to tweak the pwm a little while running the program.
Even though the LED blinking works as intended, I have a suspicion that I need to rewrite the code so it uses millis instead of the delays (?)
I have read a bunch on the topic but I don't get how to implement it in my sketch.
Here's my code and a pic of the schematic.
#include <EasyButton.h>
#define BAUDRATE 9600
// #define MOTOR_UP 10
// #define MOTOR_LEFT 11
// #define MOTOR_RIGHT 12
// #define MOTOR_DOWN 13
// BUTTON PiNS
#define UP_BUTTON_PIN 38
#define LEFT_BUTTON_PIN 37
#define RIGHT_BUTTON_PIN 36
#define DOWN_BUTTON_PIN 35
const int MOTOR_CEN = 10;
const int MOTOR_UP = 11;
const int MOTOR_LEFT = 12;
const int MOTOR_RIGHT = 13;
const int MOTOR_DOWN = 14;
const int freq = 1000;
const int pwmChannel = 0; // choose 0 - 15
const int res = 8; // 2^8 = 256
const int MAX_DUTY_CYCLE = (int)(pow(2, res) - 1);
const int POT_PIN = 3;
int aBoo = 255;
EasyButton button1(UP_BUTTON_PIN);
EasyButton button2(LEFT_BUTTON_PIN);
EasyButton button3(RIGHT_BUTTON_PIN);
EasyButton button4(DOWN_BUTTON_PIN);
// Callback function UP
void onButton1Pressed()
{
Serial.println("UP BUTTON PRESSED");
int aBoo = analogRead(POT_PIN);
aBoo = map(aBoo, 0, 4095, 25, MAX_DUTY_CYCLE);
delay(5);
Serial.println(aBoo);
for(int dutyCycle = 0; dutyCycle <= (aBoo/1.5); dutyCycle++){
ledcWrite(MOTOR_CEN, dutyCycle);
delay(3);
}
for(int dutyCycle = (aBoo/1.5); dutyCycle >= 0; dutyCycle--){
ledcWrite(MOTOR_CEN, dutyCycle);
delay(3);
}
delay(300);
for(int dutyCycle = 0; dutyCycle <= aBoo; dutyCycle++){
ledcWrite(MOTOR_UP, dutyCycle);
delay(3);
}
for(int dutyCycle = aBoo; dutyCycle >= 0; dutyCycle--){
ledcWrite(MOTOR_UP, dutyCycle);
delay(3);
}
}
// Callback function LEFT
void onButton2Pressed()
{
Serial.println("LEFT BUTTON PRESSED");
int aBoo = analogRead(POT_PIN);
aBoo = map(aBoo, 0, 4095, 25, MAX_DUTY_CYCLE);
delay(5);
Serial.println(aBoo);
for(int dutyCycle = 0; dutyCycle <= (aBoo/1.5); dutyCycle++){
ledcWrite(MOTOR_CEN, dutyCycle);
delay(3);
}
for(int dutyCycle = (aBoo/1.5); dutyCycle >= 0; dutyCycle--){
ledcWrite(MOTOR_CEN, dutyCycle);
delay(3);
}
delay(300);
for(int dutyCycle = 0; dutyCycle <= aBoo; dutyCycle++){
ledcWrite(MOTOR_LEFT, dutyCycle);
delay(3);
}
for(int dutyCycle = aBoo; dutyCycle >= 0; dutyCycle--){
ledcWrite(MOTOR_LEFT, dutyCycle);
delay(3);
}
}
// Callback function RIGHT
void onButton3Pressed()
{
Serial.println("RIGHT BUTTON PRESSED");
int aBoo = analogRead(POT_PIN);
aBoo = map(aBoo, 0, 4095, 25, MAX_DUTY_CYCLE);
delay(5);
Serial.println(aBoo);
for(int dutyCycle = 0; dutyCycle <= (aBoo/1.2); dutyCycle++){
ledcWrite(MOTOR_CEN, dutyCycle);
delay(3);
}
for(int dutyCycle = (aBoo/1.2); dutyCycle >= 0; dutyCycle--){
ledcWrite(MOTOR_CEN, dutyCycle);
delay(3);
}
delay(300);
for(int dutyCycle = 0; dutyCycle <= aBoo; dutyCycle++){
ledcWrite(MOTOR_RIGHT, dutyCycle);
delay(3);
}
for(int dutyCycle = aBoo; dutyCycle >= 0; dutyCycle--){
ledcWrite(MOTOR_RIGHT, dutyCycle);
delay(3);
}
}
// Callback function DOWN
void onButton4Pressed()
{
Serial.println("DOWN BUTTON PRESSED");
int aBoo = analogRead(POT_PIN);
aBoo = map(aBoo, 0, 4095, 25, MAX_DUTY_CYCLE);
delay(5);
Serial.println(aBoo);
for(int dutyCycle = 0; dutyCycle <= (aBoo/1.35); dutyCycle++){
ledcWrite(MOTOR_CEN, dutyCycle);
delay(3);
}
for(int dutyCycle = (aBoo/1.35); dutyCycle >= 0; dutyCycle--){
ledcWrite(MOTOR_CEN, dutyCycle);
delay(3);
}
delay(300);
for(int dutyCycle = 0; dutyCycle <= aBoo; dutyCycle++){
ledcWrite(MOTOR_DOWN, dutyCycle);
delay(3);
}
for(int dutyCycle = aBoo; dutyCycle >= 0; dutyCycle--){
ledcWrite(MOTOR_DOWN, dutyCycle);
delay(3);
}
}
void setup(){
Serial.begin(BAUDRATE);
Serial.println("abo");
Serial.println(" w ");
button1.begin();
button2.begin();
button3.begin();
button4.begin();
ledcAttach(MOTOR_CEN, freq, res); // channel is chosen automatically
ledcAttach(MOTOR_UP, freq, res); // channel is chosen automatically
ledcAttach(MOTOR_LEFT, freq, res); // channel is chosen automatically
ledcAttach(MOTOR_RIGHT, freq, res); // channel is chosen automaticall
ledcAttach(MOTOR_DOWN, freq, res); // channel is chosen automaticall
// Add the callback functions to be called when the button1 is pressed.
button1.onPressed(onButton1Pressed);
button2.onPressed(onButton2Pressed);
button3.onPressed(onButton3Pressed);
button4.onPressed(onButton4Pressed);
}
void loop(){
int aBoo = analogRead(POT_PIN);
aBoo = map(aBoo, 0, 4095, 50, MAX_DUTY_CYCLE);
//int adcVal = analogRead(PIN_ANALOG_IN); //read adc
//int aBoo = adcVal; // adcVal re-map to pwmVal
button1.read();
button2.read();
button3.read();
button4.read();
delay(50);
}