#define STEP_PIN PA0
#define DIR_PIN PA1
#define PRESSURE_PIN PA2
#define ESTOP_PIN PB12
const int PRESSURE_LIMIT = 3200;
// 200 импульсов = 1 мл
const int STEPS_PER_ML = 200;
const int TARGET_VOLUME_ML = 2;
const int TARGET_STEPS = STEPS_PER_ML * TARGET_VOLUME_ML;
// Один шаг каждые 50 000 микросекунд
const unsigned long STEP_INTERVAL_US = 50000;
const unsigned long PULSE_WIDTH_US = 5;
unsigned long lastStepTime = 0;
unsigned long pulseStartTime = 0;
unsigned long lastReportTime = 0;
int pressureValue = 0;
int totalSteps = 0;
bool stepIsHigh = false;
bool emergencyStop = false;
bool occlusion = false;
bool volumeComplete = false;
void stopMotor() {
digitalWrite(STEP_PIN, LOW);
stepIsHigh = false;
}
void setup() {
Serial.begin(115200);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(ESTOP_PIN, INPUT_PULLUP);
digitalWrite(STEP_PIN, LOW);
digitalWrite(DIR_PIN, HIGH);
analogReadResolution(12);
Serial.println("INFUSION PUMP STARTED");
Serial.println("Pressure limit: 3200");
Serial.println("Target volume: 2 ml");
Serial.println("Press E STOP for emergency stop");
}
void loop() {
unsigned long now = micros();
// 1. Аварийная остановка имеет наивысший приоритет
if (digitalRead(ESTOP_PIN) == LOW) {
emergencyStop = true;
}
if (emergencyStop) {
stopMotor();
if (millis() - lastReportTime >= 500) {
lastReportTime = millis();
Serial.println("EMERGENCY STOP! MOTOR STOPPED");
}
return;
}
// 2. Считывание давления
pressureValue = analogRead(PRESSURE_PIN);
// 3. Проверка закупорки
occlusion = (pressureValue >= PRESSURE_LIMIT);
if (occlusion) {
stopMotor();
if (millis() - lastReportTime >= 500) {
lastReportTime = millis();
Serial.print("OCCLUSION! Pressure: ");
Serial.println(pressureValue);
}
return;
}
// 4. Проверка целевого объёма
if (totalSteps >= TARGET_STEPS) {
volumeComplete = true;
stopMotor();
if (millis() - lastReportTime >= 1000) {
lastReportTime = millis();
Serial.println("INFUSION COMPLETE: 2 ml");
}
return;
}
volumeComplete = false;
// 5. Формирование STEP-импульсов
if (stepIsHigh && (now - pulseStartTime >= PULSE_WIDTH_US)) {
digitalWrite(STEP_PIN, LOW);
stepIsHigh = false;
}
if (!stepIsHigh &&
(now - lastStepTime >= STEP_INTERVAL_US)) {
digitalWrite(STEP_PIN, HIGH);
stepIsHigh = true;
pulseStartTime = now;
lastStepTime = now;
totalSteps++;
}
// 6. Вывод данных в Serial Monitor
if (millis() - lastReportTime >= 1000) {
lastReportTime = millis();
Serial.print("Pressure: ");
Serial.print(pressureValue);
Serial.print(" | Steps: ");
Serial.print(totalSteps);
Serial.print(" | Volume: ");
Serial.print((float)totalSteps / STEPS_PER_ML, 2);
Serial.println(" ml");
}
}
Loading
stm32-bluepill
stm32-bluepill