#define opStop 7
#define mode 6
#define SPG 5
#define gear 4
#define OTMR 3
#define opRestart 2
#define brakePressure A0
#define batteryVoltage A1
#define engineRPM A2
#define enginePin 1 //just to satisfy code, only applies when attached to real machine
unsigned long now = 0;
unsigned long stopTime = 0;
unsigned long startTime = 0;
unsigned long restartRecommendedTime = 0;
unsigned long restartTime = 0;
unsigned long opRestartCheckTime = 0;
unsigned long opRestartCheckDelay = 2000;
unsigned long opStopTime = 0;
int counter = 0;
long initialTimerPeriod = 10000;
long timerPeriod;
bool engineRunning = true;
int bppLimit = 500;
int bpLimit = 500;
int bvLimit = 500;
int lastState = HIGH;
int minIdleRPM = 500;
int maxIdleRPM = 700;
void setup() {
pinMode(engineRunning, INPUT_PULLUP);
pinMode(opStop, INPUT_PULLUP);
pinMode(mode, INPUT_PULLUP);
pinMode(SPG, INPUT_PULLUP);
pinMode(gear, INPUT_PULLUP);
pinMode(OTMR, INPUT_PULLUP);
pinMode(opRestart, INPUT_PULLUP);
pinMode(brakePressure, INPUT);
pinMode(batteryVoltage, INPUT);
pinMode(engineRPM, INPUT);
Serial.begin(9600);
}
bool speedCheck(){
if (digitalRead(SPG) == digitalRead(OTMR)){
return true;
} else {
return false;
}
}
void engineStop(){
engineRunning = false;
}
void engineStopMessage(){
Serial.println("Engine ready for shutdown");
}
bool engineRPMCorrectCheck(){
if (analogRead(engineRPM) > minIdleRPM && analogRead(engineRPM) < maxIdleRPM){ //idle range
return true;
} else {
return false;
}
}
void engineRestartMessage(){
Serial.println("Recommend Restart");
}
bool restartButtonCheck() {
int value = digitalRead(opRestart);
if (lastState != value) {
lastState = value;
if (value == HIGH) {
return true;
}
if (value == LOW) {
return false;
}
}
}
void engineTurnOver(){
int prob = 2;
int correctChance = random(0,8);
if (prob <= correctChance){
engineRunning = true;
Serial.println("Success");
delay(1000);
} else {
engineRunning = false;
Serial.println("Failure");
delay(1000);
}
}
void startFailed(){
}
void engineTurnOverReal(){
digitalWrite(enginePin, HIGH);
delay(30000);
digitalWrite(enginePin, LOW);
}
void LCDValues(){
}
void opStopOn(){
Serial.println("OpStopOn");
}
void errorMSG(){
Serial.println("ERROR");
}
void shuttingDownText(){
}
void manualRestart(){
engineRunning = true;
}
bool shutDownCriteriaMet(){
if (engineRunning == true &&
analogRead(brakePressure) > bppLimit &&
digitalRead(SPG) == LOW &&
digitalRead(gear) == HIGH &&
digitalRead(OTMR) == LOW &&
engineRPMCorrectCheck() == true &&
digitalRead(opStop) == LOW){
return true;
}else{
return false;
}
}
bool statsHaveDropped(){
if (analogRead(brakePressure) < bpLimit ||
analogRead(batteryVoltage) < bvLimit){
return true;
}else{
return false;
}
}
bool restartCriteria(){
if (restartButtonCheck == false &&
digitalRead(gear) == HIGH &&
analogRead(engineRPM) == 0 &&
engineRunning == false){
return true;
}else{
return false;
}
}
bool normalRunning(){
if (digitalRead(opStop) == false &&
digitalRead(SPG) == true &&
digitalRead(gear) == true &&
digitalRead(OTMR) == true &&
analogRead(engineRPM) > maxIdleRPM &&
!statsHaveDropped() &&
speedCheck() &&
engineRunning == true){
return true;
}else{
return false;
}
}
void normalRunningText(){
}
void engineRestartSuccessful(){
}
void loop() {
now = millis();
if(!normalRunning() &&
!restartCriteria() &&
!statsHaveDropped() &&
!shutDownCriteriaMet() &&
digitalRead(opStop) == LOW){
errorMSG();
}
if (digitalRead(opStop) == HIGH){
opStopTime = millis();
while (digitalRead(opStop) == HIGH){
opStopOn();
}
engineRunning = false;
}
if (normalRunning()){
while(normalRunning()){
normalRunningText();
}
}
if (shutDownCriteriaMet()){
stopTime = millis();
timerPeriod = initialTimerPeriod;
while(shutDownCriteriaMet() && (millis()-stopTime) < timerPeriod){
shuttingDownText();
if (restartButtonCheck() == false){
timerPeriod += 5000;
}
}
if (millis()-stopTime >= timerPeriod){
counter = 0;
if (digitalRead(mode) == HIGH){
tone(8,262,5000);
engineRunning = false;
}else{
tone(8,500,5000);
while (shutDownCriteriaMet() && millis()-stopTime >= timerPeriod){
engineStopMessage();
}
}
}
}
if (engineRunning == false){
while (statsHaveDropped() == false && restartButtonCheck() == true){
engineStop();
}
if (statsHaveDropped()){
tone(8,1000,5000);
restartRecommendedTime = millis();
timerPeriod = initialTimerPeriod;
while(statsHaveDropped() &&
(engineRunning == false &&
millis()-restartRecommendedTime <= timerPeriod)){
engineRestartMessage();
restartButtonCheck();
}
}while (statsHaveDropped() &&
(millis()-restartRecommendedTime >= timerPeriod &&
counter < 3 && engineRunning == false)){
engineTurnOver();
if (engineRunning == false){
counter +=1;
}
if (engineRunning == true){
engineRestartSuccessful();
}
}while (statsHaveDropped() &&
(millis()-restartRecommendedTime >= timerPeriod &&
counter >= 3)){
startFailed();
}if (restartButtonCheck() == false){
manualRestart();
}
}
}