For...Next Loop Problem?

tracecom

Senior Member
The for...next loop compiles for an 08M2, but doesn't produce the results expected. It seems that the Sum = Sum + Adval isn't working. What am I doing wrong? Thanks.

Code:
Symbol ADVal = W0
Symbol Sum = W1

main:
ADVal = 0
Sum = 0

for ADVal = 1 to 8
	readadc 4, ADVal
	Sum = Sum + ADVal
next ADVal

ADVal = Sum / 8
 

hippy

Technical Support
Staff member
Code:
for ADVal = 1 to 8
	readadc 4, ADVal
You are overwriting the ADVal FOR-NEXT index variable when you read the ADC. That is disrupting the FOR-NEXT loop. Use a different variable as the index variable.
 

tracecom

Senior Member
Code:
for ADVal = 1 to 8
	readadc 4, ADVal
You are overwriting the ADVal FOR-NEXT index variable when you read the ADC. That is disrupting the FOR-NEXT loop. Use a different variable as the index variable.
That fixed it. I knew I was doing something stupid, but I could not figure out what it was. Thanks.
 
Top