# INTERRUPT not working properly

once I upload my programme, the first time interrupt work properly but when I am trying to call it the second time or third time interrupts not working and automatically loop function start running without getting true.Basically am calling switch position in both ISR(interrupt) and in "if condition".
Help me out with this problem.

void setup()
{

PCintPort::attachInterrupt(A4,UP_STOP,RISING);

}

void loop() {

if(XYZ_STOP())
{
Serial.println("going up..");

}
}

void UP_STOP()
{
int _switch1 = analogRead(REVERSESTOP); //A4

float voltage = _switch1 * (5.0 / 1023.0);

if (voltage > 2)
{
Serial.println("stop.");

}
}

bool XYZ_STOP()
{
int _switch1 = analogRead(FORWARDSTOP); //A5

float voltage = _switch1 * (5.0 / 1023.0);

if (voltage > 2)
{
Serial.println(voltage);
return true;
}
else
{
return false;
}
}

It is not a good idea to use Serial.print() in an ISR because interrupts are automatically disabled and Serial.print() uses interrupts

Also, the divide operation is one of the slowest math operations you can do, so why not change

``````  float voltage = _switch1 * (5.0 / 1023.0);
``````

to

``````  float voltage = ( (float) _switch1) * .004887;      // .004887 = 5 / 1023
``````

Why are you bothering to calculate the voltage at all?

With your calculator work out what ADC value corresponds to the voltage you are interested in and just check for that ADC value.

...R

econjack:
Also, the divide operation is one of the slowest math operations you can do, so why not change

``````  float voltage = _switch1 * (5.0 / 1023.0);
``````

to

``````  float voltage = ( (float) _switch1) * .004887;      // .004887 = 5 / 1023
``````

There is no point doing that yourself when the compiler will do that for you.