1

Here's my Code so far:

void setup() {
  Serial.begin(9600);
}

void loop() {
  float a;
  float angle;
  int setpoint;
  a = analogRead(A1);
  angle = map(a, 0, 1023, 0, 180);

  if ( (angle>0) && (angle<70) )
  {
    setpoint=50;
  }
  if ( (angle>70) && (angle<110) )
  {
    setpoint=90;

  }
  if ( (angle>110) && (angle<180) )
  {
    setpoint=130;

  }

  Serial.print(setpoint);
  Serial.print(",");
  Serial.println(angle);
}

I'm using an Arduino Genuino.

How can I get the servo motor to move to the setpoints and stop when the angle is between the eligible range?

just wanted to update showing how i solved this problem using the following code.

void setup() {
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
Serial.begin(9600);
}
void loop() {
float a;
float b;
float desired_angle;
float actual_angle;
float error;
float motor;
float setpoint;

a=analogRead(A1);
desired_angle=map(a,0,1023,0,180);

b=analogRead(A2);
actual_angle=map(b,125,920,0,180);

 if ( (desired_angle>0) && (desired_angle<70) )
{
  setpoint=50;
}
if ( (desired_angle>70) && (desired_angle<110) )
{
  setpoint=90;

}
if ( (desired_angle>110) && (desired_angle<180) )
{
  setpoint=130;

}

error=(setpoint-actual_angle);
motor=desired_angle/270*255;


if (error>0)
{
  digitalWrite(5,LOW);
  analogWrite(6,motor);
}
if (error<0)
{
  digitalWrite(6,LOW);
  analogWrite(5,-1*motor);
}

Serial.print(actual_angle);
Serial.print(",");
Serial.println(setpoint);


}
Somerled
  • 21
  • 1
  • 3

0 Answers0