I am using the Arduino Leonardo Ethernet board and my project is to create a fan speed controller using proportional integral control. The controller would be measuring the system pressure (bar) and ramping up/down the condenser fan in order to maintain the system at a constant pressure.
I have an LCD display which shows the system pressure and the controller speaks via Ethernet which also enables me to view the system pressure on the HMI.
I have got as far as the proportional integral control and now have some questions regarding feasibility and programming:
-
Is the function of this project fully feasible? i.e: can you use an arduino for proportional integral control?
-
I am used to writing PLC software using ladder so this is new to me. How do I set up for example a 1 second clock and set parts of the program so they are being updated in different time frames.
-
If it is feasible - I have drawn out the software as a flowchat on paper. For example: in the dead band I need to do (Pressure in - set point) —> (Is the result less than 0?) —> This then has two possible outcomes, yes or no which then branch off in to many more yes or no scenarios. How would you write this using good practice?
I will attach the program I have written thus far and would really appreciate any help with my questions and any feedback on what is already written.
Dan.
#include <LiquidCrystal.h>
#include <SPI.h>
#include <Ethernet2.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); //Setting pins that control LCD screen.
const float PressureInput = A0; //Setting Pressure sensor input
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x10, 0x89, 0x6D
};
IPAddress ip(192, 168, 0, 248);
IPAddress subnet(255, 255, 255, 0);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip, subnet);
server.begin();
//-------------------------------------------------
//Setting up serial connection and LCD display
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Pressure in the");
lcd.setCursor(0, 1);
lcd.print("system >0.1 bar");
//Setting up digital pins
pinMode(8, OUTPUT);
}
//-------------------------------------------------
void loop() {
EthernetClient client = server.available();
if (client) {
//Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 1"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
//-------------------------------------------------
//Reading and converting analog input to bar. Also printing to PC screen to access all values
float sensorValue = analogRead(PressureInput); //Reading the raw input
Serial.print (" Sensor Value ");
Serial.print (sensorValue);
float voltage = (sensorValue/1023.0) * 5.0; // Converting raw value in to a voltage reading
Serial.print (" Volts ");
Serial.print (voltage);
Serial.print (" BAR: ");
float bar = (voltage *8);
Serial.println(bar);
//-------------------------------------------------
//Writing the pressure value to LCD display
if (bar>0.1){//Value that determines at what stage the LCD display wont show pressure reading
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Pressure (bar) ");
lcd.setCursor(5, 5);
lcd.print(bar);
}
else if (bar<0.1){//Value that determines at what stage the LCD display wont show pressure reading
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pressure in the");
lcd.setCursor(0, 1);
lcd.print("system <0.1 bar");
}
//------------------------------------------------
//High and low pressure alarms set for LED(pin 7) and sounder(pin 8). (High pressure set at 28 bar, Low pressure set at 2 bar).
if (bar>=28 or bar<=2){
digitalWrite(8, HIGH);
}
else if (bar<27.99 or bar>2.01){
digitalWrite(8, LOW);
}
//-----------------------------------------------
delay(50);
if ("HTTP/1.1 200 OK"); {
client.print(" Arduino Status: ");
client.println("
");
client.println("</html>");
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 1; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("System pressure is ");
client.print(bar);
client.print(" bar " );
client.println("
");
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
//Serial.println("client disconnected");
}
}