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