Haptic vibration motor pulses according to keyboard keystrokes

Hi guys !
I'm completely new to this forum and Arduino, so first of all, nice to meet all of you and thank you for taking time to read my problems.

So here is the thing, I'm making this project where I'm trying to control 5 haptic vibration motors with my phone. There will be several patterns of vibration according to which buttons I press on the app.

Right now I'm trying to check the different patterns with the serial monitor but things absolutely not happen the way I want.

I'm using an Arduino Nano V.3 compatible (not an official one), a HC-05 bluetooth module and 5 vibration motors. (I added in the attachments pictures of my wiring).
Here is my code below, what I would like to happen is that when I press "a" or "b" the script which is in the function gets followed.
But when I press "a" the script does 'a' function + 3 times 'b' function, and when I press "b" it does 2 times 'b' function.

This is very annoying, especially because I don't know wether my wiring is wrong of if my code is wrong, in any case I'm a beginner at both.

Thanks for reading and helping !

Lucas

char data = 0; 
const int haptic5 = 5;
const int haptic4 = 3;
const int haptic2 = 9;
const int haptic1 = 11;
const int haptic3 = 6;

void setup() 
{
  Serial.begin(9600);        
}
void loop()
{
  if(Serial.available() > 0) 
  {
    data = Serial.read();      
    Serial.print(data);        
    //Serial.print("\n");
          
    if(data == 'a')
    {           
    analogWrite(haptic1, 100); 
    analogWrite(haptic2, 100); 
    analogWrite(haptic3, 100); 
    analogWrite(haptic4, 100); 
    analogWrite(haptic5, 100); 
    delay(500);
    analogWrite(haptic1, 0); 
    analogWrite(haptic2, 0); 
    analogWrite(haptic3, 0); 
    analogWrite(haptic4, 0); 
    analogWrite(haptic5, 0);
    }

    else if(data == 'b');  
    {        
    analogWrite(haptic1, 255); 
    delay(500);
    analogWrite(haptic1, 0); 
    delay(500);
    analogWrite(haptic1, 255); 
    delay(400);
    analogWrite(haptic1, 0); 
    delay(400); 
    analogWrite(haptic1, 255); 
    delay(300);
    analogWrite(haptic1, 0); 
    delay(300);
    analogWrite(haptic1, 255); 
    delay(200);
    analogWrite(haptic1, 0); 
    delay(200); 
    }
  }
}

 else if(data == 'b');

So do nothing then? Don't use a semicolon after an if.

Grumpy_Mike:

 else if(data == 'b');

So do nothing then? Don't use a semicolon after an if.

Wow, so easy. Thanks a lot, it worked !
Now it only runs the function I want.

But still, for function "a", some motors work and others don't, although when I test them separately everything works fine...

Your schematic shows you're powering these motors directly off the Arduino. Probably if you run more than one at a time they don't have enough power to run.

Lucaszito:

It's a bad idea in general to power motors directly off the Arduino. They produce power spikes when running, and a negative spike when switching off. You may be able to get away with it with such small motors but it's still not safe. Connect them to their own power supply (could be the same as what powers your Arduino, but then with wiring separate from the Arduino) and switch them on/off using a transistor.

For completeness, the other image:

Lucaszito:

Hi,
Have you looked at this?

Tom... :slight_smile: