Fixing ESP32 Code Issues


Below is a version rewritten using the standard Arduino functions available on the ESP32. It replaces the custom FluxAPI calls with the native functions:

Cpp


// Define input and output pins
const int inputs[] = {15, 18, 19, 21, 34, 35};
const int outputs[] = {22, 23, 25, 26, 27, 32};

// Number of pins in the arrays
const int numInputs = sizeof(inputs) / sizeof(inputs[0]);
const int numOutputs = sizeof(outputs) / sizeof(outputs[0]);

// Track previous input states for edge detection (initialize all to LOW)
bool previousInputStates[numInputs] = {false};

// LED blinking state variables
bool ledState = false;
unsigned long lastBlinkTime = 0;

// Initialize all pins
void initializePins() {
  // Initialize output pins
  for (int i = 0; i < numOutputs; i++) {
    pinMode(outputs[i], OUTPUT);
    digitalWrite(outputs[i], LOW); // Set to LOW
  }
  // Initialize input pins with internal pulldown
  for (int i = 0; i < numInputs; i++) {
    pinMode(inputs[i], INPUT_PULLDOWN);
  }
}

// Non-blocking update of outputs based on elapsed time
void updateOutputs() {
  unsigned long currentTime = millis();
  if (currentTime - lastBlinkTime >= 500) {  // Toggle every 500 ms
    ledState = !ledState;
    for (int i = 0; i < numOutputs; i++) {
      digitalWrite(outputs[i], ledState);
    }
    lastBlinkTime = currentTime;
  }
}

// Check for input pulses (rising edges)
void checkInputs() {
  for (int i = 0; i < numInputs; i++) {
    bool currentState = digitalRead(inputs[i]);
    // Detect rising edge (LOW -> HIGH transition)
    if (currentState && !previousInputStates[i]) {
      Serial.print("pulso aqui - Porta ");
      Serial.println(inputs[i]);
    }
    previousInputStates[i] = currentState;
  }
}

void setup() {
  Serial.begin(115200);
  initializePins();
  lastBlinkTime = millis();
}

void loop() {
  updateOutputs();
  checkInputs();
  delay(10); // Small delay to reduce CPU load
}
Explanation
  • Arduino Functions:
    — Replaced flux.setPinMode with pinMode
    — Replaced flux.writeDigitalPin with digitalWrite
    — Replaced flux.readDigitalPin with digitalRead
    — Replaced flux.delay with delay
    — Replaced flux.millis with millis
  • Non-blocking Timing:
    The code toggles the outputs every 500 ms using the elapsed time from millis(), so the main loop remains free to poll inputs continuously.
  • Edge Detection:
    A rising-edge on an input is detected when the current state is HIGH and the previous state was LOW.
This code should compile and run correctly on your ESP32 using the Arduino IDE or a similar environment. Let me know if you need further adjustments!
Testing and thanks
Request debounce feature
Improve LED timing
Add logging enhancement

Name
Pin #
Type
Part Type
Terminal Order
Alimentação_ESP (+)
Unspecified
Terminal
GND_ESP (-)
Unspecified
Terminal

ESP32 one layer V4 Andon

ESP32 one layer V4 Andon thumbnail