# Needle sweep. I have needle going to 180 degrees, but I need it to sweep back to 0

**URL:** <https://discourse.processing.org/t/needle-sweep-i-have-needle-going-to-180-degrees-but-i-need-it-to-sweep-back-to-0/15473>\
**Category:** Coding Questions\
**Created:** [November 14, 2019, 1:09am UTC](https://discourse.processing.org/t/needle-sweep-i-have-needle-going-to-180-degrees-but-i-need-it-to-sweep-back-to-0/15473 "2019-11-14T01:09:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DonnieBrasco](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@DonnieBrasco](https://discourse.processing.org/u/DonnieBrasco)\
**Post date:** [November 14, 2019, 1:09am UTC](https://discourse.processing.org/t/needle-sweep-i-have-needle-going-to-180-degrees-but-i-need-it-to-sweep-back-to-0/15473/1 "2019-11-14T01:09:34Z")

</div>

```auto
//Define Global Variables

float x, y; //updating coordinates
float a = 0; //find theta angle
float r = 200; //radius

void setup(){
  size(640,480);

}
void draw(){
 background(#6E96A7); //bluish background
 drawCoordinateAxes();
 updateServoHornPosition();
 drawServoHorn();
 
}

void drawCoordinateAxes(){
  stroke(0);
  translate(width/2,height/2);
  line(0,height,0,-height);
  line(-width,0,width,0);
  fill(0,255,0);
  text("-X",width/-2, 15);
  fill(0,255,0);
  text("X",width/2.1,15);//2.1 to keep on edge
  fill(255,0,0);
  text("Y", 15, height/-2.1);
  fill(255,0,0);
  text("-Y", 15, height/2.1);
}

void updateServoHornPosition(){
       
  float angle = radians(a); //convert to radians
    x = r*cos(angle);
    y = r*sin(angle);
  
  print("x=",x);//print x coordinates
  println();//next line
  print("y=",y);//print y coordinates 
  println();//add line
  
  a = a - 1; //update theta
  
  if(a == -180) //range (theta = (0-180] degrees)
  {
       a = a +1;  
  }
 
 

  
 
}
void drawServoHorn()
{
  strokeWeight(3);
  stroke(#F50C0C);
  line(0,0,x,y);
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 14, 2019, 1:29am UTC](https://discourse.processing.org/t/needle-sweep-i-have-needle-going-to-180-degrees-but-i-need-it-to-sweep-back-to-0/15473/2 "2019-11-14T01:29:26Z")

</div>

```auto
a = a - 1; //update theta 
if(a == -180) //range (theta = (0-180] degrees) 
{ a = a +1; }

```

you are printing x and y  
but not a ( outside and inside the IF )  
then you might understand the problem

change to thinking of direction:

```auto
if a > 180 dir = -
if a < -180 dir = +
a +=dir

```

---

<div class="post-metadata">

**Author:** ![DonnieBrasco](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@DonnieBrasco](https://discourse.processing.org/u/DonnieBrasco)\
**Post date:** [November 14, 2019, 1:37am UTC](https://discourse.processing.org/t/needle-sweep-i-have-needle-going-to-180-degrees-but-i-need-it-to-sweep-back-to-0/15473/3 "2019-11-14T01:37:29Z")

</div>

> [@kll](#):
>
> if a \> 180 dir = - if a \< -180 dir = + a +=dir

what would be the correct syntax for this?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 14, 2019, 1:47am UTC](https://discourse.processing.org/t/needle-sweep-i-have-needle-going-to-180-degrees-but-i-need-it-to-sweep-back-to-0/15473/4 "2019-11-14T01:47:20Z")

</div>

try like

```auto
//Define Global Variables
float x, y; //updating coordinates
float dir = 1,a = 0; //find direction and theta angle
float r = 200; //radius

boolean diagp = true;

void setup() {
  size(640, 480);
}

void draw() {
  background(#6E96A7); //bluish background
  drawCoordinateAxes();
  updateServoHornPosition();
  drawServoHorn();
}

void drawCoordinateAxes() {
  stroke(0);
  translate(width/2, height/2);
  line(0, height, 0, -height);
  line(-width, 0, width, 0);
  fill(0, 255, 0);
  text("-X", width/-2, 15);
  fill(0, 255, 0);
  text("X", width/2.1, 15);//2.1 to keep on edge
  fill(255, 0, 0);
  text("Y", 15, height/-2.1);
  fill(255, 0, 0);
  text("-Y", 15, height/2.1);
}

void updateServoHornPosition() {
  x = r*cos(radians(a));
  y = r*sin(radians(a));
  if ( a > 0 ) dir = -1;
  if ( a < -180) dir = 1;
  a += dir; //update theta
  if ( diagp ) println("x "+x+" y "+ y+" a "+a);
}

void drawServoHorn() {
  strokeWeight(3);
  stroke(#F50C0C);
  line(0, 0, x, y);
}

```
