Hi,
first I suggest to use a debounce function to read the digital signals coming from mechanical buttons (there is an Arduino library to do this Arduino Playground - Bounce). Then you need to save the current ON/OFF state and update it as shown below:
#include <Bounce.h>
// ... your variables ...
#define ENGINE_OFF 0
#define ENGINE_ON 1
int currentState = ENGINE_OFF;
Bounce btnOnOff = Bounce( 4, 10 ); // input pin for start and off button (pin 4)
Bounce btnClutch = Bounce( 5, 10 ); // input pin for clutch (pin 5)
void setup(){
// ...
}
void loop() {
RPM = analogRead(TacSig); // read the value from the sensor
Serial.println(RPM); // print the value to the serial port
// read the buttons state
int clutch = btnClutch.update();
int start = btnOnOff.update();
if( start == HIGH ) // if start button is pressed
{
if( currentState == ENGINE_OFF )
{
// execute start procedure (read the clutch value if needed using clutch.read() ).
// update the current state (currentState = ENGINE_ON) only if the engine
// is started.
}
else
{
// execute the stop procedure and update the current state (currentState = ENGINE_OFF)
}
}
}