/*
Arduino Forum
Topics: Turn on Peristaltic pump based on PH reading
Sub-Category: Programming Questions
Category: Using Arduino
Link: https://forum.arduino.cc/t/turn-on-peristaltic-pump-based-on-ph-reading/1143658/38
*/
/*
This code sets up and controls an alarm system for pumps based on analog input readings.
It defines the characteristics of each pump, including the pins for pump and alarm control,
the setpoint for triggering the alarm, the type of alarm (Analog Input Low or High), and the delay time for the alarm.
The setup() function initializes the pump and alarm pins as outputs.
In the loop() function, it reads the analog input value, scales it to a percentage, checks for alarm conditions for each pump,
triggers the alarm output, and controls the pump output based on the alarm condition.
The AnalogAlarm function determines whether an alarm condition is met based on the analog input value, the setpoint, and the type of alarm.
Additionally, this code does not include the alarm time, which is required to be implemented using the delayTime together with the alarm flag.
*/
#define analogPin A0 // Define the analog input pin (A0)
enum AlarmType : uint8_t {AIL, AIH}; // Enumeration for AlarmType: AIL (Analog Input Low alarm), AIH (Analog Input High alarm)
typedef struct {
uint8_t pumpPin; // Pin for controlling the pump (output pin connected to the pump)
uint8_t alarmPin; // Pin for triggering the alarm (output pin connected to the alarm system)
float setpoint; // Setpoint for the alarm (threshold value for triggering the alarm)
AlarmType type; // Type of alarm (AIL for Analog Input Low alarm, AIH for Analog Input High alarm)
unsigned long delayTime; // Delay time for the alarm (time in milliseconds for the alarm to remain active)
} PumpState;
PumpState pumps[] = {
{3, 4, 80.0F, AIH, 2000UL}, // Configuration for Pump 1: pumpPin = 3, alarmPin = 4, setpoint = 80.0, type = AIH, delayTime = 2000 ms
{2, 5, 20.0F, AIL, 4000UL} // Configuration for Pump 2: pumpPin = 2, alarmPin = 5, setpoint = 20.0, type = AIL, delayTime = 4000 ms
};
const uint8_t numberOfPumps = sizeof(pumps) / sizeof(PumpState); // Total number of pumps in the array
float analogValue; // Variable to store the scaled analog value from the sensor
void setup() {
// Loop through each pump to initialize pins
for (uint8_t index = 0; index < numberOfPumps; index++) {
// Set the pump pin as an output
pinMode(pumps[index].pumpPin, OUTPUT);
// Set the alarm pin as an output
pinMode(pumps[index].alarmPin, OUTPUT);
}
}
void loop() {
// Read raw input value from analog pin
int rawValue = analogRead(analogPin);
// Scale the analog value to a percentage
analogValue = rawValue * 100.0F / 1023.0F;
// Iterate through each pump to check for alarm conditions and control pump outputs
for (uint8_t index = 0; index < numberOfPumps; index++) {
// Check for alarm condition based on analog value, setpoint, and type
bool alarm = AnalogAlarm(analogValue, pumps[index].setpoint, pumps[index].type);
// Trigger the alarm output
digitalWrite(pumps[index].alarmPin, alarm);
// To be implemented: Determine pump timer on/off based on alarm condition
bool pump = alarm;
// Control the pump output
digitalWrite(pumps[index].pumpPin, pump);
}
}
bool AnalogAlarm(float value, float setpoint, AlarmType type) {
// Check the type of alarm (Analog Input High or Analog Input Low)
switch (type) {
case AIH: // Check for Analog Input High alarm
// Return true if the value is greater than or equal to the setpoint (high alarm condition)
return value >= setpoint;
default: // For Analog Input Low alarm
// Return true if the value is less than or equal to the setpoint (low alarm condition)
return value <= setpoint;
}
}
|
80.0%
HIGH
|
20.0%
LOW
|
0
|
1023
|
0.0%
|
100.0%
P1
P2
A1
A2