Loop problem-Generator start sensor monitor-Record while key held on start

Hello all, I have been trying to set up a monitor for taking readings while the start switch is being held in the cranking position on a diesel generator.

The reason for this is so that i can see what things are doing during the cranking phase and then decide what would be useful and reliable as a control/backup for the cranking phase of a generator auto start, besides an rpm device.

At the moment the only sensor that comes on the generator is an oil switch.(also 240v meter but don't know if I can get a reading from it)

For this test i am looking at using:

  • Load sensor on the starter cable (amps)
  • Voltage meter on battery (battery)
  • Voltage meter on the generator output (genVolt)
  • RPM Sensor? (rpm)
  • Oil switch which is already on the unit (oilSwitch)

Process:
-Initialise and print "Ready for start cycle"

-Monitor starter switch

-> Start switch activated
-Print "Starting cycle"
-Reset timer to zero

-> readings() Enter readings loop
-Print time since loop starter cycle started
-Take readings and print them to serial
-Check that start switch is still activated
-If still active > repeat readings()
-If start switch has been released > return to monitoring switch.

If started switch is pressed again reset timer, take readings etc.

At the moment I seem to have the code close to what i was hoping for but the main two things that I seem to be having a problem with it is resetting the timer to zero on the next cycle starts, and keeping it in just the readings() loop while start switch is being held active.(will loop but keeps the counter going from the first cycle instead of offsetting back to zero)
Please find code below.

int startSig  =  3;   //Starter state
int battery   =  A0;   //battery volts
int rpm       =  A1;   //rpm
int amps      =  A2;   //starter amp draw
int oilSwitch =  4;   //oil switch state
int genVolt   =  A3;   //240 volt reading

int OIL;        //variable to store read value

long cycleStart;
long startTime;
long elapsedTime;
int fractional;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(startSig, INPUT_PULLUP);   //Starter state
  pinMode(oilSwitch, INPUT_PULLUP);  //oil switch state
  Serial.println("Pin Modes Set");
  Serial.println("Ready For Start Cycle");
}

void loop() {
  delay(5);

  if (digitalRead(startSig) == LOW) {
    Serial.println("Starting Cycle");  // display cycle start
    cycleStart = millis();
    startTime = millis() - cycleStart;
    Serial.println(startTime);          //zero timer and print once
    readings();
  }
}

void readings()  {          //start engaged take readings
  // if (digitalRead(startSig) == LOW);{
  int battRead = analogRead(battery);      //battery volts
  int rpmRead = analogRead(rpm);           //rpm
  int ampRead = analogRead(amps);         //starter amp draw
  int OIL = digitalRead(oilSwitch);    //oil switch state
  int genVoltRead = analogRead(genVolt);     //240 volt reading

  //Conversions
  float Batt = battRead * (5.0 / 1023);
  float RPM = rpmRead * (5.0 / 1023);
  float AMPS = ampRead * (5.0 / 1023);
  float GENVOLT = genVoltRead * (5.0 / 1023);

  //Print Results
  Serial.print("Battery: ");
  Serial.print(Batt);
  Serial.print(", ");
  Serial.print("RPM: ");
  Serial.print(RPM);
  Serial.print(", ");
  Serial.print("AMPS: ");
  Serial.print(AMPS);
  Serial.print(", ");
  Serial.print("Oil: ");
  Serial.print(OIL);
  Serial.print(", ");
  Serial.print("Gen Volts: ");
  Serial.println(GENVOLT);

  //Print Time
  elapsedTime = millis() - startTime;
  Serial.print((int)(elapsedTime / 1000L));
  Serial.print(".");
  fractional = (int)(elapsedTime % 1000L);
  if (fractional == 0)
    Serial.print("000");
  else if (fractional < 10)
    Serial.print("00");
  else if (fractional < 100)
    Serial.print("0");
  Serial.println(fractional);


  //start released stop readings
  if (startSig == HIGH) {
    Serial.println("END OF START CYCLE");
    loop();
  }

}

