0% found this document useful (0 votes)
4 views

The First Pressure Based Switching Controlled Pump Plc

The document outlines the code for a pressure-based switching controlled pump PLC using an Arduino. It includes setup for various components such as pumps, LEDs, and an LCD display, along with logic for handling pressure thresholds and pump operation. The code manages user interactions through mode switches and push-to-read buttons to display pressure readings and set thresholds.

Uploaded by

conceptronix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

The First Pressure Based Switching Controlled Pump Plc

The document outlines the code for a pressure-based switching controlled pump PLC using an Arduino. It includes setup for various components such as pumps, LEDs, and an LCD display, along with logic for handling pressure thresholds and pump operation. The code manages user interactions through mode switches and push-to-read buttons to display pressure readings and set thresholds.

Uploaded by

conceptronix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

The First pressure based switching controlled pump plc

#include <LiquidCrystal.h>

// Initialize the LCD with the correct pins


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int inputPin = 6; // Original input pin for controlling the pumps
const int pump1Pin = 8; // Pump 1 (to be inverted)
const int pump2Pin = 9; // Pump 2 (to be inverted)
const int pump3Pin = 10; // Pump 3 (to be inverted)
const int controlLedPin = 7; // Control LED pin (shows pump activity)
const int pushToReadPin = 13; // Push-to-read switch pin
const int modeSwitchPin = A2; // Mode switch pin (to toggle to threshold setting
screen)
const int lowPressureLedPin = A5;// LED to indicate low pressure (stays HIGH when
pressure is low)
const int vfdLedPin = A1; // VFD LED pin (activates when pumps are running)

// Potentiometers for pressure thresholds


const int highThresholdPin = A3; // Potentiometer for high threshold
const int lowThresholdPin = A4; // Potentiometer for low threshold

const unsigned long delayDuration = 10000; // 10 seconds


const unsigned int closeDelay = 3500; // 3.5 seconds delay during shutdown
const unsigned long debounceDelay = 200; // 200ms debounce for mode switch

unsigned long startTime;


bool pump1On = false;
bool pump2On = false;
bool pump3On = false;

unsigned long controlLedTimer = 0; // Timer for Control LED


bool controlLedActive = false; // Tracks if Control LED is active

unsigned long vfdLedTimer = 0; // Timer for VFD LED


bool vfdLedActive = false; // Tracks if VFD LED is active

unsigned long lastLcdUpdate = 0; // Last time LCD was updated


const unsigned long lcdUpdateInterval = 200;// Update LCD every 200 milliseconds
int cycleCount = 0; // Variable to track the current cycle

unsigned long lastDebounceTime = 0; // Last time the input was changed


int lastModeSwitchState = HIGH; // Previous state of the mode switch
bool modeSwitchPressed = false; // Track whether mode switch is pressed
bool isInThresholdMode = false; // Is the system in threshold-setting
mode?

float highThreshold = 3.0; // High pressure threshold (in BAR), initialized to


mid-range
float lowThreshold = 1.0; // Low pressure threshold (in BAR), initialized to mid-
range
bool lowPressureLedOn = false; // Status of low pressure LED
bool displayPressureScreen = false; // Flag for push-to-read screen

void setup() {
// Setup original hardware
pinMode(inputPin, INPUT);
pinMode(pump1Pin, OUTPUT);
pinMode(pump2Pin, OUTPUT);
pinMode(pump3Pin, OUTPUT);
pinMode(controlLedPin, OUTPUT); // Control LED pin
pinMode(pushToReadPin, INPUT_PULLUP); // Push-to-read button
pinMode(modeSwitchPin, INPUT_PULLUP); // Mode switch for threshold screen
pinMode(lowPressureLedPin, OUTPUT); // Low pressure LED (A5)
pinMode(vfdLedPin, OUTPUT); // VFD LED (A0)

lcd.begin(20, 4); // Use 4x20 LCD

// Define the custom clock icon (8x8 pixels)


byte clockIcon[8] = {
B11111, B01110, B01110, B00100,
B01110, B01110, B11111, B11111
};
lcd.createChar(0, clockIcon); // Store the custom character in CGRAM

// Define a new custom icon (8x8 pixels)


byte newIcon[8] = {
B11111, B11111, B01110, B00100,
B01110, B11111, B11111, B01110
};
lcd.createChar(1, newIcon); // Store the new custom character in CGRAM

// Start with all pumps and LEDs off (inverted logic for pumps)
digitalWrite(pump1Pin, LOW); // Pump 1 OFF (inverted logic)
digitalWrite(pump2Pin, LOW); // Pump 2 OFF (inverted logic)
digitalWrite(pump3Pin, LOW); // Pump 3 OFF (inverted logic)
digitalWrite(controlLedPin, LOW); // Control LED OFF (not inverted)
digitalWrite(lowPressureLedPin, LOW); // Low pressure LED OFF initially
digitalWrite(vfdLedPin, LOW); // VFD LED OFF initially

Serial.begin(9600); // Start Serial Monitor for debugging


}

