Alex — ESP32 simulation

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

Components

Sketch code

#include <Arduino.h>
#include <Preferences.h>
#include <esp_system.h>
#include <time.h>

// ============================================================================
// 1. PIN DEFINITIONS
// ============================================================================
const int SEG_A = 4;
const int SEG_B = 5;
const int SEG_C = 15;
const int SEG_D = 2;
const int SEG_E = 18;
const int SEG_F = 19;
const int SEG_G = 21;

const int DIG1 = 22;
const int DIG2 = 23;
const int DIG3 = 25;
const int DIG4 = 26;

const int BTN_DOWN  = 27;
const int BTN_ENTER = 32;
const int BTN_UP    = 33;

const int segPins[7] = {SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G};
const int digitPins[4] = {DIG1, DIG2, DIG3, DIG4};

const bool ENABLE_SENSOR_IO = false;
const bool ENABLE_RELAY_OUTPUTS = true;

const int PRESSURE_ADC_PIN = 36;
const int OIL_MIST_ADC_PIN = 39;
const int OPTICAL_ADC_PIN  = 13;

const int SPEED_INPUT_PIN = 34;
const int RUN_CONTACT_PIN = 35;

const int RELAY_LOAD_PIN     = 12;
const int RELAY_PREALARM_PIN = 14;
const int RELAY_FAILURE_PIN  = 16;
const int RELAY_TRIP_PIN     = -1;

const bool RUN_CONTACT_ACTIVE_LOW = true;
const float SHUNT_OHMS = 165.0f;
const float ADC_GAIN = 1.0f;
const float ADC_OFFSET_V = 0.0f;
const float OPTICAL_E08_MA = 19.5f;

// ============================================================================
// 2. DATA STRUCTURES & ENUMS (DEFINED FIRST FOR C++ COMPLIANCE)
// ============================================================================
struct Button {
  int pin;
  bool stable;
  bool raw;
  uint32_t rawChanged;
  uint32_t pressedAt;
  bool pressEvent;
  bool longEvent;
  bool longDelivered;
};

struct Parameter {
  int number;
  long value;
  long min;
  long max;
  long step;
  uint8_t decimals;
};

enum CodeId {
  NONE, H04, H05, H09, H10, H11,
  E01, E02, E03, E04, E05, E06, E08, E10, E42
};

enum UiMode {
  NORMAL, SELECT_PARAM, EDIT_PARAM
};

enum TestState {
  T_IDLE, T_P42, T_WAIT10, T_WAIT5, T_TRIGGER, T_H09, T_H11, T_H10, T_DONE
};

// ============================================================================
// 3. EXPLICIT FUNCTION PROTOTYPES
// ============================================================================
void updateButton(Button &b);
bool takePress(Button &b);
bool takeLong(Button &b);
bool isDown(const Button &b);

void digitsOff();
void segmentsOff();
void clearDisplay();
void setSymbol(int pos, const byte symbol[7]);
void setNumber(int pos, int n);
void showPxx(int p);
void showUnsigned4(long v);
void showSigned4(long v);
void showCode(char prefix, int n);
void show8888();
void refreshDisplay();

int findParameter(int n);
long pValue(int n);
void saveParameters();
void loadParameters();
void factoryDefaults();

float loopmA(int pin);
float pressureFrommA(float mA);
float pressureRaw();
float opticalmA();
float oilIncreaseRaw();

void updateSpeed();
bool runContact();
bool engineRunning();

void applyClock();
void loadRtc();
void saveRtc();

void updateBaseline();
void updateFilte