#define ONE_WIRE_BUS 13
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int count = 0;
unsigned long pHigh;
unsigned long pLow;
unsigned long interval;
unsigned long RPS;
unsigned long total;
unsigned long start_time;
int choke_on = 8;
int choke_off = 9;
int start_ctrl = 10;
int val;
int average;
int pulsePin = 5;
int button1 = 14;
void setup(){
pinMode (pulsePin, INPUT);
pinMode (choke_on, OUTPUT);
pinMode (choke_off, OUTPUT);
Serial.begin(9600);
sensors.begin();
}
void loop(){
if (button1==HIGH) {
starter_engaged();
}
}
void starter_engaged() { //
start_time = millis();
total = 0;
for (int i = 0; i < 5; )
{
pHigh = pulseIn(pulsePin, HIGH, 100000);
pLow = pulseIn(pulsePin, LOW, 100000);
unsigned long interval = pHigh + pLow;
if ( pHigh > 0 && pLow > 0 && interval > 6000 )
{
Serial.print(pHigh);
Serial.print("\t:\t");
Serial.print(pLow);
RPS = (1000000UL / interval);
Serial.print("\tRPS: ");
Serial.println(RPS);
total = total + RPS;
i++;
}
else if ((millis()-start_time) > 3300) { // Test if 3.3 seconds has passed since the starter was engaged
digitalWrite(start_ctrl, LOW);
Serial.println ("STARTER OFF");
disengage_starter_timeout();
}
average = total / 5;
Serial.print(" Average RPS: ");
Serial.println(average);
if (average >= 25) {
digitalWrite(start_ctrl, LOW);
Serial.println ("STARTER OFF");
Serial.print ("RPS: ");
Serial.println (RPS);
Serial.println ("ENGINE RUNNING");// Send "Engine Running" message after engine has started
delay(10);
disengage_starter(); // Go to disengage_starter after engine is running
}
// else {
// starter_engaged();
}
}
void disengage_starter() { // disengage_starter()
sensors.requestTemperatures(); // Read the temperature
val = (sensors.getTempCByIndex(0)); // Store temp
if (val < 0) {
delay(100);
Serial.println ("WARM UP ACTIVE");
delay(10);
Serial.print ("WAIT : ");
Serial.println (val);
disengage_starter(); // repeat until 0 degrees
} else if (val >= 0) {
digitalWrite(choke_on, LOW);
digitalWrite(choke_off, HIGH);
delay(500);
digitalWrite(choke_off, LOW);
Serial.println ("CHOKE OFF");
delay(10);
loop();
}
}
void disengage_starter_timeout() { //disengage_starter_timeout()
digitalWrite(start_ctrl, LOW); // Disengage the starter
Serial.println ("ENGINE START UNSUCESSFUL");
delay(1300);
starter_REngaged(); // Restart engine
}
void starter_REngaged() { // starter_REngaged()
if (count < 3) {
count++; // Increment the count
Serial.println ("RESTARTING");
delay(1200);
Serial.print ("ATTEMPT No: ");
Serial.println (count);
starter_engaged();
} else {
Serial.println ("EXCEEDED 3 ATTEMPTS!!! CHECK SYSTEM!!!");
count = 0; //Reset counter
loop();
}
}
Now the conditions in the for loop in starter_engaged() requires 5 sets of pulses to exit. My concern is that if for some reason the pulses arent read then the condition is not met, so if that is the case can i break; from the for loop in subsequent loops such as starter_REngaged()?