Potentiometers interfering (code not working)

Hi, I want to control a motor speedcontroller and a servo (I use 2 servo's for testing), for controlling them I use 2 potentiometers so I can control them separately. But when I'm testing it, the servo's goes crazy. But when I disconnect 1 it works. I think that the potentiometers are interfering. I have here my code:

#include <Servo.h>
#include <pins_arduino.h>

Servo servo;
Servo motor;

int potpin1 = A0;
int potpin2 = A1;

int pot1;
int pot2;

void setup() {
  servo.attach(5);
  motor.attach(6);

 Serial.begin(9600);
}

void loop() {
  motor1();
  delay(5);
  servo1();
  delay(5);
}

void motor1() {
  pot1 = analogRead(potpin1);
  pot1 = map(pot1, 0, 1023, 0, 255);

  Serial.println(pot1);
  motor.write(pot1);

}

void servo1() {
  pot2 = analogRead(potpin2);
  pot2 = map(pot2, 0, 1023, 0, 255);

  Serial.println(pot2);
  servo.write(pot2);
}

Can someone help me out?

isn't the range of values to servo write() 0-180, not 255

Don't use motor.attach etc for a servo.

That is a variable right?

didn't work :confused:

How are you powering the servo and motor? If you are powering the servo from the Arduino that is a problem! It can cause the Arduino to reset and may even damage it. You must directly connect the servo to a power supply that can provide enough current.

I'm using a micro servo, those little cheap ones. And they are powered via the Arduino.
And this code works. But this is only 1 servo and 1 potentiometer.

#include <Servo.h>
Servo myservo;

int potpin = A0; 
int val;                                  

void setup() {
  myservo.attach(6); 
  Serial.begin(9600);
}

void loop() {
  val = analogRead(potpin);
  val = map(val, 0, 1023, 0, 255);
  Serial.println(val);
  myservo.write(val);
  delay(5);
}

so I'm a bit confused

how about posting the value read from the pots?

just what I see in the serial monitor?

Indulge us. Provide power to both servos from another source.

Until you do and still fail, no one is going to think other than that there is an issue with that.


Nah, motor is just a lousy name for a servo object.

a7

this is what I see:
162

0

162

0

164

0

162

0

164

0

162

0

162

0

ehhhh wat do you mean?

i'd prefer you report both the raw value read from the pot and well as the mapped value passed as an argument to write()

Yes. If you add the other one it goes crazy right? Put a serial print in your setup() function. You may see that when 2 are connected it resets. It is likely 2 servos are pulling too much current causing the voltage to drop.

ehh yes

Use servo control functions for a servo, not motor control style.

I have tried multiple names but none of them work.

I also tried a power supply but still they are going crazy. When I look at the serial plotter I get spikes from zero to the point where one of the pots are sitting on.

Does this mean it reset?

eh no

a other strange thing is that I see only 1 line on the serial plotter, but I have 2 readings