Hi! I need a servo and a DC motor working at the same time, each one controlled by a potentiometer (and a mosfet for the DC motor), as you can see in the picture that I attached.
If I move just the servo, it works fine, but when I switch on the DC motor, the servo goes to 0º automatically untill I stop the DC motor.
I simulated it on 123D circuits and it works. I read on other forum that it is impossible to use pin 9 and 10 for PWM when you´re using the servo library, so I didn´t use those, I tried all the pin combinations but it doesn´t work well.
I hope you can help me. Thanks for your time
#include <Servo.h>
Servo myServo;
int const potPin = A5; // analog pin used to connect the potentiometer
int potVal; // variable to read the value from the analog pin
int angle;
const int potMotPin = A0;
const int motorPin = 11;
const int servoPin = 9;
int potValor;
int ajusteVel;
void setup() {
pinMode(motorPin, OUTPUT);
pinMode (potMotPin, INPUT);
myServo.attach(servoPin);
Serial.begin(9600);
}
void loop() {
potValor = analogRead(potMotPin);
Serial.println(potValor);
ajusteVel = potValor / 4;
delay(5);
analogWrite(motorPin, ajusteVel);
potVal = analogRead(potPin); // read the value of the potentiometer
// print out the value to the serial monitor
Serial.print("potVal: ");
Serial.print(potVal);
// scale the numbers from the pot
angle = map(potVal, 0, 1023, 0, 179);
// print out the angle for the servo motor
Serial.print(", angle: ");
Serial.println(angle);
// set the servo position
myServo.write(angle);
// wait for the servo to get there
delay(15);
}