Newbe: Nested For..Next loops problem
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
Re: Newbe: Nested For..Next loops problem
Sorted it!
It was another issue lower down in the code, a missing ENDIF causing the compiler not to find the the two NEXT statements. :rolleyes:
It was really bugging me for a while. Should have had a cup of coffee and re-look at the code.
Lesson learned !
Thank for the forum space
Re: Newbe: Nested For..Next loops problem
Hi, Biggazzzz
3 sensors ??? ;)
could also be written
Code:
mainloop:
For index = 1 to 3 ; read 10 values for 3 sensors
FOR index2 = 1 to 10 ; 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
ELSE IF temp_val[index2] > temp_val_hi[index] then ; is reading higher than highest
temp_val_hi[index] = temp_val[index2] ; update highest
ENDIF
temp1[index] = temp1[index] + temp_val[index2] ; add reading to running total
NEXT index2 ; update next reading
NEXT index ; update next sensor
GOTO mainloop
as you add temp1[index] to total whatever the value ...
Alain
Re: Newbe: Nested For..Next loops problem
Cheers Alain,
Anything to make the code look better and save code space :)
Having a bit of a problem with the final maths i.e. dropping the highest and lowest readings and divide result 8.
for some reason result (636) // 8 is giving be 0.4 and not 0.5 as expected. I'm still working on it before I bail out and ask why here :D
Re: Newbe: Nested For..Next loops problem
Hi,
I'm sure you've figured it out by now but the // operator returns the "reminder". When you // by 10 this happens to be what would be on the right hand side of the decimal point but when // by 8 it's not.
(636/8=79.5)
636 / 8 = 79 and 636 // 8 = 4 because 79*8+4=636
(636 / 10 = 63.6)
636 / 10 = 63 and 636 // 10 = 6 because 63*10+6=636
Re: Newbe: Nested For..Next loops problem
thanks guy's
I was going for the following
636 was the running total of 10 samples minus the highest and lowest. That left 8 samples.
I therefore wanted to divide 636 by to get 79.5.
I ended up multiplying by 50 and diving by 4, then handle the digits either side of the decimal place (see example) it's work in progress, but welcome any pointers
Code:
temp_val[1] = 100 ; real tempeture 10.0c
temp_val[2] = 115 ; 11.5c
temp_val[3] = 122 ; 12c etc...
temp_val[4] = 139
temp_val[5] = 150
temp_val[6] = 130
temp_val[7] = 80
temp_val[8] = 100
temp_val[9] = 115
temp_val[10] = 252
' total = 1301
' lowest = 8.0c
' highest = 25.2c
' middle 8 samples = 971
' average of the 8 sample = 121.1375 or 12.137c
;----[Main Program]---------------------------------------------------------
mainloop:
'index = 0
'index2 = 0
temp1[1] = 0
temp1[2] = 0
temp1[3] = 0
for index = 1 to 3 step 1 ; Take 10 sampels from 3 sensors
For index2 = 1 to 10 step 1 ; Take 10 reading
IF index2 = 1 THEN
temp_val_lo[index] = temp_val[1] ; Set initial lowest reading to first reading
temp_val_hi[index] = temp_val[1] ; Set initial highest reading to first reading
endif
LCDOUT $fe,1,"Sensor ",DEC index, " Data ",DEC index2
if temp_val[index2] <= temp_val_lo[index] then ; If readling lower than lowest
temp_val_lo[index] = temp_val[index2] ; Update lowest
elseif temp_val[index2] => temp_val_hi[index] then ; If reading higher than highest
temp_val_hi[index] = temp_val[index2] ; Update highest
ENDIF ; Else reading is not highest or lowest and....
temp1[index] = temp1[index] + temp_val[index2] ; Add reading to running total
next index2 ; Update next sample
temp1[index] = temp1[index] - temp_val_hi[index] ; Subtract the highest reading
temp1[index] = temp1[index] - temp_val_lo[index] ; Subtract the lowest reading
temp1[index] = (temp1[index] * 50)/4 ; Get rid of the decimal places
LCDOUT $FE,1, "Sensor = ",DEC index
LCDOUT $FE, $C0,"Temp = ",DEC temp1[index] DIG 4, DEC temp1[index] DIG 3,".", DEC temp1[index] dig 2, dec temp1[index] dig 1, dec temp1[index] Dig 0
PAUSE 2000
next index ; update next sensor
PAUse 5000
goto mainloop
End
1 Attachment(s)
Re: Newbe: Nested For..Next loops problem
Attachment 5164
Quote:
Originally Posted by
Big Gaz
Sorted it!
It was another issue lower down in the code, a missing ENDIF causing the compiler not to find the the two NEXT statements. :rolleyes:
Exactly what FinelineIDE is for.
Red "O" is for orphan.
Norm