Problem with analogWrite

Hello,

I am working with motorized potentiometers, and I am in the process of controlling them. This must be relatively accurate, which is why my code is a bit complicated. At the moment I first move the fader with full power to the approximate location where I want to have it. Then I take the power out, and drive to the exact position where I want the fader to be.

Heres my Code:

void setMotorPoti(int faderID, int value) {
  int upPin = 22 + (faderID * 2);
  int downPin = upPin + 1;

  while (readPoti(faderID) != value) {
    analogWrite(faderID + 2, 255); //It doesn't Work here
    while (readPoti(faderID) < value - 5 || value + 5 < readPoti(faderID)) {
      int potiValue = readPoti(faderID);
      if (potiValue < value) {
        digitalWrite(upPin, HIGH);
        digitalWrite(downPin, LOW);
      }

      if (potiValue > value) {
        digitalWrite(upPin, LOW);
        digitalWrite(downPin, HIGH);
      }
    }

    while (readPoti(faderID) != value) {
      int potiValue = readPoti(faderID);
      analogWrite(faderID + 2, map(getPositiveInt(value - potiValue), 0, 100, 90, 255) * 1); //It works here
      if (potiValue < value) {
        digitalWrite(upPin, HIGH);
        digitalWrite(downPin, LOW);
      }

      if (potiValue > value) {
        digitalWrite(upPin, LOW);
        digitalWrite(downPin, HIGH);
      }
    }
    digitalWrite(upPin, LOW);
    digitalWrite(downPin, LOW);
    delay(100);
  }
}

The code gets values between 0 and 100. My problem is, I have too much power in the beginning. I use a L293D, so I can change the speed with analogWrite. But this only works in the lower part of the code. If the upper analogWrite (I marked it in the code) has a value other than 255, nothing happens.

Hope you can help me

isi_ko

What Arduino board are you using?

You should post the whole program. Snippets often leave out relevant information.

I am Using a Arduino Mega and heres the Full Code:

int numberOfPotis = 2;
int numberOfButtons = 5;
int potiValues[2] = {0, 0};
int highLowValues[2][2] = {{1023, 0}, {1023, 0}};

void setup() {
  Serial.begin(1000000);

  for (int i = 0; i < numberOfPotis; i++) {
    setPhysicalFaderValueToFull (i);
    highLowValues[i][1] = analogRead(i);
    setPhysicalFaderValueToNull (i);
    highLowValues[i][0] = analogRead(i);
  }
  Serial.println("S;E");
}

void loop() {
  if (Serial.available() > 0 && char(Serial.read()) == 'S') {
    readMsg();
  }
  int newValue;
  for (int i = 0; i < numberOfPotis; i++)
  {
    newValue = readPoti(i);
    if (newValue != potiValues[i]) {
      potiValues[i] = newValue;
      String line;
      line =  "S;0;1;" + String(1) + ";" + String(i) + ";" + String(potiValues[i]) + ";" +  String(newValue) + ";E"; //System: S;commandType(1=getValue);valueType;sensorID;value;debugInfo;E
      Serial.println(line);
    }
  }
}

void setPhysicalFaderValueToNull (int faderID) {
  int upPin = 22 + (2 * faderID);
  int downPin = upPin + 1;
  digitalWrite(downPin, HIGH);
  digitalWrite(upPin, LOW);
  delay(500);
  digitalWrite(downPin, LOW);
}

void setPhysicalFaderValueToFull (int faderID) {
  int upPin = 22 + (2 * faderID);
  int downPin = upPin + 1;
  digitalWrite(upPin, HIGH);
  digitalWrite(downPin, LOW);
  delay(500);
  digitalWrite(upPin, LOW);
}

void setMotorPoti(int faderID, int value) {
  int upPin = 22 + (faderID * 2);
  int downPin = upPin + 1;

  while (readPoti(faderID) != value) {
    analogWrite(faderID + 2, 255);
    while (readPoti(faderID) < value - 5 || value + 5 < readPoti(faderID)) {
      int potiValue = readPoti(faderID);
      if (potiValue < value) {
        digitalWrite(upPin, HIGH);
        digitalWrite(downPin, LOW);
      }

      if (potiValue > value) {
        digitalWrite(upPin, LOW);
        digitalWrite(downPin, HIGH);
      }
    }

    while (readPoti(faderID) != value) {
      int potiValue = readPoti(faderID);
      analogWrite(faderID + 2, map(getPositiveInt(value - potiValue), 0, 100, 90, 255) * 1);
      if (potiValue < value) {
        digitalWrite(upPin, HIGH);
        digitalWrite(downPin, LOW);
      }

      if (potiValue > value) {
        digitalWrite(upPin, LOW);
        digitalWrite(downPin, HIGH);
      }
    }
    digitalWrite(upPin, LOW);
    digitalWrite(downPin, LOW);
    delay(100);
  }
}

void readMsg() {
  boolean msgComplete = false;
  int msgValues[2] = {0, 0};
  int activeSlot = -1;
  while (Serial.available() > 0) {
    char nextChar = char(Serial.read());
    switch (nextChar) {
      case 'E':
        msgComplete = true;
        break;
      case ';':
        activeSlot++;
        break;
      default:
        msgValues[activeSlot] = (msgValues[activeSlot] * 10) + (nextChar - '0');
        break;
    }
    if (nextChar == 'E') {
      break;
    }
  }
  if (msgValues[0] < numberOfPotis && msgValues[1] <= 100) {
    //    while (readPoti(msgValues[0]) != msgValues[1]) {
    setMotorPoti(msgValues[0], msgValues[1]);
    //      delay(10);
    //    }
  }
}

int readPoti(int pin) {
  int iterations = 30;
  int gesammt = 0;
  for (int i = 0; i < iterations; i++) {
    gesammt += analogRead(pin);
  }
  return map(gesammt / iterations, highLowValues[pin][1], highLowValues[pin][0] - 1, 100, 0);
}

int getPositiveInt (int i) {
  if (i < 0) i = i * -1;
  return i;
}