The Netduino 2 Plus AnalogInput has a range of 0V to 3V3. The Non Invasive Current Sensor we are using returns 0-1V AC for 0-30 Amps AC. To measure the sensor’s output waveform the Energy Monitor Shield uses a voltage divider to offset the reference voltage to 3v3/2. To calibrate the sensor we averaged the input voltage and over 100,000 readings.
The initial code looked like this
int value;
int valueSum = 0;
AnalogInput x1 = new AnalogInput(Cpu.AnalogChannel.ANALOG_0);
stopwatch.Start();
for (int i = 0; i < SampleCount; i++)
{
value = x1.ReadRaw();
valueSum = valueSum + value;
}
stopwatch.Stop();
Duration = 2272 mSec 44014/sec
Duration = 2273 mSec 43994/sec
Duration = 2272 mSec 44014/sec
Duration = 2272 mSec 44014/sec
Duration = 2272 mSec 44014/sec
Duration = 2272 mSec 44014/sec
Duration = 2272 mSec 44014/sec
Duration = 2272 mSec 44014/sec
Duration = 2272 mSec 44014/sec
Duration = 2272 mSec 44014/sec
I then modified the code to not use a temporary variable
int valueSum = 0;
AnalogInput x1 = new AnalogInput(Cpu.AnalogChannel.ANALOG_0);
stopwatch.Start();
for (int i = 0; i < SampleCount; i++)
{
valueSum = valueSum + x1.ReadRaw();
}
stopwatch.Stop();
Duration = 2181 mSec 45829/sec
Duration = 2182 mSec 45829/sec
Duration = 2181 mSec 45829/sec
Duration = 2181 mSec 45829/sec
Duration = 2181 mSec 45829/sec
Duration = 2181 mSec 45829/sec
Duration = 2181 mSec 45829/sec
Duration = 2181 mSec 45829/sec
Duration = 2181 mSec 45829/sec
Duration = 2181 mSec 45829/sec
The code without the temporary variable was slightly faster.
Pingback: Code Camp 2014 Information | devMobile's blog