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

Schematic of ESP32 one layer V4 Andon
Alimentação_ESP (+)
GND_ESP (-)
C1
Capacitance
100nF
LED2
LED14
LED3
LED6
LED9
LED12
LED1
LED13
LED10
LED7
LED5
LED4
LED11
Q8
Q3
Q2
Q1
R17
Resistance
220 Ω
R7
Resistance
220 Ω
R1
Resistance
330 Ω
R9
Resistance
1000 Ω
R5
Resistance
3900 Ω
R2
Resistance
330 Ω
R3
Resistance
3900 Ω
R4
Resistance
1000 Ω
R6
Resistance
1000 Ω
R16
Resistance
220 Ω
C4
Capacitance
220uF
C2
Capacitance
220uF
L3
R21
Resistance
10 kΩ
U2
(5 V)
Vin_ESP (24v)
IN (D32)
F1
L2
Not Recommended for New Designs
D3
D6
D7
U1

ESP32 one layer V4 Andon