void shutDown() { //engine shutdown, execute relay
//If the charging current drops to 0 to 5 amperes and charging voltage is under 15 volts (safety) and motor is running
// start the shutdown function and turn off the relay
if (current >= 0 && current < 5 && sensVoltage < 15 && digitalRead(motorPin) == HIGH && motorStatus == 2) {
digitalWrite(relayPin, LOW);
if (millis() - lastTime >= 2000) { //wait at least 2 seconds to engine stall down
analogWrite(pwmPin, 255); // Stop Duty Cycle at 100 %, alternator electromagnet off
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sammutus");
motorStatus = 3; //motor has run, to reset, switch power off, on next startup, it will be reset
#ifdef DEBUG
Serial.println("Sammutettu.");
#endif
}
} else if (motorStatus == 2) {
lastTime = millis();
digitalWrite(relayPin, HIGH); //else, keep relay on
#ifdef DEBUG
Serial.println("Kaynnissa.");
#endif
}
}