void loop() {
unsigned long currentTime = millis();

// Handle pressure logic and A5 LED


handlePressureLogic();

// Handle mode switch and screen logic


handleScreenLogic();

// Handle Control LED logic (Pin 7)


handleControlLedLogic(currentTime);

// Handle VFD LED logic (Pin A0)


handleVfdLedLogic(currentTime);

// Original pump logic (unchanged)


handleOriginalPumpLogic();

// Update the LCD at regular intervals


if (currentTime - lastLcdUpdate >= lcdUpdateInterval) {
lastLcdUpdate = currentTime;
updateLCD(); // Ensure the LCD is updated regularly
}
}

// Function to handle pressure threshold and LED logic on A5


void handlePressureLogic() {
// Monitor current pressure and control A5 LED
float currentPressure = (analogRead(A0) * (5.0 / 1023.0)) * 6.0; // Pressure in
BAR

// Control Low Pressure LED (A5) based on thresholds


if (currentPressure < lowThreshold && !lowPressureLedOn) {
digitalWrite(lowPressureLedPin, HIGH); // Turn on the low-pressure LED
lowPressureLedOn = true;
} else if (currentPressure > highThreshold && lowPressureLedOn) {
digitalWrite(lowPressureLedPin, LOW); // Turn off the low-pressure LED
lowPressureLedOn = false;
}
}

// Function to handle Control LED (Pin 7) logic


void handleControlLedLogic(unsigned long currentTime) {
int inputState = digitalRead(inputPin);

// Turn Control LED ON 3 seconds before pin 6 goes HIGH


if (inputState == HIGH && !controlLedActive) {
controlLedTimer = currentTime + 3000; // Set timer to turn on 3 seconds later
controlLedActive = true;
}

// Turn Control LED OFF 3 seconds after pin 6 goes LOW


if (inputState == LOW && controlLedActive) {
if (currentTime - controlLedTimer >= 3000) {
digitalWrite(controlLedPin, LOW); // Turn off Control LED
controlLedActive = false;
}
}

// Turn Control LED ON if the timer has passed and input is HIGH
if (controlLedActive && currentTime >= controlLedTimer && inputState == HIGH) {
digitalWrite(controlLedPin, HIGH); // Turn on Control LED
}
}

// Function to handle VFD LED (Pin A0) logic


void handleVfdLedLogic(unsigned long currentTime) {
// Check if any pump is on
if ((pump1On || pump2On || pump3On) && !vfdLedActive) {
vfdLedTimer = currentTime - 5000; // Set timer to turn on 5 seconds earlier
vfdLedActive = true;
digitalWrite(vfdLedPin, HIGH); // Turn on VFD LED
}

// Turn off VFD LED 5 seconds after all pumps stop


if (!pump1On && !pump2On && !pump3On && vfdLedActive) {
if (currentTime - vfdLedTimer >= 5000) {
digitalWrite(vfdLedPin, LOW); // Turn off VFD LED
vfdLedActive = false;
}
}
}
// Function to handle the screen logic (mode switch and push-to-read switch)
void handleScreenLogic() {
int pushToReadState = digitalRead(pushToReadPin); // Read the state of the push-
to-read switch
int modeSwitchState = digitalRead(modeSwitchPin); // Read the state of the mode
switch

unsigned long currentTime = millis();

// Mode switch logic to enter threshold setting mode


if (modeSwitchState != lastModeSwitchState) {
lastDebounceTime = currentTime; // Reset debounce timer
}

if ((currentTime - lastDebounceTime) > debounceDelay && modeSwitchState == LOW &&


!modeSwitchPressed) {
modeSwitchPressed = true; // Switch pressed
isInThresholdMode = !isInThresholdMode; // Toggle threshold setting mode
delay(300); // Short delay to avoid rapid toggling
} else if (modeSwitchState == HIGH) {
modeSwitchPressed = false; // Reset flag when switch is released
}

lastModeSwitchState = modeSwitchState;

// Logic for push-to-read switch


if (pushToReadState == LOW) {
displayPressureScreen = true; // Display the pressure readout screen when
switch is pressed
} else {
displayPressureScreen = false; // Return to default screen when released
}

// If in threshold setting mode, read the potentiometers for thresholds


if (isInThresholdMode) {
highThreshold = (analogRead(highThresholdPin) * (6.0 / 1023.0)); // Convert to
BAR
lowThreshold = (analogRead(lowThresholdPin) * (6.0 / 1023.0)); // Convert to
BAR
}
}

// Function to handle the original pump logic (unchanged)


