Need help: Running vibrators in sequence

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);
}


Not looked the code ( but see you are declaring the variable “boo” many times , that’s wrong ) but not a good idea to use your processor board to power those motors - it’s not capable . When making such projects , test and get working one part ( eg the pot ) at a time then add it in .
Never used easy button , does it need pull-ups on the inputs ?

Unless you know why you need to do that, you don't need to do it.

Thanks, I'm not particularly experienced when it comes to coding (as if that wasn't obvious) :sweat_smile:

Appreciate the feedback and I am certain this could all be written in a much much cleaner way.

(Re-)Declaring the variable in every callback was what made everything work (almost).
I began by just declaring it once and had no luck with that.
I don't doubt that you are correct however, I guess I've made some other mistake(s) and that I managed to bypass by doing it like that.

EasyButton was the library I felt was working best for me, I tried several others as well.

It uses the internal pull-ups on the esp32

You should write a description that describes what shall happens as a sequence of conditions and actions.

If button1 is pressed

  1. .......
  2. ......
  3. ......

and / or draw a timing-digram that shows how the different things happen over time.

Trying to understand this from your code is not possible because your code does not yet what it should. So a timing-diagram derived from your code would be wrong

Sure, its not complicated.

If button.UP is pressed:
 1. Run Motor.CENTER (at 50% PWM and/or duration), then stop
 2. Short Delay 
 3. Run Motor.UP (at 100%) then stop.

That's it, the other buttons are the same with the exception of which motor to run at "step #3."
Like:

    if button.RIGHT is pressed:
 1. Run Motor.CENTER (50%) and stop
 2. Short Delay 
 3. Run Motor.RIGHT (100%) and stop.

and the same for the DOWN and LEFT buttons.

Weirdly enough the code kinda does what it should, since it works with all the LEDs.
As well running the different motors in "step #3". works, but no matter what I try it just won't run the center-motor in "step #1."

It's also incomplete.

What should happen if two buttons are pressed simultaneously?
What should happen if a second button is pressed during Step 1 of the first button?
What should happen if a second button is pressed during Step 2 of the first button?
What should happen if a second button is pressed during Step 3 of the first button?
.
.
.
etc?

@dubbeltrubbel
Every time you declare aBoo with a type (in this case int) another unrelated instance of aBoo is created. I count seven.

The fix for that is to create one global int aBoo variable, and remove the int type prefixing the aBoo in all the functions.

aBoo = analogRead(POT_PIN);
1 Like

True, but those situations doesn't really matter, at least not at this stage.
For now I suppose it would be preferable if nothing at all happened in all the instances you mentioned

Ahaa alright! Thanks, I will try that out right now

It's fixed now, but the result is the same as before unfortunately.
Good to know it's less of a mess at least :slight_smile:

Is the result "motor one" does not vibrate (or whatever it does)? I would suspect wiring is not holding together, especially if it is vibrating.

Yeah I suspected that at first too, but I've triple-checked everything as well as switching cables between the motors etc. The common motor (center.motor) is working. I just can't get it to spin in this sequence for some reason :confused:
if I instead run it as the second in the sequence, then it buzzes like it should, but it's impossible for me to get a sequence of two motors buzzing after each other

Okay, the bad motor is not bad (it works).

Is the first motor channel broken?

At the same time, or one motor, then the second motor?

Why don't you write a tiny small testcode that does nothing more than

motor 1 on
delay
motor 1 off
motor 2 on
delay
motor 2 off

just to make sure the motor itself is OK.

As the next step

write a testcode that does not register your functions
onButton1Pressed()
onButton2Pressed()
onButton3Pressed()
onButton4Pressed()
as callbackfunctions

Just call them directly from loop()

Sorry for the late reply, been away over the Easter.

I've tried your suggestions now (very logical steps)

I made a simplified version of the code without the buttons and functions.
When running all four sequences manually in the loop it works as it should!
First the Center Motor runs, then the other one.

Then I tried a code with the functions but without buttons.
(Hope I understood you correctly)

First below is just the function, same as before. Same code that works when it's run directly in the loop

void GoUp()
{
  aBoo = analogRead(POT_PIN);
  aBoo = map(aBoo, 0, 4095, 25, MAX_DUTY_CYCLE);
  delay(5);
  Serial.println(aBoo);
  
// RUN CENTER MOTOR
for(int dutyCycle = 0; dutyCycle <= (aBoo/3); dutyCycle++){   
    ledcWrite(MOTOR_CEN, dutyCycle);
    delay(3);
  }
  for(int dutyCycle = (aBoo/3); dutyCycle >= 0; dutyCycle--){
    ledcWrite(MOTOR_CEN, dutyCycle);   
    delay(3);
  }
    delay(300);
// RUN UP MOTOR
    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);
}
delay(2000);
}

Then I called them in the loop as such:

void loop(){
  GoUp();
  delay(2000);

Result is the same as before, only the 2nd motor runs, never the center motor

So there's something wrong with my functions I suppose?
Can't wrap my head around this, something is strange here for sure

Please, post the full sketch that you are using in the description(s).