joystick rolll throttle pitch — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by josephkinuthia541.

Components

Sketch code

const int ROLL = 35;
const int THROTTLE = 34;

const int PITCH = 32;

const int CENTER = 2047;
const int DEADZONE = 300;

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

void loop() {

  // =========================
  // READ CONTROLS
  // =========================

  int rollInput = analogRead(ROLL);
  int throttleInput = analogRead(THROTTLE);
  int pitchInput = analogRead(PITCH);


  // =========================
  // THROTTLE
  // =========================

  int throttle = 0;

  if (throttleInput < CENTER - DEADZONE) {

    throttle = map(
      throttleInput,
      CENTER - DEADZONE,
      0,
      0,
      255
    );
  }

  throttle = constrain(throttle, 0, 255);


  // =========================
  // ROLL
  // =========================

  int roll = 0;

  if (rollInput < CENTER - DEADZONE) {

    roll = map(
      rollInput,
      CENTER - DEADZONE,
      0,
      0,
      100
    );

  }
  else if (rollInput > CENTER + DEADZONE) {

    roll = map(
      rollInput,
      CENTER + DEADZONE,
      4095,
      0,
      -100
    );
  }

  roll = constrain(roll, -100, 100);


  // =========================
  // PITCH
  // =========================

  int pitch = 0;

  if (pitchInput < CENTER - DEADZONE) {

    pitch = map(
      pitchInput,
      CENTER - DEADZONE,
      0,
      0,
      100
    );

  }
  else if (pitchInput > CENTER + DEADZONE) {

    pitch = map(
      pitchInput,
      CENTER + DEADZONE,
      4095,
      0,
      -100
    );
  }

  pitch = constrain(pitch, -100, 100);


  // =========================
  // START WITH THROTTLE
  // =========================

  int motor1 = throttle;
  int motor2 = throttle;
  int motor3 = throttle;
  int motor4 = throttle;


  // =========================
  // ROLL MIXING
  // =========================

  motor1 += roll;
  motor4 += roll;

  motor2 -= roll;
  motor3 -= roll;


  // =========================
  // PITCH MIXING
  // =========================

  motor1 += pitch;
  motor2 += pitch;

  motor3 -= pitch;
  motor4 -= pitch;


  // =========================
  // LIMIT MOTORS
  // =========================

  motor1 = constrain(motor1, 0, 255);
  motor2 = constrain(motor2, 0, 255);
  motor3 = constrain(motor3, 0, 255);
  motor4 = constrain(motor4, 0, 255);


  // =========================
  // DISPLAY
  // =========================

  Serial.print("THROTTLE: ");
  Serial.print(throttle);

  Serial.print(" | ROLL: ");
  Serial.print(roll);

  Serial.print(" | PITCH: ");
  Serial.print(pitch);

  Serial.print(" | M1: ");
  Serial.print(motor1);

  Serial.print(" | M2: ");
  Serial.print(motor2);

  Serial.print(" | M3: ");
  Serial.print(motor3);

  Serial.print(" | M4: ");
  Serial.println(motor4);

  delay(100);
}

More projects by josephkinuthia541