you can't.
but you can't because interrupt on change (like all interrupt) works on digital value, not analogic. But here a trick:
if the input voltage is < 512, the for arduino this is a digital 0, if voltage >= 512 then you have a 1.
So if you want a read only when pin is HIGH (>512) then you need another trick:
the interrupt are
LOW to trigger the interrupt whenever the pin is low,
CHANGE to trigger the interrupt whenever the pin changes value
RISING to trigger when the pin goes from low to high,
FALLING for when the pin goes from high to low.
if there was an interrupt like HIGH to trigger the interrupt whenever the pin is high was ok, but there isn't
so you can do something like this (in pseudocode):
ISR(PCINT1_vect)
{
iHaveToRead=pinstatus;//boolean 1 or 0
}
loop(){
if (iHaveToRead){
Serial.println(ReadSensor(1));
}
}
this is good because, as PaulS said:
Serial.print() in an ISR is really not a good idea, especially at 9600 baud.
Right now this function is called by timer2 interrupt. I've tried @ 4ms but the mcu spend too much time reading the buttons, now i read @ 30ms but i think is slow.
What is the best interval to read the buttons without too much load on the mcu?
also never put delay(10); in interrupt. interrupt has to be executed faster has you can, the best way is set some flag and let the loop do the logic, so you don't have to worry about Serial or delay.
I assure you that way i use 4 change interrupt + 3 analogic read + 1 i2c sensor and my loop is about 4ms
Normally this (the GetKey function) is way too long to be called from an ISR, especially with that delay(10) there.
if(KeyVal == ReadSensor(1))
What does ReadSensor() read from? If it's an analogue input there's a 1 in 1024 chance it will be == KeyVal, meaning that the following code in the if block will probably never be executed.
I would have the ISR and code more like
volatile int sensor_val;
ISR (TMR1_OVL_vect) { // I forget the actual timer vector name but you cannot use a PCINT_vect
sensor_val = ReadSensor(1);
}
loop () {
if (sensor_val > 856) ... etc etc
}
Thank you all for the help provided.
I manage to get the right delay for buttons debouce and i call getkey function from a loop, so i don'd need the interrupt anymore