Created
February 5, 2021 22:16
-
-
Save bagdonas/502554a95d4e880b4a28ccc898bdd07f to your computer and use it in GitHub Desktop.
Test hall effect sensor (like A1324LUA-T). Attempt to make reading linear (based on distance)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <math.h> | |
| const float POW_CBRT = 1.0f / 3.0f; | |
| const float R_MAX = pow(0.01f, POW_CBRT); | |
| float calcR(int value) { | |
| // I = r^-3 | |
| // r = cbrt(1 / I) | |
| float I = (value / 1023.0f - 0.5f) * 2.0f; // [-1..1] | |
| float r = pow(abs(I), -POW_CBRT); | |
| return R_MAX / max(r - 1.0f, R_MAX); | |
| } | |
| void setup() { | |
| pinMode(A0, INPUT); | |
| } | |
| void loop() { | |
| int vA0 = analogRead(A0); | |
| float rA0 = calcR(vA0); | |
| Serial.println(rA0); | |
| delay(10); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment