Rotating using potentiometer issue

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.

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:

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
  }
1 Like

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).

So you can say:

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

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 :slight_smile:

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? :sweat_smile:

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 :thinking:

1 Like

Maybe this helps?

1 Like

not much of an issue however. I can manage