analogRead + digitalRead crashes IDE

I have two input devices - a timing gear sensor and an analog potentiometer.

If I just run the "ReadTimingGear" or just run the "CheckEngineLoad" functions individually I get the results I expect. However if I allow both functions to run in the same code the IDE crashed.

Should I be using a digital pot rather than an analog one for this excersise?

Thanks, Daniel.

int SensorValue = 3; // Read A2 pin for throttle load value from the potentiometer
float ThrottleLoad = 0; // Stores the current load of the throttle

int SetPin4 = 4;  // LED connected to digital pin 13
int SetPin2 = 2;    // pushbutton connected to digital pin 7
int Pin2Read = 0;      // variable to store the read value
float SetPin3 = 3;
float Pin3Read = 0;
int Last2PinRead = 0;
int SetPin6 = 6;
int Pin6Read = 0;
int Last6PinRead = 0;

unsigned long PreviousMillis = 0;
unsigned long CurrentMillis = 0;
unsigned long RPM = 0;


int CounterOne = 0;

void setup(void)
{
	//pinMode(led_pin, OUTPUT);
	Serial.begin(115200);
	pinMode(SetPin4, OUTPUT);  // sets the digital pin 4 as output
	pinMode(SetPin6, INPUT_PULLUP);    // sets the digital pin 2 as input
	pinMode(SetPin3, INPUT_PULLUP);

	sei();

}


void loop()
{
	attachInterrupt(digitalPinToInterrupt(2), ReadTimingGear, RISING);
	CheckEngineLoad();
}

void ReadTimingGear() {
	CurrentMillis = millis();
	Serial.println(CurrentMillis);


}

void CheckEngineLoad()
{
	ThrottleLoad = analogRead(SensorValue) * (100.0 / 1023.0);
	Serial.println(ThrottleLoad);
}

However if I allow both functions to run in the same code the IDE crashed

Can you explain that sentence, please?

float SetPin3 = 3; Why use a floating point variable to hold a pin number, which is inherently an integer value?

And when we tell you not to do serial I/O in interrupt context, we really, really mean it.

the IDE crashed

Extremely unlikely. Tell us what actually happened.

Fatal problems with your code:

  1. Variables shared with interrupts MUST be declared volatile.
volatile unsigned long CurrentMillis = 0;
  1. As mentioned above:
void ReadTimingGear() {
	CurrentMillis = millis();
	Serial.println(CurrentMillis); //REMOVE
}

jremington:
Extremely unlikely. Tell us what actually happened.

Thanks for the reply and thanks for pointing out the problems with my code.

What actually happens to the IDE is that it totally freezes. As with a non responding window. I am thinking that maybe it is just tied up processing but if I comment out one function call or the other everything runs as expected it's only when I allow both functions to be called that the problem is shown.

Thanks, Daniel.

OK. I'll just let replies 1 & 2 sink in.

DanielHowden:
What actually happens to the IDE is that it totally freezes.

Do you mean the serial monitor? (The IDE being where you edit your code and kick off its compilation and uploading.)

Hal_Posoyan:
Do you mean the serial monitor? (The IDE being where you edit your code and kick off its compilation and uploading.)

Yes the serial monitor and the IDE, they both become unresponsive.

How does the code look now?