Rotating car 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
  }

That looks like Processing code

This is an Arduino forum

Do you see the problem ?

Hi,
Welcome to the forum.
Something like this site might be able to help;

Tom... :smiley: :+1: :coffee: :australia:

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