joystick throttle roll — ESP32 simulation

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

Components

Sketch code

const int VERT = 35;
const int HOR  = 34;

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

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

void loop() {

  // Read joystick
  int rollInput = analogRead(VERT);
  int throttleInput = analogRead(HOR);

  // -------------------------
  // 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);


  // -------------------------
  // MOTOR MIXING
  // -------------------------

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

  // Roll adjustment

  motor1 += roll;
  motor4 += roll;

  motor2 -= roll;
  motor3 -= roll;


  // Keep motor values between 0 and 255

  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(" | 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