Hi,
so I have a dc motor and a servo each controlled with a different potentiometer.First I made two separate codes and all works fine, but when I combine the two codes the servo starts getting readings from both pots.Here are the working codes
int motorPin = 9;
int potpin = 0;
int val;
void setup()
{
pinMode(potpin,INPUT);
pinMode(motorPin, OUTPUT);
}
void loop()
{
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 255);
analogWrite(motorPin, val);
delay(15);
}
AND
#include <Servo.h>
Servo myservo;
int potpin = 5;
int val;
void setup()
{
myservo.attach(3);
}
void loop()
{
val = analogRead(potpin);
if (val<300){
myservo.write(67);
}else if (val<600){
myservo.write(89);
} else {
myservo.write(109);
}
delay(15);
}
Heres the combined code that doesnt work.If you see whats wrong please reply, couse I cant find the mistake here.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int motorPin = 9;
int potpin1 = 5; // GEARS
int potpin = 0; // SPEED
int val; // variable to read the value from the analog pin
void setup()
{
pinMode(potpin1,INPUT); //GEARS
pinMode(potpin,INPUT); //SPEED
pinMode(motorPin, OUTPUT);
myservo.attach(3); // attaches the servo on pin 9 to the servo object
}
void loop()
{
val = analogRead(potpin); //SPEED
val= map(val, 0, 1023, 0, 255);
analogWrite(motorPin, val);
delay(15);
val = analogRead(potpin1); // GEARS
if (val<300){
myservo.write(130);
}else if (val<600){
myservo.write(110);
} else {
myservo.write(90);
}
delay(15);
}
ALSO HERES A VIDEO SHOWING THE APLICATION OF THE FIRST TWO CODES