I haven't got the sensors yet so i have just got floating values from the analog pins. Figured I should wait until i find out what the input values to expect before trying to map them.
Also not sure what I am going to try for the rpm as yet.
BTW this is one of those cheap imported generators like picture below.
Cheers,
Mowby.

Sorry, Forgot to mention:
Arduino version is 1.6.7
Using an Arduino (Adeept) UNO
Cheers,
Mowby

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

I took only a quick look at your code but something jumped out at me, line 82:

Mowby:

  loop();}

You should never call loop(). The functions called from loop return to loop. If you need to return to loop before the end of the function you can do this:

return;

https://www.arduino.cc/en/Reference/Return

Here's another problem, line 80:

Mowby:

if (startSig == HIGH){

startSig is your pin number (3). HIGH happens to be defined as 1. Therefore that if statement will never be true and "END OF START CYCLE" will never be printed. Did you maybe mean:

if (digitalRead(startSig) == HIGH){

https://www.arduino.cc/en/Reference/DigitalRead

Hi Pert,
Thanks for your speedy reply.
That change worked perfectly!

The cycle now does the loop as usual but now also displays the "end of cycle".
I have posted what is displays below,
The only other things i would like to clean up is displaying "starting cycle" once per cycle.
Also reset the timer on each new start cycle(I may drop the "0" at the start also).
The below displays 2 full start cycles(readouts are currently random as no inputs attached).

Pin Modes Set
Ready For Start Cycle
Starting Cycle
0
Battery: 3.22, RPM: 2.77, AMPS: 2.72, Oil: 1, Gen Volts: 2.91
2.492
Starting Cycle
0
Battery: 3.28, RPM: 3.16, AMPS: 3.90, Oil: 1, Gen Volts: 3.67
2.584
Starting Cycle
0
Battery: 0.79, RPM: 0.99, AMPS: 0.69, Oil: 1, Gen Volts: 0.58
2.677
END OF START CYCLE
Starting Cycle
0
Battery: 1.22, RPM: 1.39, AMPS: 1.31, Oil: 1, Gen Volts: 1.32
4.646
Starting Cycle
0
Battery: 2.76, RPM: 2.66, AMPS: 3.56, Oil: 1, Gen Volts: 3.39
4.739
Starting Cycle
0
Battery: 0.25, RPM: 0.43, AMPS: 0.16, Oil: 1, Gen Volts: 0.04
4.832
END OF START CYCLE

I'll have another fresh look over the code when I get home tonight.
Thanks again,
Mowby

Thought I would post an update. After taking pert's advise and changing the code last night for the digitalRead(startSig) tonight everything else just fell into place with a fresh look over the code.
The code is working just how I was hoping, Now to buy some sensors and do some more tweaking.
Modified code attached below, Also the readings as expected below that(3 Cycles).
Thanks again for the help. It is very much appreciated.
Regards,
Mowby

int startSig  =  3;   //Starter state
int battery   =  A0;   //battery volts
int rpm       =  A1;   //rpm
int amps      =  A2;   //starter amp draw
int oilSwitch =  4;   //oil switch state
int genVolt   =  A3;   //240 volt reading

int OIL;        //variable to store read value
int prevSig = HIGH;    //previous start signal

long cycleStart;
long startTime;
long elapsedTime;
int fractional;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(startSig, INPUT_PULLUP);   //Starter state
  pinMode(oilSwitch, INPUT_PULLUP);  //oil switch state
  Serial.println("Pin Modes Set");
  Serial.println("Ready For Start Cycle");
}

void loop() {
  delay(5);

  if (prevSig == HIGH && digitalRead(startSig) == LOW) {
    Serial.println("Starting Cycle");  // display cycle start
    startTime = millis();
    Serial.println(startTime - millis());        //zero timer and print once
    prevSig = LOW;
    readings();
  }
  else if (prevSig == LOW && digitalRead(startSig) == LOW) {
    readings();
  }
}


void readings()  {          //start engaged take readings
  // if (digitalRead(startSig) == LOW);{
  int battRead = analogRead(battery);      //battery volts
  int rpmRead = analogRead(rpm);           //rpm
  int ampRead = analogRead(amps);         //starter amp draw
  int OIL = digitalRead(oilSwitch);    //oil switch state
  int genVoltRead = analogRead(genVolt);     //240 volt reading

  //Conversions
  float Batt = battRead * (5.0 / 1023);
  float RPM = rpmRead * (5.0 / 1023);
  float AMPS = ampRead * (5.0 / 1023);
  float GENVOLT = genVoltRead * (5.0 / 1023);

  //Print Results
  Serial.print("Battery: ");
  Serial.print(Batt);
  Serial.print(", ");
  Serial.print("RPM: ");
  Serial.print(RPM);
  Serial.print(", ");
  Serial.print("AMPS: ");
  Serial.print(AMPS);
  Serial.print(", ");
  Serial.print("Oil: ");
  Serial.print(OIL);
  Serial.print(", ");
  Serial.print("Gen Volts: ");
  Serial.println(GENVOLT);

  //Print Time
  elapsedTime = millis() - startTime;
  Serial.print((int)(elapsedTime / 1000L));
  Serial.print(".");
  fractional = (int)(elapsedTime % 1000L);
  if (fractional == 0)
    Serial.print("000");
  else if (fractional < 10)
    Serial.print("00");
  else if (fractional < 100)
    Serial.print("0");
  Serial.println(fractional);


  //start released stop readings
  if (digitalRead(startSig) == HIGH) {
    Serial.println("END OF START CYCLE");
    prevSig = HIGH;
    return;
  }

}
Pin Modes Set
Ready For Start Cycle
Starting Cycle
0
Battery: 2.73, RPM: 2.28, AMPS: 2.38, Oil: 1, Gen Volts: 2.17
0.017
Battery: 2.48, RPM: 2.42, AMPS: 2.46, Oil: 1, Gen Volts: 2.35
0.091
Battery: 2.32, RPM: 2.29, AMPS: 2.32, Oil: 1, Gen Volts: 2.30
0.164
Battery: 2.24, RPM: 2.20, AMPS: 2.21, Oil: 1, Gen Volts: 2.21
0.236
Battery: 2.31, RPM: 2.30, AMPS: 2.32, Oil: 1, Gen Volts: 2.28
0.309
END OF START CYCLE
Starting Cycle
0
Battery: 1.99, RPM: 1.89, AMPS: 2.03, Oil: 1, Gen Volts: 2.04
0.017
Battery: 2.09, RPM: 2.03, AMPS: 2.07, Oil: 1, Gen Volts: 2.07
0.090
Battery: 1.95, RPM: 1.89, AMPS: 1.89, Oil: 1, Gen Volts: 1.92
0.163
Battery: 2.01, RPM: 2.00, AMPS: 2.01, Oil: 1, Gen Volts: 1.98
0.235
Battery: 1.95, RPM: 1.93, AMPS: 1.95, Oil: 1, Gen Volts: 1.94
0.308
END OF START CYCLE
Starting Cycle
0
Battery: 1.90, RPM: 1.85, AMPS: 1.99, Oil: 1, Gen Volts: 1.96
0.017
Battery: 1.86, RPM: 1.80, AMPS: 1.83, Oil: 1, Gen Volts: 1.84
0.090
Battery: 1.93, RPM: 1.91, AMPS: 1.93, Oil: 1, Gen Volts: 1.91
0.163
Battery: 1.77, RPM: 1.74, AMPS: 1.74, Oil: 1, Gen Volts: 1.76
0.236
END OF START CYCLE

Glad to hear it's working! Thanks for taking the time to update your thread.
Enjoy! Per