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
LED2
LED14
LED3
R17
Resistance
220 Ω
Q8
LED9
R7
Resistance
220 Ω
R1
Resistance
330 Ω
LED1
R9
Resistance
1000 Ω
Q3
LED13
Q2
R5
Resistance
3900 Ω
R2
Resistance
330 Ω
R3
Resistance
3900 Ω
R4
Resistance
1000 Ω
Q1
R6
Resistance
1000 Ω
R16
Resistance
220 Ω
LED4
C1
Capacitance
100nF
U2
Alimentação_ESP (+)
(5 V)
D3
D6
GND_ESP (-)
D7
Vin_ESP (24v)
IN (D32)
R21
Resistance
10 kΩ
F1
U1
L3
C4
Capacitance
220uF
C2
Capacitance
220uF
L2
Not Recommended for New Designs

ESP32 one layer V4 Andon