# Rotating using potentiometer issue

**URL:** <https://discourse.processing.org/t/rotating-using-potentiometer-issue/31064>\
**Category:** Coding Questions\
**Created:** [July 5, 2021, 8:26am UTC](https://discourse.processing.org/t/rotating-using-potentiometer-issue/31064 "2021-07-05T08:26:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![millionish](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@millionish](https://discourse.processing.org/u/millionish)\
**Post date:** [July 5, 2021, 8:26am UTC](https://discourse.processing.org/t/rotating-using-potentiometer-issue/31064/1 "2021-07-05T08:26:11Z")

</div>

I could actually implement the rotation of the car using arduino’s potentiometer (like a steering). But the issue is when I rotate the car with potentiometer, it doesn’t drive in that direction, but instead drives in mouseX mouseY direction.

I feel this is one of the lines that’s causing the issue.

```auto
PVector vel = PVector.sub(new PVector(mouseX, mouseY), pos);

```

So, what do I need to change in the code to make it work?

Full code here:

```auto
import processing.serial.*;

Serial myPort; // serial port
PImage carImage;
PImage background;
PVector backgroundPos;
Car car;
boolean move = false;
float speed;

/* STEERING */
float mapval;
int val;

void setup() {
  imageMode(CENTER);
  size(1200, 675);
  carImage = loadImage("car.png");
  background = loadImage("https://www.clipartkey.com/mpngs/m/33-331548_clip-art-oval-oval-race-track-clipart.png");
  int size = 3;
  background.resize(background.width*size, background.height*size);

  car = new Car(width/2, height/2);

  backgroundPos = new PVector(background.width/2, background.height/2);

  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\n');
}

void draw() {
  background(0);
  pushMatrix();
  //scale(2.0);
  translate(backgroundPos.x, backgroundPos.y);
  image(background, 0, 0);
  popMatrix();
  car.show();
  car.update();
}
void moveBackground(PVector vel) {
  vel.x = -vel.x;
  vel.y = -vel.y;

  backgroundPos.add(vel);
}
float angle;
class Car {
  //PImage carImage;
  PVector pos;
  //float angle;
  //float speed;
  Car(float x, float y) {
    //carImage = loadImage("car.png");
    //link to the Picture: https://toppng.com/uploads/preview/aston-martin-one77-01-top-down-car-sprite-1156302814774vtgthfmb.png

    carImage.resize(0, 100);

    pos = new PVector(x, y);
    angle = 0;
    speed = 20;
  }
  void show() {
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(angle);
    image(carImage, 0, 0);
    popMatrix();
  }
  void update() {
    updateAngle();
    //Just try both and decide which one you like more

    //updateMovementFirstExample();
    updateMovementSecondExample();
  }
  void updateAngle() {
    PVector vel = PVector.sub(new PVector(mouseX, mouseY), pos);
    angle = atan2(vel.y, vel.x) + PI / 2;
  }

  void updateMovementFirstExample() {
    if (!keyPressed) return;
    if (key != 'w' && key != 'W') return;

    PVector vel = PVector.sub(new PVector(mouseX, mouseY), pos);

    if (vel.mag() < speed) return;

    //You could do it like this:
    vel.normalize();
    vel.mult(speed);
    pos.add(vel);

    float distToMid = dist(pos.x, pos.y, width/2, height/2);
    if (distToMid > min(width, height)/15) {
      pos.sub(vel);
      moveBackground(vel);
    }
  }
  void updateMovementSecondExample() {
    if (move==false) return;
    //if (key != 'w' && key != 'W') return;

    PVector vel = PVector.sub(new PVector(mouseX, mouseY), pos);

    if (vel.mag() < speed) return;

    vel.normalize();
    vel.mult(speed);
    moveBackground(vel);
  }
}

// to read potentiometer values
void serialEvent(Serial myPort) {
  if (myPort.available() > 0) {
    val = myPort.read();
    mapval = map(val,0,255,0,6.5);
    angle = mapval; // rotate the car
  }

```

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [July 5, 2021, 10:11am UTC](https://discourse.processing.org/t/rotating-using-potentiometer-issue/31064/2 "2021-07-05T10:11:58Z")

</div>

Hey you could use the` PVector.fromAngle()` function.  
Here the Description:  
[**Calculates and returns a new 2D unit vector from the specified angle value (in radians).**](https://processing.org/reference/PVector_fromAngle_.html)

So you can say:

```auto
PVector vel = PVector.fromAngle(angle);
vel.mult(speed);

```

---

<div class="post-metadata">

**Author:** ![millionish](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@millionish](https://discourse.processing.org/u/millionish)\
**Post date:** [July 5, 2021, 11:25am UTC](https://discourse.processing.org/t/rotating-using-potentiometer-issue/31064/3 "2021-07-05T11:25:45Z")

</div>

cool it works, but after running for sometime, the program throws an error “Error, disabling serialEvent() for COM4, null”. Any idea?

Also, the game lags a bit so is there anything I can do to make it run smoother? If not, then it’s okay it’s manageable 🙂

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [July 5, 2021, 11:52am UTC](https://discourse.processing.org/t/rotating-using-potentiometer-issue/31064/4 "2021-07-05T11:52:30Z")

</div>

No, unfortunately, I am not very familiar with Arduino processing…  
Maybe you could create a new topic.

Can you send me the Car.png image? 😅

Then I could try if it works on my MacBook from 2011.  
Also have you already tried to run the program without the Arduino?  
Maybe it is because of the serial port that it is lagging 🤔

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [July 5, 2021, 11:53am UTC](https://discourse.processing.org/t/rotating-using-potentiometer-issue/31064/5 "2021-07-05T11:53:55Z")

</div>

Maybe this helps?

> <https://stackoverflow.com/questions/26070466/arduino-and-processing-code-error-disabling-serialevent>

---

<div class="post-metadata">

**Author:** ![millionish](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@millionish](https://discourse.processing.org/u/millionish)\
**Post date:** [July 5, 2021, 12:30pm UTC](https://discourse.processing.org/t/rotating-using-potentiometer-issue/31064/6 "2021-07-05T12:30:05Z")

</div>

![car](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4a94d9fd08808244c48289b74bde0b64c1d02ea0.png)

---

<div class="post-metadata">

**Author:** ![millionish](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@millionish](https://discourse.processing.org/u/millionish)\
**Post date:** [July 5, 2021, 12:30pm UTC](https://discourse.processing.org/t/rotating-using-potentiometer-issue/31064/7 "2021-07-05T12:30:38Z")

</div>

not much of an issue however. I can manage
