Servo Motor and Ammeter controlled by Potentiometer

I have some code to control both a servo motor and a Ammeter (PWM) however I am having difficulty running both at the same time. When I enable the line


i no longer am able to control the PWM with a potentiometer. It becomes either all on or all off. Any advice would be appreciated. I am sure there must be something conflicting between them because i can get them to run separately.

Here is my code that I have.

#include <Servo.h>

//// Servos
#define rollPin     10      // Roll Pin

// Define Pot Pins

#define deflrPotPin    A7   // Define Deflector L/R

#define j1xPotPin      A2   // Define Joy 1 XAxis Roll


Servo roll; 

int angleRoll = 540;     // 90 deg * 6

int potstate, lrDefvalue, lrDefvoltage;

//// Define Pot Matrix

int deflrMat = 0;    // Deflector L/R
int j1xMat = 0;      // Roll


int potToChange (int reading, int potstate)
    if (abs(reading - potstate) > 0) {
      potstate = reading;
      //Serial.println(reading, DEC);
      //Serial.println(potstate[pot], DEC);
      return potstate;

// Define Pot Pins

void deflrPot ()     //    A7   // Define Deflector L/R
  int reading;

  reading = analogRead(deflrPotPin);
  potstate = potToChange(reading, potstate);
//  Serial.println(reading, DEC);  // 145
//  Serial.println(potstate, DEC);  //145
  lrDefvalue = potstate;
//  Serial.println(lrDefvalue, DEC);  //145
  lrDefvoltage = map(lrDefvalue, 1023, 0, 0, 255);  // Convert speed to PWM duty cycle
//  Serial.println(lrDefvoltage, DEC);  // 35
  analogWrite(lrDefPin, lrDefvoltage);

void j1xPot() //     A2   // Define Joy 1 XAxis Roll
  int reading;
  reading = analogRead(j1xPotPin);
  potstate = potToChange(reading, potstate);
  if (potstate >= 6) {
      angleRoll = potstate / 6;
  else {
      angleRoll = 2;
  Serial.println(angleRoll, DEC);

void setup() {
  //start serial

  //Define pin modes
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);

// Define Servo Motors


void loop() {

  deflrPot ();

  //delay so all these print satements can keep up. 


//------------------------------------------------end main loop

You have this line

analogWrite(lrDefPin, lrDefvoltage);

but I can't see where lrDefPin is defined

Have you posted the complete program? If not, please do so.


Your posted code does not compile, not least because it is missing several pin definitions. So it's not exactly surprising that it doesn't work.

Please post the actual code that gives the problem you are describing.


sorry i tried to take the extra stuff out to make it easier. i attached the entite code

falcon2.ino (12 KB)

I think the problem is that the servo library is taking over the HardwareTimer that you are trying to use for analogWrite(). Check the documentation for the Servo library.


See Servo - Arduino Reference


See Servo - Arduino Reference


So I took a look at this site and I am using a Mega and this is what I read.

"On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12."

I would think it shouldn't be affect the PWM but wasn't sure

On what pin is analogWrite() not working when you attach the servo?

Have you checked to see if analogWrite() works properly on other PWM pins?


I tried it on a different pin and it seemed to work. I think that was my problem.