Hi Guy's

I'm having problems with MCSP and PBP reporting a for..next loop error for the code below.

the error reported is:

'Error Line 94: FOR without matching NEXT. (sampling.pbp)
'Error Line 96: FOR without matching NEXT. (sampling.pbp)'

These are the For index.... and For index2.... in my code.

But i can see the two next statements and they are in line.

I have even but then together before cutting and pasting my code in between. What am I doing wrong?

The code below is some not elegant code I'm starting to work on, where I would like to take 10 samples for each of three sensors, hence the need for a 10 way loop within a 3 way loop.

I'm sure there is a better way of doing it, but i'm learning at the moment and trying things out.

Eventually I want to record the highest and lowest two reading, drop these and average the remaining 8 samples. Only if I get a valid reading (DS18B20 temp sensors)

Code:
mainloop:

For index = 1 to 3 step 1                                   ; read 10 values for 3 sensors
 
    FOR index2 = 1 to 10  step 1                            ; Take 10 reading
          
        IF temp_val[index2] < temp_val_lo[index] then       ; is readlng lower than lowest
            temp_val_lo[index] = temp_val[index2]           ; update lowest
            temp1[index] = temp1[index] + temp_val[index2]  ; add reading to running total 
                            
        ELSE IF temp_val[index2] > temp_val_hi[index] then  ; is reading higher than highest
            temp_val_hi[index] = temp_val[index2]           ; update highest
            temp1[index] = temp1[index] + temp_val[index2]  ; add reading to running total

        ELSE                                                ; if reading not highest or lowest 
            temp1[index] = temp1[index] + temp_val[index2]  ; add reading to running total   
    
    NEXT index2                                             ; update next reading
            
NEXT index                                                  ; update next sensor

GOTO mainloop