Hi, trying to fill a list, below is my code and re...
# help
m
import gpio import gpio.adc show Adc SampleSize := 3 Sample := 0 SampleTotal := 0.0 adc := Adc (gpio.Pin 34) Readings := [] main: for i := 0; i < SampleSize; i += 1: Readings[i] = adc.get sleep --ms=1 //print Readings[i] Calculate_RMS sleep --ms=10000 Calculate_RMS: Readings.do: SampleTotal = SampleTotal + it.SampleTotal print SampleTotal print Sample Decoding by
jag
, device has version ****************************************************************************** EXCEPTION error. OUT_OF_BOUNDS 0: List_.[]= /core/collections.toit:1865:24 1: main adc_test.toit:14:17 ******************************************************************************
line 14 is Readings[i] = adc.get
After much trial & error I fixed it. My answer was:
for i := 0;i < SampleSize - 1; i += 1: Reading = adc.get Readings.add Reading
k
i < SampleSize was correct.
... and an alternative to calling add (which grows the list) is to do
Readings := List SampleSize
before the loop.
You can even try
Readings := List SampleSize: adc.get
and drop the loop.
It calls the block passed to the list constructor SampleSize times.
You can sleep from within there too.
m
Thanks @kasperl, yes I discovered that Readings kept growing, so cleared it after each "loop" I will try your suggestion.