wireless comm + failsafe — ESP32 simulation

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

Components

Sketch code

#include <Arduino.h>

// =========================
// CONTROLLER JOYSTICK PINS
// =========================
const int THROTTLE_PIN = 34;
const int ROLL_PIN     = 35;
const int PITCH_PIN    = 32;
const int YAW_PIN      = 33;

// Joystick center
const int CENTER = 2047;

// Deadzone
const int DEADZONE = 300;


// =========================
// CONVERT JOYSTICK TO -100..100
// =========================
int joystickToPercent(int raw)
{
  int value = raw - CENTER;

  // Deadzone
  if (abs(value) < DEADZONE)
  {
    return 0;
  }

  int output = map(raw, 0, 4095, -100, 100);

  return constrain(output, -100, 100);
}


// =========================
// THROTTLE 0..255
// =========================
int throttleToPWM(int raw)
{
  int throttle = map(raw, 0, 4095, 255, 0);

  return constrain(throttle, 0, 255);
}


// =========================
// SETUP
// =========================
void setup()
{
  Serial.begin(115200);

  pinMode(THROTTLE_PIN, INPUT);
  pinMode(ROLL_PIN, INPUT);
  pinMode(PITCH_PIN, INPUT);
  pinMode(YAW_PIN, INPUT);

  Serial.println();
  Serial.println("================================");
  Serial.println("   ESP32 #002 CONTROLLER TEST");
  Serial.println("================================");
  Serial.println("Joystick test starting...");
  Serial.println();
}


// =========================
// LOOP
// =========================
void loop()
{
  // Read joysticks
  int throttleRaw = analogRead(THROTTLE_PIN);
  int rollRaw     = analogRead(ROLL_PIN);
  int pitchRaw    = analogRead(PITCH_PIN);
  int yawRaw      = analogRead(YAW_PIN);

  // Convert values
  int throttle = throttleToPWM(throttleRaw);
  int roll     = joystickToPercent(rollRaw);
  int pitch    = joystickToPercent(pitchRaw);
  int yaw      = joystickToPercent(yawRaw);

  // Display results
  Serial.print("RAW  | T:");
  Serial.print(throttleRaw);

  Serial.print(" R:");
  Serial.print(rollRaw);

  Serial.print(" P:");
  Serial.print(pitchRaw);

  Serial.print(" Y:");
  Serial.print(yawRaw);

  Serial.print("   ||   COMMAND  | T:");
  Serial.print(throttle);

  Serial.print(" R:");
  Serial.print(roll);

  Serial.print(" P:");
  Serial.print(pitch);

  Serial.print(" Y:");
  Serial.println(yaw);

  delay(200);
}

More projects by josephkinuthia541