dears,
i'm new with programming and arduino,a week i was working on little project.
control module for screw air compressor.
itead Rboard (atmel328) the module with lcd to display information,running status,motor temperature (used ds18b20),pressure status (used 4-20ma pressure transmitter)
so the idea is press boutton start motor (3 phase 380V)in delta star mode,pressed again stop.
during ranning status if pressure reach 9Bars it stop in idle mode until pressure drops to 4 bars it star again.
also during running if temperature reach 90degree it stop and restart if temp drop to 80degres.
also use eeprom library in order to display total running hours during power on (for maintenance).
electrovalve for unload ,to be used after start and litle while before stop.
actually the module works great,but if any advice is welcome,othewise hope that help somebody as i found usefull other projects.
Thanks to all.
/* ADTS
Code By fouad
For Mahle Compressor -MSK-G
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <EEPROM.h>
//Eeprom variable
long RunTime ;
long PRunTime = 0;
//int address = 0;
//RunCouter Variables
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 60000; //the value is a number of milliseconds
// Temp Sensor
#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int SensorTemp;
int cooldown = 0;
int PidMax = 90;//temperature high set ponit
int PidMin = 80;//temperature low setpoint
//Temp Smoothing
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input temperature
int readIndex = 0; // the index of the current reading temperature
int total = 0; // the running total temperature
int average = 0; // the average temperature
//Pressure Smoothing
const int PnumReadings = 10;
int Preadings[PnumReadings]; // the readings from the analog input pressure
int PreadIndex = 0; // the index of the current reading pressure
int Ptotal = 0; // the running total pressure
int Paverage = 0; // the average pressure
//const byte POWER_LED = 2;
//Input Output variable Definitons
int Halted = 0;
const byte BUTTON_1 = 9;//pin 3 on 24l01
const byte RELAY_1 = 4;//Main Contactor
const byte RELAY_2 = 5;//Star Contactor
const byte RELAY_3 = 6;//Delta Contactor
const byte RELAY_4 = 7;//Valve Contactor
//Sensor Configuration
const int P1pin = A0;
//const int P2pin= A1;
int netP = 0;
//int ledpin=7;
int P1analogreading; //read from sensor
const int PidMaxPr = 9;//max pressure set point
const int PidMinPr = 6;//low pressure set point
int PidStb = 0;//variable for standby stature ,0 not in standby
//const int pmax_current = 20 ;
//const int pmin_current = 4;
const int pmax = 2500 ;//sensor max pressure
const int pmin = 0;//sensor minimum pressure
const int sensorhigh = 901;//max adc value when sensor at 40ma,with 220ohm resistor
const int sensorlow = 190;//minimum adc value when sensor at 4ma(0 bar)
int last_button_state = 0;
byte BUTTON_STATE[15];
byte RELAY_STATE[15];
/* number of milliseconds to enable RELAY_2 after start button has been pressed */
const int START_MS = 3000;//delay time for change status delta start
//LCD Code
//Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
//Temp Sensor Initialisation
sensors.begin();
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;//smoother initialisation
}
//LCD Initialisation
lcd.begin();
lcd.backlight();
RunTime = 0;//initialis variable for working hours.
//RunCounter Timer Initialisation
startMillis = millis(); //initial start time
//pressur sensor
lcd.setCursor(6, 0);
EEPROM.get( 0, RunTime );//get working hours stored in eepron
PRunTime = RunTime / 60;//calculate working hours,working hours are stored in minutes
lcd.print(PRunTime);
lcd.setCursor(0, 1);
lcd.print("A.D.T.S");
delay(5000);
lcd.setCursor(0, 1);
lcd.print("Pret ");//ready
//Output define
pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
pinMode(RELAY_3, OUTPUT);
pinMode(RELAY_4, OUTPUT);
pinMode(BUTTON_1, INPUT);
//Set All State to 0
for (int n = 0; n <= sizeof(BUTTON_STATE); n++) {
BUTTON_STATE[n] = 0;
}
for (int n = 0; n <= sizeof(RELAY_STATE); n++) {
RELAY_STATE[n] = 0;
}
UpdateRelay();
last_button_state = 1;
}
void SetRelayStop()
{
RELAY_STATE[RELAY_4] = 0;
UpdateRelay();
delay(500);//pressure valve opened before stop motor
RELAY_STATE[RELAY_1] = 0;
RELAY_STATE[RELAY_2] = 0;
RELAY_STATE[RELAY_3] = 0;
lcd.setCursor(0, 1);
lcd.print("Arret ");//stop
UpdateRelay();
delay (1000);//Delay for Stop motor from 2000 to 0 rpm.
}
void UpdateRelay()
{
digitalWrite(RELAY_1, RELAY_STATE[RELAY_1]);
digitalWrite(RELAY_2, RELAY_STATE[RELAY_2]);
digitalWrite(RELAY_3, RELAY_STATE[RELAY_3]);
digitalWrite(RELAY_4, RELAY_STATE[RELAY_4]);
}
void SetRelayStarting()
{
RELAY_STATE[RELAY_1] = 1;
RELAY_STATE[RELAY_2] = 0;
RELAY_STATE[RELAY_3] = 1;
lcd.setCursor(0, 1);
lcd.print("Demarrage ");//starting
UpdateRelay();
}
void SetRelayRunning()
{
RELAY_STATE[RELAY_3] = 0;
RELAY_STATE[RELAY_1] = 0;//used this metode because no protection on contators,so little while that can be adjuted to prevent short circuit betwen delta contactor and star contactor.
UpdateRelay();
delay (500);
RELAY_STATE[RELAY_1] = 1;
RELAY_STATE[RELAY_2] = 1;
UpdateRelay();
lcd.setCursor(0, 1);
lcd.print("Marche ");//running
RELAY_STATE[RELAY_4] = 1;
UpdateRelay();
}
void Monitor () {
//using smoothing value for both pressure and temperature
total = total - readings[readIndex];
// read from the sensor:
SensorTemp = sensors.getTempCByIndex(0);
readings[readIndex] = SensorTemp;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
sensors.requestTemperatures();
//Pressure Read
Ptotal = Ptotal - Preadings[PreadIndex];
P1analogreading = analogRead (P1pin);
Preadings[PreadIndex] = P1analogreading;
Ptotal = Ptotal + Preadings[PreadIndex];
PreadIndex = PreadIndex + 1;
if (PreadIndex >= PnumReadings) {
PreadIndex = 0;
}
Paverage = Ptotal / PnumReadings;
Paverage = map(P1analogreading, sensorhigh, sensorlow, pmax, pmin);
Paverage = Paverage /100;//to display xx pressure in bar
//Pressure Readin
lcd.print (" ");
lcd.setCursor (0, 0);
lcd.print ("PR:");//pressure display
lcd.print (Paverage);
lcd.print (" TP:");//temperature disply
lcd.print (average);
}
void TempPid()
{
if ((average >= PidMax) && (average != -127)) {
SetRelayStop();
cooldown = 1;
while (cooldown == 1) {
lcd.setCursor(0, 1);
lcd.print ("Temp Haute ");//high temperature
lcd.setCursor (0, 0);
Monitor ();
if (average <= PidMin) {
cooldown = 0;
Halted = 1;
}
}
}
if (average == -127) {
SetRelayStop();
lcd.setCursor(0, 1);
lcd.print("ERREUR TEMP 01 ");//error temp sensor
}
}
void PressurePid ()
{
Monitor ();
if (Paverage >= PidMaxPr) {
SetRelayStop();
PidStb = 1;
}
while (PidStb == 1){
Monitor ();
if (Paverage < PidMinPr) {
PidStb = 0;
Halted = 1;
}
}
if ( Paverage < 0 ){
SetRelayStop();
lcd.setCursor(0, 1);
lcd.print("ERREUR Pr 01 ");//pressure sensor error
}
}
//initializ variable for button and running state
int last_state_change = 0;
int STATE = 0;
int last_bounce = 0;
