Skip to content

Commit 2a6eaee

Browse files
authored
fix build (#34)
* fix #33 build * redo setADC() * update readme.md
1 parent f5094d7 commit 2a6eaee

File tree

6 files changed

+145
-103
lines changed

6 files changed

+145
-103
lines changed

ACS712.cpp

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: ACS712.cpp
33
// AUTHOR: Rob Tillaart, Pete Thompson
4-
// VERSION: 0.3.4
4+
// VERSION: 0.3.5
55
// DATE: 2020-08-02
66
// PURPOSE: ACS712 library - current measurement
77
// URL: https://github.com/RobTillaart/ACS712
@@ -26,7 +26,7 @@ ACS712::ACS712(uint8_t analogPin, float volts, uint16_t maxADC, float mVperAmper
2626
_midPoint = maxADC / 2;
2727

2828
// default ADC is internal.
29-
setADC(_internalAnalog, volts, maxADC);
29+
setADC(NULL, volts, maxADC);
3030
}
3131

3232

@@ -42,16 +42,16 @@ float ACS712::mA_peak2peak(float frequency, uint16_t cycles)
4242
{
4343
int minimum, maximum;
4444
// Better than using midPoint
45-
minimum = maximum = _readADC(_pin);
45+
minimum = maximum = _analogRead(_pin);
4646

4747
// find minimum and maximum
4848
uint32_t start = micros();
4949
while (micros() - start < period) // UNO ~180 samples...
5050
{
51-
int value = _readADC(_pin);
51+
int value = _analogRead(_pin);
5252
if (_suppresNoise) // average 2 samples.
5353
{
54-
value = (value + _readADC(_pin))/2;
54+
value = (value + _analogRead(_pin))/2;
5555
}
5656
// determine extremes
5757
if (value < minimum) minimum = value;
@@ -82,17 +82,17 @@ float ACS712::mA_AC(float frequency, uint16_t cycles)
8282
uint16_t zeros = 0;
8383

8484
int _min, _max;
85-
_min = _max = _readADC(_pin);
85+
_min = _max = _analogRead(_pin);
8686

8787
// find minimum and maximum and count the zero-level "percentage"
8888
uint32_t start = micros();
8989
while (micros() - start < period) // UNO ~180 samples...
9090
{
9191
samples++;
92-
int value = _readADC(_pin);
92+
int value = _analogRead(_pin);
9393
if (_suppresNoise) // average 2 samples.
9494
{
95-
value = (value + _readADC(_pin))/2;
95+
value = (value + _analogRead(_pin))/2;
9696
}
9797
// determine extremes
9898
if (value < _min) _min = value;
@@ -144,10 +144,10 @@ float ACS712::mA_AC_sampling(float frequency, uint16_t cycles)
144144
while (micros() - start < period)
145145
{
146146
samples++;
147-
int value = _readADC(_pin);
147+
int value = _analogRead(_pin);
148148
if (_suppresNoise) // average 2 samples.
149149
{
150-
value = (value + _readADC(_pin))/2;
150+
value = (value + _analogRead(_pin))/2;
151151
}
152152
float current = value - _midPoint;
153153
sumSquared += (current * current);
@@ -169,15 +169,15 @@ float ACS712::mA_AC_sampling(float frequency, uint16_t cycles)
169169
float ACS712::mA_DC(uint16_t cycles)
170170
{
171171
// read at least twice to stabilize the ADC
172-
_readADC(_pin);
172+
_analogRead(_pin);
173173
if (cycles == 0) cycles = 1;
174174
float sum = 0;
175175
for (uint16_t i = 0; i < cycles; i++)
176176
{
177-
int value = _readADC(_pin);
177+
int value = _analogRead(_pin);
178178
if (_suppresNoise) // average 2 samples.
179179
{
180-
value = (value + _readADC(_pin))/2;
180+
value = (value + _analogRead(_pin))/2;
181181
}
182182
sum += (value - _midPoint);
183183
}
@@ -233,7 +233,7 @@ uint16_t ACS712::autoMidPoint(float frequency, uint16_t cycles)
233233
uint32_t start = micros();
234234
while (micros() - start < twoPeriods)
235235
{
236-
uint16_t reading = _readADC(_pin);
236+
uint16_t reading = _analogRead(_pin);
237237
subTotal += reading;
238238
samples++;
239239
// Delaying prevents overflow
@@ -329,14 +329,14 @@ float ACS712::detectFrequency(float minimalFrequency)
329329
{
330330
int maximum = 0;
331331
int minimum = 0;
332-
maximum = minimum = _readADC(_pin);
332+
maximum = minimum = _analogRead(_pin);
333333

334334
// determine maxima
335335
uint32_t timeOut = round(1000000.0 / minimalFrequency);
336336
uint32_t start = micros();
337337
while (micros() - start < timeOut)
338338
{
339-
int value = _readADC(_pin);
339+
int value = _analogRead(_pin);
340340
if (value > maximum) maximum = value;
341341
if (value < minimum) minimum = value;
342342
}
@@ -352,13 +352,13 @@ float ACS712::detectFrequency(float minimalFrequency)
352352
timeOut *= 10;
353353
start = micros();
354354
// casting to int to keep compiler happy.
355-
while ((int(_readADC(_pin)) > Q1) && ((micros() - start) < timeOut));
356-
while ((int(_readADC(_pin)) <= Q3) && ((micros() - start) < timeOut));
355+
while ((int(_analogRead(_pin)) > Q1) && ((micros() - start) < timeOut));
356+
while ((int(_analogRead(_pin)) <= Q3) && ((micros() - start) < timeOut));
357357
start = micros();
358358
for (int i = 0; i < 10; i++)
359359
{
360-
while ((int(_readADC(_pin)) > Q1) && ((micros() - start) < timeOut));
361-
while ((int(_readADC(_pin)) <= Q3) && ((micros() - start) < timeOut));
360+
while ((int(_analogRead(_pin)) > Q1) && ((micros() - start) < timeOut));
361+
while ((int(_analogRead(_pin)) <= Q3) && ((micros() - start) < timeOut));
362362
}
363363
uint32_t stop = micros();
364364

@@ -386,13 +386,13 @@ float ACS712::getMicrosAdjust()
386386
// DEBUG
387387
uint16_t ACS712::getMinimum(uint16_t milliSeconds)
388388
{
389-
uint16_t minimum = _readADC(_pin);
389+
uint16_t minimum = _analogRead(_pin);
390390

391391
// find minimum
392392
uint32_t start = millis();
393393
while (millis() - start < milliSeconds)
394394
{
395-
uint16_t value = _readADC(_pin);
395+
uint16_t value = _analogRead(_pin);
396396
if (value < minimum) minimum = value;
397397
}
398398
return minimum;
@@ -401,13 +401,13 @@ uint16_t ACS712::getMinimum(uint16_t milliSeconds)
401401

402402
uint16_t ACS712::getMaximum(uint16_t milliSeconds)
403403
{
404-
uint16_t maximum = _readADC(_pin);
404+
uint16_t maximum = _analogRead(_pin);
405405

406406
// find minimum
407407
uint32_t start = millis();
408408
while (millis() - start < milliSeconds)
409409
{
410-
uint16_t value = _readADC(_pin);
410+
uint16_t value = _analogRead(_pin);
411411
if (value > maximum) maximum = value;
412412
}
413413
return maximum;
@@ -425,5 +425,17 @@ void ACS712::setADC(uint16_t (* f)(uint8_t), float volts, uint16_t maxADC)
425425
}
426426

427427

428+
//////////////////////////////////////////////////////////////////////
429+
//
430+
// PRIVATE
431+
//
432+
uint16_t ACS712::_analogRead(uint8_t pin)
433+
{
434+
// if extern ADC is defined use it.
435+
if (_readADC != NULL) return _readADC(pin);
436+
return analogRead(pin);
437+
}
438+
439+
428440
// -- END OF FILE --
429441

ACS712.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: ACS712.h
44
// AUTHOR: Rob Tillaart, Pete Thompson
5-
// VERSION: 0.3.4
5+
// VERSION: 0.3.5
66
// DATE: 2020-08-02
77
// PURPOSE: ACS712 library - current measurement
88
// URL: https://github.com/RobTillaart/ACS712
@@ -13,7 +13,7 @@
1313

1414
#include "Arduino.h"
1515

16-
#define ACS712_LIB_VERSION (F("0.3.4"))
16+
#define ACS712_LIB_VERSION (F("0.3.5"))
1717

1818

1919
// ACS712_FF_SINUS == 1.0/sqrt(2) == 0.5 * sqrt(2)
@@ -123,16 +123,10 @@ class ACS712
123123
// EXPERIMENTAL 0.3.4
124124
// supports up to 16 bits ADC.
125125
uint16_t (* _readADC)(uint8_t);
126+
uint16_t _analogRead(uint8_t pin);
127+
126128
};
127129

128130

129-
// wrapper for internal analogRead()
130-
// solves platform specific casting.
131-
static uint16_t _internalAnalog(uint8_t pin)
132-
{
133-
return analogRead(pin);
134-
}
135-
136-
137131
// -- END OF FILE --
138132

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.3.5] - 2023-01-18
10+
- fix #33 failing build => issue 345 created @ arduino-ci
11+
- redo **setADC()**
12+
- allows reset to internal **analogRead()** too now.
13+
- update README.md
14+
15+
916
## [0.3.4] - 2023-01-14
1017
- experimental
1118
- add **void setADC()** to use an external ADC for measurements.
1219
- add **static uint16_t internalAnalog(uint8_t p)** wrapping analogRead() - solves casting.
1320
- add example ACS712_20_DC_external_ADC.ino
1421

15-
1622
## [0.3.3] - 2023-01-03
1723
- update GitHub actions
1824
- update license

0 commit comments

Comments
 (0)