// ---------------------------------------------------------------------------
// (| " stm32-blue-pill.ino "|)
// (| " STM32 Blue Pill project using the STM32 Arduino Core (stm32duino) "|)
// (| " Sketch que enciende un LED durante 250 mseg, luego lo apaga "|)
// (| " durante 250 mili segundos, indefinidamente. "|)
// (| " "|)
// (| " Este código de ejemplo es de dominio público. "|)
// (| " "|)
// Simulación de conteo de estuches en Wokwi con Blue Pill (STM32)
// Lee un potenciómetro en A0, calcula el voltaje y decide cuántos estuches hay.
const int PIN_SENSOR = A0; // Conecta aquí el potenciómetro
const float VREF = 3.3f; // La Blue Pill trabaja a 3.3V
// Rangos con histéresis (los mismos que usábamos en HAL)
const float estuche_up[10] = {0.210f,0.419f,0.628f,0.838f,1.048f,1.257f,1.466f,1.676f,1.885f,2.095f};
const float estuche_down[10] = {0.162f,0.372f,0.559f,0.745f,0.931f,1.118f,1.304f,1.490f,1.676f,1.862f};
int8_t current_estuches = 0;
int8_t last_estuches = -1;
unsigned long last_tx_ms = 0;
const unsigned long TX_PERIOD_MS = 200;
// OJO: depende del core STM32, puede ser 4095 o 1023.
// Prueba primero con 4095; si el valor nunca pasa de ~1000, cambia a 1023.0f.
const float ADC_MAX = 1023.0f;
int8_t voltageToEstuchesHysteresis(float v) {
if (current_estuches < 10 && v >= estuche_up[current_estuches]) {
current_estuches++;
} else if (current_estuches > 0 && v <= estuche_down[current_estuches - 1]) {
current_estuches--;
}
return current_estuches;
}
void setup() {
Serial.begin(115200);
pinMode(PIN_SENSOR, INPUT_ANALOG); // core STM32duino suele permitir esto
delay(500);
Serial.println("Simulacion estuches STM32 / Wokwi");
}
void loop() {
int raw = analogRead(PIN_SENSOR);
float v = (raw * VREF) / ADC_MAX;
int8_t estuches = voltageToEstuchesHysteresis(v);
unsigned long now = millis();
bool change = (estuches != last_estuches);
bool timeup = (now - last_tx_ms) >= TX_PERIOD_MS;
if (change || timeup) {
Serial.print("ADC=");
Serial.print(raw);
Serial.print(" V=");
Serial.print(v, 3);
Serial.print(" V Estuches=");
Serial.println(estuches);
last_tx_ms = now;
last_estuches = estuches;
}
delay(10);
}