Interupt handler seems to ignore conditions

Hi,

I have a program where i am calling an external interrupt upon the falling edge.

within the interrupt I have a switch block that determines what is done during interrupt.

For some reason it seems to ignore the switch block and run both conditions regardless whenever I trigger it. The condition variable is cal_state and it is initialized to 0 at the beginning of the program

interrupt [EXT_INT0] void ext_int0_isr(void)
{
 
#asm("cli");

 //PORTB.3 = 1;    //set LED light on showing when programing    
 avg_height = 0;
 analog_value = 0; 
 
 for(i=0;i<10;i++){
   avg_height = avg_height + height_value[i]; 
 }
 switch(cal_state){
 
 case 0:
    
 analog_value =  read_adc(0x02);
 
 eeprom_var = ((analog_value - 100.12) / 2.2819);
 startup(); 
   
          
 cal_state = 1;     
 break;
                 
 case 1:
      PORTB.3 = 1;
    
    KNEEL_1 = avg_height/10;
    
      
    kneel = KNEEL_1;
 cal_state = 0;
 break;
 
 }

Can anyone see anything terribly wrong with my code?

Can anyone see anything terribly wrong with my code?

You mean apart from that fact that you haven't posted all of it?

What does "setup" do?
Where is the code for "read_adc"?

// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
 
#asm("cli");

 PORTB.3 = 1;    //set LED light on showing when programing    
 avg_height = 0;
 
 for(i=0;i<10;i++){
   avg_height = avg_height + height_value[i]; 
 }
 switch(cal_state){
 
 case 0: 
 analog_value =  read_adc(0x02);
 eeprom_var = ((analog_value - 100.12) / 2.2802);   
 cal_state = 1;     
 break;

 case 1:
 cal_state++;
 break;
                 
 case 2:
 KNEEL_1 = avg_height/10;    
 cal_state = 4;
 break;
 
 case 4: 
 cal_state++;
 break;
 
 case 5:
 HIGHBUOY_1 = avg_height/10;
 cal_state = 6;
 break;
 
 case 6:
 cal_state =0;
 
 }   
    

 //for(i=0; i<32000;i++){ for(analog_value=0;analog_value <15;analog_value++){}}              
 for(i=0;i<32000;i++){ 
 analog_value =0;
 while(analog_value <20){analog_value++;}
 }
    
  startup();                                    
  state = 0; 
   PORTB.3 = 0;      //set LED light off showing programming done
     
}

Thats my current code. I noticed it wasn't ignoring the conditions, but infact it was triggering twice for no reason. What I did didn't solve the problem, but it avoids it.

Here is my code for start up. (all it's doing is taking certain values which are in eeprom ((eeprom_var, HIGHBUOY_1, KNEEL_!)) and putting them into global variables.

void startup(void){ 
          
          ref_height = eeprom_var;
                 
          //loops 180 degrees in both directions for reference points
          if( (ref_height - 180) < 0){
            low_ref = 180 + ref_height;
          }
          else{
            low_ref = ref_height - 180;
          }
          if( (ref_height + 179) > 360){
                  high_ref = ref_height - 179;
          }
          else{
              high_ref = ref_height + 179;
          }    
          kneel = KNEEL_1;
          highbuoy = HIGHBUOY_1; 
          mid_high = highbuoy - 2;
          ride_high = highbuoy -4;                          
        }

readadc is a stardard ADC from a sensor. It works fine, but here is the code regardless.

unsigned int read_adc(unsigned char adc_input)
{
ADMUX=(adc_input & 0x0f) | ADC_VREF_TYPE;
// Delay needed for the stabilization of the ADC input voltage

for(i=0;i<1000;i++){}

//delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}

I believe the issue is a bouncing issue. That being said i'm not sure which is the best way to go about avoiding it. I could poll the pin that the interrupt is on, and if X amount of time has passed where it is still high, do the code that i have in the interrupt.

I think thats likely the best way to avoid the double triggering.

Any other ideas?

When an bouncing input is triggering the interrupt line it is best to use hardware debouncing with a resistor and capacitor.