# Sample for peak ADC values over set interval on pin1

I’ve been banging my head against the wall trying to figure this one out. I hope it’s something silly I’m just missing.
Basically, I’ve got a ACS714 hall effect sensor and I’m attempting to sample the sine wave (I’m wanting to monitor AC current) I’ve seen a few different ways to sample using Arduino code - but nothing that I can seem to properly translate.

I’ve been at this for two days (not a programmer on either platform - yet - and I like to figure these things out the hard way, but I’m lost here - any advice / direction / suggestions would be greatly appreciated. I’ve read over the forums, as well as the porting of code - swapping ‘int’ for ‘local’, ‘void’ for ‘function’ and as many of the other tips from the arduinoport pages.

I can sort out all the math, but I can’t manage to sample enough values in a 60Hz 110v signal to isolate the peaks - and thus have the amps. The logic simply escapes me. I posted the original code as opposed the the modification I’ve attempted to make with the hopes someone can make sense.

Attempt #1
(original Arduino:)

int analogReadAmplitude = 0, min = 512, max = 0, filter = 4;
unsigned long start = millis();
do {
int val = 0;
for (int i = 0; i < filter; i++)
val = (val / filter);
if (max < val) max = val;
if (val < min) min = val;
} while (millis() - start <= 1100/hz); //10% + to ensure p2p is acquired
analogReadAmplitude = (max - min) / 2;
<n\>
<n\>
My Translation:

function getPin1Voltage()
const local sensorPin = hardware.pin1.configure(ANALOG_IN); // pin that the sensor is attached to
const local hz = 60; // frequency of signal
const local filter = 4; // get average of several samples to reduce noize

// Current sensor (ACS714 5A)
local get_mA () {
local max = 0, min = 32767, cnt = 0;
unsigned long start = millis ();
do {
local val = 0;
for (local i = 0; i <filter; i ++)
if (max <val) max = val;
if (min> val) min = val;
cnt ++;
} While (millis () - start <= 1100 / hz); // 10% + for ensure p2p is acquired
local mA = long ((max-min)) * 9333/1000 / filter;
return mA;
}
<n\>
<n\>
Attempt #2 (Arduino code)

int analogReadAmplitude = 0, min = 512, max = 0, filter = 4;
unsigned long start = millis();
do {
int val = 0;
for (int i = 0; i < filter; i++)
val = (val / filter);
if (max < val) max = val;
if (val < min) min = val;
} while (millis() - start <= 1100/hz); //10% + to ensure p2p is acquired
analogReadAmplitude = (max - min) / 2;
<n\>
<n\>
Attempt #3 (This one I thought I almost had working!)

int analogPin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V
long maxVal = 0;
int samples = 10000; // how many samples per reading

void setup()
{
Serial1.begin(9600); // setup serial
pinMode(13, OUTPUT);
digitalWrite(2, HIGH);
}

void loop()
{
maxVal = 0;
// delay(500);
for (int counter = 1; counter < samples; counter++) {
{
}
}

Serial1.println(maxVal); // debug value
}

Reprinted with Code blocks for readability…

Attempt #1
(original Arduino:)

```````
int analogReadAmplitude = 0, min = 512, max = 0, filter = 4;
unsigned long start = millis();
do {
int val = 0;
for (int i = 0; i < filter; i++)
val = (val / filter);
if (max < val) max = val;
if (val < min) min = val;
} while (millis() - start `
``````

My Translation:

```````function getPin1Voltage()
const local sensorPin = hardware.pin1.configure(ANALOG_IN); // pin that the sensor is attached to
const local hz = 60; // frequency of signal
const local filter = 4; // get average of several samples to reduce noize

// Current sensor (ACS714 5A)
local get_mA () {
local max = 0, min = 32767, cnt = 0;
unsigned long start = millis ();
do {
local val = 0;
for (local i = 0; i val) min = val;
cnt ++;
} While (millis () - start `
``````

Attempt #2 (Arduino code)

```````int analogReadAmplitude = 0, min = 512, max = 0, filter = 4;
unsigned long start = millis();
do {
int val = 0;
for (int i = 0; i < filter; i++)
val = (val / filter);
if (max < val) max = val;
if (val < min) min = val;
} while (millis() - start `
``````

Attempt #3 (This one I thought I almost had working!)

```````int analogPin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V
long maxVal = 0;
int samples = 10000; // how many samples per reading

void setup()
{
Serial1.begin(9600); // setup serial
pinMode(13, OUTPUT);
digitalWrite(2, HIGH);
}

void loop()
{
maxVal = 0;
// delay(500);
for (int counter = 1; counter < samples; counter++) {
{
}
}

Serial1.println(maxVal); // debug value
}`
``````

You need to read more of the documentation on Squirrel. Electric Imp has some very good resources on the website. Getting an understanding of the syntax is essential before you can start solving the problem. Have a look at some of the worked examples to get an idea of how variables are declared.

To solve the problem (ie read an AC waveform), you’ll need to consider the sampler class.

Okay - thanks. I figured it was in there, I just haven’t been able to put all the parts together yet. I’ll keep reading and learning. Any pointers when it come to looking at the variable of the sampler class - ie: a good walk through of the classes and how to poll them?

Thanks for cleaning up my messy post too - I’ll be more careful posting in the future. Just been a long couple of days with a fair amount of frustration - but it’s all part of the learning curve I suppose.

Cheers,
A