Skip to content

Commit 641d5b7

Browse files
author
Main Push Robot
committed
Committing dev changes
1 parent 8b8914d commit 641d5b7

7 files changed

+59
-24
lines changed

examples/analogRead/analogRead.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
***************************************************/
1212

1313
#include "Hall-Effect-SOLDERED.h"
14-
#define HALL_EFFECT_PIN A0
14+
#define HALL_EFFECT_PIN 32
1515

1616
// For this example, connect the Hall Effect Sensor to your Dasduino board as such:
1717
// VCC to VCC on Dasduino

examples/analogRead_easyC/analogRead_easyC.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void setup()
4747
void loop()
4848
{
4949
// Read raw measurement
50-
int hallRawReading = hall.getRawReading();
50+
uint16_t hallRawReading = hall.getRawReading();
5151

5252
// Read milli Teslas
5353
float hallMilliTeslas = hall.getMilliTeslas();

keywords.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
# Datatypes (KEYWORD1)
77
##################################################
88

9-
HallEffect_Digital KEYWORD1
10-
HallEffect_Digital_EasyC KEYWORD1
11-
HallEffect_Analog KEYWORD1
12-
HallEffect_Analog_EasyC KEYWORD1
9+
HallEffect_Digital KEYWORD1
10+
HallEffect_Digital_EasyC KEYWORD1
11+
HallEffect_Analog KEYWORD1
12+
HallEffect_Analog_EasyC KEYWORD1
1313

1414
##################################################
1515
# Methods and Functions (KEYWORD2)
1616
##################################################
1717

18-
getReading KEYWORD2
19-
getRawReading KEYWORD2
20-
getMilliTeslas KEYWORD2
18+
getReading KEYWORD2
19+
getRawReading KEYWORD2
20+
getMilliTeslas KEYWORD2
2121

2222
##################################################
2323
# Constants (LITERAL1)

src/Hall-Effect-Analog-SOLDERED.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,12 @@ HallEffect_Analog::HallEffect_Analog(int _pin)
2727
/**
2828
* @brief Reads hall effect sensor value
2929
*
30-
* @return Returns a unit16_4 sensor reading value.
31-
*
30+
* @return Returns a unit16_t sensor reading value.
31+
*
3232
*/
3333
uint16_t HallEffect_Analog::getReading()
3434
{
35-
#ifdef ESP32
36-
uint32_t value = analogReadMilliVolts(pin);
37-
return map(value, 142, 3166, 0, 1024);
38-
#else
39-
return analogRead(pin);
40-
#endif
35+
return analogRead(pin);
4136
}
4237

4338
/**
@@ -51,5 +46,17 @@ uint16_t HallEffect_Analog::getReading()
5146
float HallEffect_Analog::getMilliTeslas()
5247
{
5348
float value = float(getReading());
54-
return 20.47 * (10 * (value / 1023.0) / 5.0 - 1);
49+
// Fix for ESP32's ADC
50+
#ifdef ESP32
51+
if(value >= 2710)
52+
{
53+
return (value - 2710.0) * (20.47 - 0.0) / (4095.0 - 2710.0) + 0.0;
54+
}
55+
else
56+
{
57+
return (value) * (20.47) / (2710.0) - 20.47;
58+
}
59+
#else
60+
return 20.47 * (NUM_BITS * (value / (ADC_MAX - 1)) / VOLTAGE_RES - 1);
61+
#endif
5562
}

src/Hall-Effect-Analog-SOLDERED.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,35 @@
1515

1616
#include "Arduino.h"
1717

18+
/**
19+
* Different boards use different ADC's with a different range and resolution
20+
*/
21+
#ifdef __AVR__
22+
23+
#define VOLTAGE_RES 5
24+
#define ADC_MAX 1024
25+
#define NUM_BITS 10
26+
27+
#elif ESP32
28+
29+
#define VOLTAGE_RES 3.3
30+
#define ADC_MAX 4096
31+
#define NUM_BITS 12
32+
33+
#elif ESP8266
34+
35+
#define VOLTAGE_RES 3.3
36+
#define ADC_MAX 1024
37+
#define NUM_BITS 10
38+
39+
#else
40+
41+
#define VOLTAGE_RES 5
42+
#define ADC_MAX 1024
43+
#define NUM_BITS 10
44+
45+
#endif
46+
1847
/**
1948
* @brief Base class Analog Hall Effect Sensor
2049
*

src/Hall-Effect-Analog-easyC-SOLDERED.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
*
1818
* @return Returns raw reading as a float
1919
*/
20-
int HallEffect_Analog_EasyC::getRawReading()
20+
uint16_t HallEffect_Analog_EasyC::getRawReading()
2121
{
2222
// Reading register will always return two bytes
23-
// Both must be read but we're only interested in the first one
24-
char raw[2];
25-
readRegister(ANALOG_READ_REG, raw, 2 * sizeof(uint8_t));
26-
uint16_t value = raw[0] | (raw[1]) << 8;
23+
uint8_t raw[2];
24+
readRegister(ANALOG_READ_REG, (char *)raw, 2);
25+
uint16_t value = ( (uint8_t)raw[1] << 8) | (uint8_t)raw[0];
2726
return value;
2827
}
2928

src/Hall-Effect-Analog-easyC-SOLDERED.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class HallEffect_Analog_EasyC : public EasyC
2424
{
2525
public:
2626
HallEffect_Analog_EasyC(){};
27-
int getRawReading();
27+
uint16_t getRawReading();
2828
float getMilliTeslas();
2929

3030
protected:

0 commit comments

Comments
 (0)