1/2
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <PID_v1.h>
#include <TimerOne.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Sets the LCD I2C address
Servo servo1; // Engagement servo declaration
Servo motor1; // Starter motor declaration
// PIN affectation
const int start = 14; //Start button
//const int Emerg = 18; //Emergency stop button
const int PPump = 45; //Preheat pump PWM motor driver
const int servo = 42; //PWM output for starter motor engagement servo
const int motor = 43; //PWM output for starter motor speed (controls the ESC)
const int modeventil = 3; //Mode Ventilation switch (HIGH=ON) 15K PULLDOWN INSTALLED
const int modestart = 4; //Mode Start switch (HIGH=ON) 15K PULLDOWN INSTALLED
const int spark = 34; //Spark module relay (LOW=ON)
const int drain = 35; // Drain Valves relay (LOW=ON)
const int led = 19; //status led
const int LED1=22; //LED1 = STATUS (blinking)
const int LED2=23; //LED2 = Spark
const int LED3=24; //LED3 = Servo + Starter Motor
const int LED4=25; //LED4 = PreHeatPump (PPump)
const int LEDstart=20; //LED bouton start blinking
double Setpoint, Input, Output;
const long t1 = 0; //phase 1 duration in ms
const long t2 = 5000; //phase 2 duration in ms
const long t3 = 6000; //phase 3 duration in ms
const long t4 = 25000; //phase 4 duration in ms
const long t5 = 30000; //phase 5 duration in ms
const long tv = 40; //ventilation in s
const int v1=5; //STAND-BY Starter motor Speed (0RPM)
const int v2=50; //MID starter motor speed (3000RPM)
const int v3=90; //MAX starter motor speed (~18000RPM)
int rampstepup=100; //timestep between two incrementations of Starter motor speed
int rampstepdown=100; //timestep between two decrementations of PreheatPumpMotor speed
int vtemp=0; //speed variable to increment the starter motor speed with a ramp
int rampx=0; //time variable to increment the starter motor speed with a ramp
unsigned long sequencestart = 0; //Time variables
unsigned long previousMillis=0;
unsigned long currentMillis=0;
unsigned long test3s=0;
int blinkperiod = 200; //LED blink period (200ms=5Hz)
boolean sequence_auth=false; //start authorization flag
boolean sequence_running=false; //sequence running flag
boolean modeswpos=HIGH; //mode switch reading : Ventilation=HIGH ; Start=LOW
int state=0; //State indicator (will be used to display which phase it is in on the LCD
int pin=0;
int read_state = 1; //for RS232 Serial communication with FADEC
int readindex = 0; //for RS232 Serial communication with FADEC
byte trame[48]; //for RS232 Serial communication with FADEC
int i; //for RS232 Serial communication with FADEC
int egt=0; //Exhaust gas temperature reading from FADEC
long int RPMN2=0;
unsigned long degt; //time value to "debounce" egt overheat
PID Preheat(&Input, &Output, &Setpoint,2,5,1, DIRECT);
void setup() {
digitalWrite(spark,HIGH); //Switching off Spark relay
digitalWrite(drain,HIGH); //Switching off drain relay
lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines, on backlight
servo1.attach(servo);
motor1.attach(motor);
Serial.begin(19200); //Initializing serial communication with PC (via USB)
Serial2.begin(2400); //Initializing the serial communication with the FADEC
Input=egt;
Setpoint=250;
Preheat.SetMode(AUTOMATIC);
SetOutputLimits(64, 255) //setting limits to PID outputs
pinMode(motor, OUTPUT);
pinMode(start, INPUT);
// pinMode(Emerg, INPUT);
pinMode(PPump, OUTPUT);
pinMode(servo, OUTPUT);
pinMode(modeventil, INPUT);
pinMode(modestart, INPUT);
pinMode(spark, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
digitalWrite(LED1,LOW); //Lighting the LEDS 1s @ startup (test)
digitalWrite(LED2,LOW); //Lighting the LEDS 1s @ startup (test)
digitalWrite(LED3,LOW); //Lighting the LEDS 1s @ startup (test)
digitalWrite(LED4,LOW); //Lighting the LEDS 1s @ startup (test)
delay(1000);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
motor1.write(v1); //Starter Motor ESC STAND-BY
rampstepup=((t4-t3-1000)/(v3-v2)); //calculating the time step for each +1 speed step #STARTER MOTOR RAMP
//SPLASH SCREEN
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("xxx Technologies");
lcd.setCursor(0,3);
lcd.print("Test Test");
delay(1000);
lcd.clear();
lcd.setCursor(0,1);
lcd.print(" READY? ");
delay(1000);
lcd.setCursor(0,3);
lcd.print(" GO! ");
lcd.clear();
}
void loop()
{
currentMillis=millis();
read_fadec ();
pollswitch ();
safety3s ();
launchpad ();
tmonitoring();
}
//Polling the switch so that you can't change the sequence while running
void pollswitch ()
{
if (sequence_auth==false && sequence_running==false /* && state!=10 && state!=11 && state!=2 */)
{
if(digitalRead(modestart)==HIGH)
{
modeswpos=HIGH;
state=1;
}
else
{
if(digitalRead(modeventil)==HIGH)
{
modeswpos=LOW;
}
}
}
}
//Temperature monitoring to stop everything in case of an overtemperature
void tmonitoring ()
{
if ((egt > 850) && degt==0)
{
degt = currentMillis;
}
if (degt!=0 && millis()-degt>500)
{
if(egt>850){
overheatstop();
}
else if(egt<650){
degt=0;
}
}
}
//Requires to press 3seconds the start button (and keep it pressed) before any action is taken.
void safety3s ()
{
if (sequence_auth==false && sequence_running==false && digitalRead(start)==HIGH)
{
state=2;
digitalWrite(LED1,LOW); //Lighting LED1 while waiting the 3seconds
ledState=LOW;
if ((currentMillis-test3s)>3000 && digitalRead(start)==HIGH)
{
sequence_auth=true;
if(sequence_running==false);
{
sequencestart=currentMillis;
}
}
}
else{
test3s=currentMillis; //if test3s = currentMillis, it can never reach the 3seconds...
if(digitalRead(start)==HIGH) //Blinking LED1 during Start button pressed and sequence running
{
if(currentMillis - previousMillis > blinkperiod) {
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(LED1, ledState);
}
}
}
if(digitalRead(start)==LOW && state!=10 && state!=11)
{
sequence_auth=false;
digitalWrite(LED1,HIGH);
motor1.write(0);
servo1.write(0);
analogWrite(PPump,0);
digitalWrite(spark,HIGH);
digitalWrite(drain,HIGH);
state=1;
}
}
//SEQUENCE CHOICE AND LAUNCH
void launchpad()
{
switch (modeswpos)
{
case LOW:
startsequence();
break;
case HIGH:
ventilation();
break;
}
}