Potentiometer code goes up to 32 and begins to count down?

I am trying to convert the 0-1023 value of the potentiometer into 0-180 for the servo angle. I am then making one angle and a complementary angle up to 180 degrees. I am getting a negative primary servo angle? it goes up to 32 and then begins to count downwards to 0… Please Help. I need angles of 0-180 for the servo angles.

AlternateServoAnglesc.ino (1.43 KB)

How about dividing the analogRead() by 6, put the results in the 0 to 170 range? Or divide by 6 and add 5, for 5 to 175 range. Most servos won't quite reach their full swing.

CrossRoads:
How about dividing the analogRead() by 6, put the results in the 0 to 170 range? Or divide by 6 and add 5, for 5 to 175 range. Most servos won't quite reach their full swing.

I tested my servos, and it seems that all of them are hitting the full 0-180 degree range. I don't know why, but the potAngle is spitting back only numbers up to 30, and then goes back down and bounces between 0 and 30. is something wrong with my function?

Can't you just use map() for this?

servoAngle = map(potValue, 0, 1023, 0, 180);

Perhaps it is something in your code. But I can’t see your code. More members will see your code if you post your code as described in the forum guidelines.

You have variables with the same names declared in globals, setup() and other functions, fix that and try:

analog_value = pulse_wide / 1000000.0 * FREQUENCY * 4096;

And

servoAngle = potRead * 180.0 / 1023;

Hi,
Ops code in tags

#include <Adafruit_PWMServoDriver.h>
#include <Wire.h>


Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);


#define MIN_PULSE_WIDTH       650
#define MAX_PULSE_WIDTH       2350
#define DEFAULT_PULSE_WIDTH   1500
#define FREQUENCY             50


int potInput;
int potPin = A2;


int servoAngle;
int alternateServo;


int alternateAngle (int x) {
  int y = 180;
  int z;
  z = y - x;
  return abs(z);
}


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("alternate Servo Angle prototype");


  pwm1.begin();
  pwm1.setPWMFreq(FREQUENCY);


  int servoAngle = 0;
  int alternateServo = 180;


  pinMode(potPin, INPUT);
}


void loop() {
  // put your main code here, to run repeatedly:
  potInput = analogRead(potPin);
  Serial.println(potInput);
  servoAngle = potAngle(potInput);
  Serial.println(servoAngle);
  alternateServo = alternateAngle(servoAngle);
  Serial.println(alternateServo);
  pwm1.setPWM(1, 0, pulseWidth(alternateServo));
  pwm1.setPWM(2, 0, pulseWidth(servoAngle));
  delay(500);
}


int pulseWidth(int angle)
{
  int pulse_wide, analog_value;
  pulse_wide   = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
  analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
  return analog_value;
}


int potAngle (int potRead)
{
  int servoAngle;
  servoAngle = int(float((potRead) * 180) / 1023);
  return abs(servoAngle);
}

Tom… :slight_smile:

servoAngle = int(float((potRead) * 180) / 1023);

If potRead is > 182, the red part will overflow integer math on an 8-bit AVR.

Also, ditch the abs() function in the return statement. The reason for the negative results is the above-mentioned integer overflow. Sprinkling in abs() won't fix that. Use uint32_t math.

return abs(servoAngle);

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.