Motor takes few ms to turn on

I’m using the following arduino code:

const int ledPin = 2;
const int ledPin2 = 13;
const int ledPin3 = 4;
const int motorPin = 3;
const int motorPin2 = 10;

int incomingByte = 0;
int state = 0;

void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(motorPin, OUTPUT);
  pinMode(motorPin2, OUTPUT);

  digitalWrite(ledPin, LOW);  // start with LED off
  digitalWrite(ledPin2, LOW);  // start with LED off
  digitalWrite(ledPin3, LOW);  // start with LED off


  Serial.setTimeout(10);
}

void loop() {
  // Check if data is available
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    state = Serial.parseInt();

    
    // Check the received value
    if (incomingByte == 'A') {
      
      digitalWrite(ledPin, state);
    } 
  }
     // Check the received value
    if (incomingByte == 'B') {
      
      digitalWrite(ledPin2, state);
    } 
    if (incomingByte == 'C') {
      
      digitalWrite(ledPin3, state);
    } 
    if (incomingByte == 'D') {
      
      analogWrite(motorPin, state);
    } 
      if (incomingByte == 'E') {
      
      analogWrite(motorPin2, state);
    } 
}

from max map I’m sending the letter D follow by either 0 or 255

the moment I press D 255 the motor is buzzing for few ms, stops to few ms more then actually start vibrating at full power.

What can cause this?

I try both with non 2n2222a transistor as well with FQPN06L MOSFET (see schematic)

edit:

I try both with the R3 pulldown resistor and without. It worked the same in both cases. Do I need to use it or I can go without?

the circuit works (apart of what I described in my topic) - in that case it is better to use the mosfet or the 2n222a transistor?

The Serial.parseInt(); function returns the int when a character is received that is not part of an int, such as a Carriage Return or Linefeed. If no characters are received then after a timeout period the function returns what it has received so far. Could this be the cause of the problem ?

You can change the timeout period using the setTimeout() function

I do have this line: Serial.setTimeout(10);
I change it to (3) and still the same

How is the input string terminated, if at all ?

I’m not sure the question

Is the string immediately followed by any other non numeric characters ?

What do you mean by "max map" ?

I’m sending either 68 32 48 or 68 32 50 53 53

when sending 68 32 48 - the motor stops immediately , when sending 68 32 50 53 53 - the motor takes few ms to start

lets put aside MaxMSP software because I also check it from the arduino serial and the same problem happens. When sending D 255 motor takes few ms to start. when sending D 0 - motor stops immediately

edit: I also checked different motor and same problem happens

Actually, from what you say, you are sending a D followed by a space followed by 0 or 255. What is the purpose of the space ?

Your code seems to be using data types in an odd way. For instance

int incomingByte = 0;

Why isn't that

char incomingChar;

After ll, you are receiving a character and naming an int variable as incomingByte makes no sense

I clean the code and change to byte, still the same problem:

const int motorPin = 3;

char incomingByte = 0;
int state = 0;

void setup() {
  Serial.begin(115200);
  pinMode(motorPin, OUTPUT);
  Serial.setTimeout(2);
}

  void loop() {
    // Check if data is available
    if (Serial.available() > 0) {
      incomingByte = Serial.read();
      state = Serial.parseInt();


      // Check the received value
      if (incomingByte == 'D') {

        analogWrite(motorPin, state);
      }
    }
  }


I still don't like the name of your char variable, but the compiler won't care

Have you tried taking out the space for which you have not explained the purpose ?

I made this space for easier reading. you mean to send D0 or D255 ? what should I change in the code?

Yes, I mean send D0 or D255 but do not change anything in the code

Try it first using the Serial Monitor set to "No Line Ending"

I assume that you have MaxMSP, whatever that is, connected to the Tx and Rx pins of whatever Arduino you are using. This is not generally a good idea

I did as suggested:

// 
const int motorPin = 3;

void setup() {
  Serial.begin(115200);
  pinMode(motorPin, OUTPUT);
  Serial.setTimeout(5);
}

void loop() {
  if (Serial.available()) {
    char cmd = Serial.read();  

    if (cmd == 'D') {
      int value = Serial.parseInt();  
      value = constrain(value, 0, 255);
      analogWrite(motorPin, value);
    }
  }
}

and use the Serial Monitor with “No Line Ending”

Same problem

How it behaves when you connect Motor1 manually to VCC?

actually with the Same problem!

So worthless to play with code.
Post info about your motor, power supplies and physical wiring.

Add a drop of WD-40 to each of the bearings and try again several times.

Thank you all, problem solved. I change the PSU (I’m using phone chargers) to a difference one and it solved it!

regarding my question at my initial posts:

I try both with the R3 10k pulldown resistor and without. It worked the same in both cases. Do I need to use it or I can go without?

the two circuit works(MOSFET and transistor) - in that case it is better to use the MOSFET or the 2n222a transistor?

my thought of why using the MOSFET - is that if I would want to use different motors that require much more current to work I could still make it work with the MOSFET while the 2n2222a is much more limited in the current it can pass.

Depends on your motor.
(depends on the mosfet you have, even on the base resistor of your transistor).
Why you ask us to guess?

Edit: FQP30N06L looks like better overall choice.