void handleOriginalPumpLogic() {
int currentInputState = digitalRead(inputPin);
unsigned long currentTime = millis();

// Original input-based pump control logic (unchanged from original code)


if (currentInputState == HIGH && !pump1On && !pump2On && !pump3On) {
startTime = currentTime;
pump1On = true;
Serial.println("Pump sequence started.");
}

// Pump sequence (cycled based on timing and input state)


int pumpOrder[3];
switch (cycleCount % 3) {
case 0:
pumpOrder[0] = pump1Pin;
pumpOrder[1] = pump2Pin;
pumpOrder[2] = pump3Pin;
break;
case 1:
pumpOrder[0] = pump2Pin;
pumpOrder[1] = pump3Pin;
pumpOrder[2] = pump1Pin;
break;
case 2:
pumpOrder[0] = pump3Pin;
pumpOrder[1] = pump1Pin;
pumpOrder[2] = pump2Pin;
break;
}

if (pump1On && (currentTime - startTime >= delayDuration)) {


digitalWrite(pumpOrder[0], HIGH); // Turn ON the first pump in the order
(inverted logic)
Serial.println("First Pump ON");
}

if (pump1On && !pump2On && (currentTime - startTime >= delayDuration * 2)) {


digitalWrite(pumpOrder[1], HIGH); // Turn ON the second pump in the order
(inverted logic)
pump2On = true;
Serial.println("Second Pump ON");
}

if (pump2On && !pump3On && (currentTime - startTime >= delayDuration * 3)) {


digitalWrite(pumpOrder[2], HIGH); // Turn ON the third pump in the order
(inverted logic)
pump3On = true;
Serial.println("Third Pump ON");
}

// Shutdown logic when the input goes LOW


if (currentInputState == LOW && (pump1On || pump2On || pump3On)) {
if (pump1On) {
digitalWrite(pumpOrder[0], LOW); // Turn OFF the first pump in the order
pump1On = false;
Serial.println("First Pump OFF");
delay(closeDelay);
}

if (pump2On) {
digitalWrite(pumpOrder[1], LOW); // Turn OFF the second pump in the order
pump2On = false;
Serial.println("Second Pump OFF");
delay(closeDelay);
}

if (pump3On) {
digitalWrite(pumpOrder[2], LOW); // Turn OFF the third pump in the order
pump3On = false;
Serial.println("Third Pump OFF");
delay(closeDelay);
}
cycleCount = (cycleCount + 1) % 3; // Increment cycleCount for the next cycle
startTime = currentTime;
}
}

// Function to update the LCD display based on mode and screen toggling
void updateLCD() {
lcd.clear(); // Clear the screen

if (isInThresholdMode) {
// Screen to set pressure thresholds
lcd.setCursor(0, 0);
lcd.print("Set Pressure Limits");

lcd.setCursor(0, 1);
lcd.print("High Threshold: ");
lcd.print(highThreshold, 2); // Display high threshold with 2 decimal points

lcd.setCursor(0, 2);
lcd.print("Low Threshold: ");
lcd.print(lowThreshold, 2); // Display low threshold with 2 decimal points

lcd.setCursor(0, 3);
lcd.print("Press Mode to Exit");
} else if (displayPressureScreen) {
// Display the pressure and pump statuses when the push-to-read switch is
pressed
lcd.setCursor(0, 0);
lcd.print("PRESSURE: ");
lcd.print((analogRead(A0) * (5.0 / 1023.0)) * 6.0, 2); // Convert to BAR

lcd.setCursor(0, 1);
lcd.print("PUMP1=");
lcd.print(digitalRead(pump1Pin) == HIGH ? "ON " : "OFF");

lcd.setCursor(10, 1);
lcd.print("PUMP2=");
lcd.print(digitalRead(pump2Pin) == HIGH ? "ON " : "OFF");

lcd.setCursor(0, 2);
lcd.print("PUMP3=");
lcd.print(digitalRead(pump3Pin) == HIGH ? "ON " : "OFF");

lcd.setCursor(10, 3);
lcd.write(byte(1)); // Display the custom icon
lcd.setCursor(11, 3);
lcd.print("RIK & SUBHRO");
} else {
// Default screen (COSMOS screen)
lcd.setCursor(0, 0);
lcd.print("*******COSMOS*******");

lcd.setCursor(0, 1);
lcd.print("PUMP PLC T5 ROSEDALE");

lcd.setCursor(0, 2);
lcd.print("PUMP1=");
lcd.print(digitalRead(pump1Pin) == HIGH ? "ON " : "OFF");
lcd.setCursor(10, 2);
lcd.print("PUMP2=");
lcd.print(digitalRead(pump2Pin) == HIGH ? "ON " : "OFF");

lcd.setCursor(0, 3);
lcd.print("PUMP3=");
lcd.print(digitalRead(pump3Pin) == HIGH ? "ON " : "OFF");

lcd.setCursor(10, 3);
lcd.write(byte(0)); // Display the custom clock icon
lcd.setCursor(11, 3);

unsigned long elapsedSeconds = (millis() - startTime) / 1000;


unsigned int hours = elapsedSeconds / 3600;
unsigned int minutes = (elapsedSeconds % 3600) / 60;
unsigned int seconds = elapsedSeconds % 60;
char timeBuffer[9];
snprintf(timeBuffer, sizeof(timeBuffer), "%02u:%02u:%02u", hours, minutes,
seconds);
lcd.print(timeBuffer);
}
}

You might also like