Components and supplies
Neodymium magnet, 6mm diameter x 2mm
Hall-effect magnetic sensor UGN3503
Arduino UNO
Tools and machines
Mastech MS8217 Autorange Digital Multimeter
Project description
Code
LInterp.h
h
Arduino C-compiler pre-processor script to generate interpolation / translation / lookup arrays in PROGMEM
1/* LInterp.h Linear interpolation array generator */ 2/* <<c) dxb 1982 - 2022 */ 3 4#if defined( LI_ELT ) /* generate array elt */ 5#if LI_LEV < LI_BND && !defined( LI_EOI ) 6 #if ( LI_ELT <= LI_S( LI_LEV, LI_NXT ) + ( LI_LEV == LI_BND - 1 ) ) 7 ,(LI_TYP) ( LI_RND + LI_OFS + LI_SCL * ( LI_LEV + 8 ( LI_ELT - LI_R( 0, LI_LEV ) + LI_P( 0, LI_LEV ) ) / 9 LI_R( LI_LEV, LI_NXT ) ) / LI_BND ) 10 #else 11 #define LI_EOI 12 #endif 13#endif 14#undef LI_ELT 15 16#elif defined( LI_LEV ) /* process intervals */ 17#define LI_ELT 1 18#include "LInterp.h" 19#define LI_ELT 2 20#include "LInterp.h" 21#define LI_ELT 3 22#include "LInterp.h" 23#define LI_ELT 4 24#include "LInterp.h" 25#define LI_ELT 5 26#include "LInterp.h" 27#define LI_ELT 6 28#include "LInterp.h" 29#define LI_ELT 7 30#include "LInterp.h" 31#define LI_ELT 8 32#include "LInterp.h" 33#define LI_ELT 9 34#include "LInterp.h" 35#define LI_ELT 10 36#include "LInterp.h" 37#define LI_ELT 11 38#include "LInterp.h" 39#define LI_ELT 12 40#include "LInterp.h" 41#define LI_ELT 13 42#include "LInterp.h" 43#define LI_ELT 14 44#include "LInterp.h" 45#define LI_ELT 15 46#include "LInterp.h" 47#define LI_ELT 16 48#include "LInterp.h" 49#define LI_ELT 17 50#include "LInterp.h" 51#define LI_ELT 18 52#include "LInterp.h" 53#define LI_ELT 19 54#include "LInterp.h" 55#define LI_ELT 20 56#include "LInterp.h" 57#define LI_ELT 21 58#include "LInterp.h" 59#define LI_ELT 22 60#include "LInterp.h" 61#define LI_ELT 23 62#include "LInterp.h" 63#define LI_ELT 24 64#include "LInterp.h" 65#define LI_ELT 25 66#include "LInterp.h" 67#define LI_ELT 26 68#include "LInterp.h" 69#define LI_ELT 27 70#include "LInterp.h" 71#define LI_ELT 28 72#include "LInterp.h" 73#define LI_ELT 29 74#include "LInterp.h" 75#define LI_ELT 30 76#include "LInterp.h" 77#define LI_ELT 31 78#include "LInterp.h" 79#define LI_ELT 32 /* <- repeat these lines for more */ 80#include "LInterp.h" /* <- interps, with inc elt idxs */ 81/* ... */ 82#if LI_LEV < LI_BND && !defined( LI_EOI ) 83#error LInterp: Too many interps in one interval. Add mapping pts or decrease LI_INT 84#endif 85#undef LI_EOI 86#undef LI_LEV 87#undef LI_NXT 88 89#else /* define array params */ 90#if !defined( LI_P0 ) || !defined( LI_P1 ) || !defined( LI_P2 ) || !defined( LI_P3 ) 91#error LIinterp: define at least 4 mapping values LI_P0 to LI_Pnn (nn < 32) 92#endif 93#if !defined( LI_TYP ) 94#define LI_TYP float /* default array type */ 95#define LI_RND 0 /* no rounding */ 96#else /* all integer types! */ 97#define LI_RND 0.5 /* for float-int convs */ 98#endif 99#if !defined( LI_ARR ) 100#define LI_ARR LInterp /* default array name */ 101#endif 102#if !defined ( LI_RNG ) /* input ordinate range */ 103#error LInterp: Define an ordinate mapping scale LI_RNG 104#endif 105#if !defined( LI_CAL ) /* array indexing scale */ 106#error LInterp: Define an array-base indexing scale LI_CAL 107#endif 108#if !defined( LI_INT ) 109#define LI_INT 1 /* interp interval */ 110#endif 111#if !defined( LI_SCL ) 112#define LI_SCL 1 /* array value scale */ 113#endif 114#if !defined( LI_OFS ) 115#define LI_OFS 0 /* array value offset */ 116#endif 117#if !defined( LI_VAR ) 118#define LI_VAR const /* array value offset */ 119#endif 120 121#if !defined( LI_P4 ) 122#define LI_BND 3 123#elif !defined( LI_P5 ) 124#define LI_BND 4 125#elif !defined( LI_P6 ) 126#define LI_BND 5 127#elif !defined( LI_P7 ) 128#define LI_BND 6 129#elif !defined( LI_P8 ) 130#define LI_BND 7 131#elif !defined( LI_P9 ) 132#define LI_BND 8 133#elif !defined( LI_P10 ) 134#define LI_BND 9 135#elif !defined( LI_P11 ) 136#define LI_BND 10 137#elif !defined( LI_P12 ) 138#define LI_BND 11 139#elif !defined( LI_P13 ) 140#define LI_BND 12 141#elif !defined( LI_P14 ) 142#define LI_BND 13 143#elif !defined( LI_P15 ) 144#define LI_BND 14 145#elif !defined( LI_P16 ) 146#define LI_BND 15 147#elif !defined( LI_P17 ) 148#define LI_BND 16 149#elif !defined( LI_P18 ) 150#define LI_BND 17 151#elif !defined( LI_P19 ) 152#define LI_BND 18 153#elif !defined( LI_P20 ) 154#define LI_BND 19 155#elif !defined( LI_P21 ) 156#define LI_BND 20 157#elif !defined( LI_P22 ) 158#define LI_BND 21 159#elif !defined( LI_P23 ) 160#define LI_BND 22 161#elif !defined( LI_P24 ) 162#define LI_BND 23 163#elif !defined( LI_P25 ) 164#define LI_BND 24 165#elif !defined( LI_P26 ) 166#define LI_BND 25 167#elif !defined( LI_P27 ) 168#define LI_BND 26 169#elif !defined( LI_P28 ) 170#define LI_BND 27 171#elif !defined( LI_P29 ) 172#define LI_BND 28 173#elif !defined( LI_P30 ) 174#define LI_BND 29 175#elif !defined( LI_P31 ) 176#define LI_BND 30 177/* repeat following defn block for more mapping points */ 178/* increasing iterators as per sequence above */ 179/* also add matching interval inits at end of file */ 180#elif !defined( LI_P32 ) 181#define LI_BND 31 182/* ... */ 183#else 184#error LInterp: Too many mapping points defined. Add extra point defs to LInterp.h 185#endif 186 187#define LI_V( i ) ( LI_P##i ) /* compile pass */ 188#define LI_Q( i ) ( LI_V( i ) * 1L * LI_CAL ) 189#define LI_P( i, j ) ( ( LI_Q( j ) - LI_Q( i ) ) / ( 1L * LI_INT * LI_RNG ) ) 190#define LI_R( i, j ) ( (float) ( LI_Q( j ) - LI_Q( i ) ) / ( LI_INT * LI_RNG ) ) 191#define LI_S( i, j ) ( LI_P( 0, j ) - LI_P( 0, i ) ) 192 193#if defined( PROGMEM ) && !defined( LI_RAM ) 194const LI_TYP LI_ARR[] PROGMEM = { LI_OFS 195#else 196LI_VAR LI_TYP LI_ARR[] = { LI_OFS 197#endif 198#define LI_LEV 0 199#define LI_NXT 1 200#include "LInterp.h" 201#define LI_LEV 1 202#define LI_NXT 2 203#include "LInterp.h" 204#undef LI_P1 205#define LI_LEV 2 206#define LI_NXT 3 207#include "LInterp.h" 208#undef LI_P2 209#define LI_LEV 3 210#define LI_NXT 4 211#include "LInterp.h" 212#undef LI_P3 213#define LI_LEV 4 214#define LI_NXT 5 215#include "LInterp.h" 216#undef LI_P4 217#define LI_LEV 5 218#define LI_NXT 6 219#include "LInterp.h" 220#undef LI_P5 221#define LI_LEV 6 222#define LI_NXT 7 223#include "LInterp.h" 224#undef LI_P6 225#define LI_LEV 7 226#define LI_NXT 8 227#include "LInterp.h" 228#undef LI_P7 229#define LI_LEV 8 230#define LI_NXT 9 231#include "LInterp.h" 232#undef LI_P8 233#define LI_LEV 9 234#define LI_NXT 10 235#include "LInterp.h" 236#undef LI_P9 237#define LI_LEV 10 238#define LI_NXT 11 239#include "LInterp.h" 240#undef LI_P10 241#define LI_LEV 11 242#define LI_NXT 12 243#include "LInterp.h" 244#undef LI_P11 245#define LI_LEV 12 246#define LI_NXT 13 247#include "LInterp.h" 248#undef LI_P12 249#define LI_LEV 13 250#define LI_NXT 14 251#include "LInterp.h" 252#undef LI_P13 253#define LI_LEV 14 254#define LI_NXT 15 255#include "LInterp.h" 256#undef LI_P14 257#define LI_LEV 15 258#define LI_NXT 16 259#include "LInterp.h" 260#undef LI_P15 261#define LI_LEV 16 262#define LI_NXT 17 263#include "LInterp.h" 264#undef LI_P16 265#define LI_LEV 17 266#define LI_NXT 18 267#include "LInterp.h" 268#undef LI_P17 269#define LI_LEV 18 270#define LI_NXT 19 271#include "LInterp.h" 272#undef LI_P18 273#define LI_LEV 19 274#define LI_NXT 20 275#include "LInterp.h" 276#undef LI_P19 277#define LI_LEV 20 278#define LI_NXT 21 279#include "LInterp.h" 280#undef LI_P20 281#define LI_LEV 21 282#define LI_NXT 22 283#include "LInterp.h" 284#undef LI_P21 285#define LI_LEV 22 286#define LI_NXT 23 287#include "LInterp.h" 288#undef LI_P22 289#define LI_LEV 23 290#define LI_NXT 24 291#include "LInterp.h" 292#undef LI_P23 293#define LI_LEV 24 294#define LI_NXT 25 295#include "LInterp.h" 296#undef LI_P24 297#define LI_LEV 25 298#define LI_NXT 26 299#include "LInterp.h" 300#undef LI_P25 301#define LI_LEV 26 302#define LI_NXT 27 303#include "LInterp.h" 304#undef LI_P26 305#define LI_LEV 27 306#define LI_NXT 28 307#include "LInterp.h" 308#undef LI_P27 309#define LI_LEV 28 310#define LI_NXT 29 311#include "LInterp.h" 312#undef LI_P28 313#define LI_LEV 29 314#define LI_NXT 30 315#include "LInterp.h" 316#undef LI_P29 317#define LI_LEV 30 /* <- repeat this set of defs */ 318#define LI_NXT 31 /* <- with increasing indices */ 319#include "LInterp.h" /* <- for further map defn pts */ 320#undef LI_P30 /* <- */ 321/* ... */ 322#undef LI_P31 /* change to match LI_BND defn */ 323}; 324 325#undef LI_P0 326#undef LI_ARR 327#undef LI_SCL 328#undef LI_TYP 329#undef LI_RND 330#undef LI_RNG 331#undef LI_CAL 332#undef LI_INT 333#undef LI_SCL 334#undef LI_OFS 335#undef LI_BND 336#undef LI_RAM 337#undef LI_VAR 338#undef LI_V 339#undef LI_Q 340#undef LI_P 341#undef LI_R 342#undef LI_S 343#undef LI_N 344#undef LI_W 345#endif 346 347/* EOF LInterp.h */ 348
LInMagSens.ino
arduino
Demo Arduino script for LInMagSens.h - A linear virtualising function for a non-linear magnetic position sensor
1/* Linearised magnetic position sensor example */ 2/* using LInterp PROGMEM array generator on */ 3/* (any) Arduino board <<c) dxb 2022 */ 4#include "LInMagSens.h" /* device virtualiser */ 5 6void setup() { 7Serial.begin( 9600 ); 8} 9 10void loop() { 11Serial.println( Position(), 1 ); 12delay( 500 ); 13} 14
LInMagSens.ino
arduino
Demo Arduino script for LInMagSens.h - A linear virtualising function for a non-linear magnetic position sensor
1/* Linearised magnetic position sensor example */ 2/* using LInterp PROGMEM array generator on */ 3/* (any) Arduino board <<c) dxb 2022 */ 4#include "LInMagSens.h" /* device virtualiser */ 5 6void setup() { 7Serial.begin( 9600 ); 8} 9 10void loop() { 11Serial.println( Position(), 1 ); 12delay( 500 ); 13} 14
LInterp.h
h
Arduino C-compiler pre-processor script to generate interpolation / translation / lookup arrays in PROGMEM
1/* LInterp.h Linear interpolation array generator */ 2/* <<c) dxb 1982 - 2022 */ 3 4#if defined( LI_ELT ) /* generate array elt */ 5#if LI_LEV < LI_BND && !defined( LI_EOI ) 6 #if ( LI_ELT <= LI_S( LI_LEV, LI_NXT ) + ( LI_LEV == LI_BND - 1 ) ) 7 ,(LI_TYP) ( LI_RND + LI_OFS + LI_SCL * ( LI_LEV + 8 ( LI_ELT - LI_R( 0, LI_LEV ) + LI_P( 0, LI_LEV ) ) / 9 LI_R( LI_LEV, LI_NXT ) ) / LI_BND ) 10 #else 11 #define LI_EOI 12 #endif 13#endif 14#undef LI_ELT 15 16#elif defined( LI_LEV ) /* process intervals */ 17#define LI_ELT 1 18#include "LInterp.h" 19#define LI_ELT 2 20#include "LInterp.h" 21#define LI_ELT 3 22#include "LInterp.h" 23#define LI_ELT 4 24#include "LInterp.h" 25#define LI_ELT 5 26#include "LInterp.h" 27#define LI_ELT 6 28#include "LInterp.h" 29#define LI_ELT 7 30#include "LInterp.h" 31#define LI_ELT 8 32#include "LInterp.h" 33#define LI_ELT 9 34#include "LInterp.h" 35#define LI_ELT 10 36#include "LInterp.h" 37#define LI_ELT 11 38#include "LInterp.h" 39#define LI_ELT 12 40#include "LInterp.h" 41#define LI_ELT 13 42#include "LInterp.h" 43#define LI_ELT 14 44#include "LInterp.h" 45#define LI_ELT 15 46#include "LInterp.h" 47#define LI_ELT 16 48#include "LInterp.h" 49#define LI_ELT 17 50#include "LInterp.h" 51#define LI_ELT 18 52#include "LInterp.h" 53#define LI_ELT 19 54#include "LInterp.h" 55#define LI_ELT 20 56#include "LInterp.h" 57#define LI_ELT 21 58#include "LInterp.h" 59#define LI_ELT 22 60#include "LInterp.h" 61#define LI_ELT 23 62#include "LInterp.h" 63#define LI_ELT 24 64#include "LInterp.h" 65#define LI_ELT 25 66#include "LInterp.h" 67#define LI_ELT 26 68#include "LInterp.h" 69#define LI_ELT 27 70#include "LInterp.h" 71#define LI_ELT 28 72#include "LInterp.h" 73#define LI_ELT 29 74#include "LInterp.h" 75#define LI_ELT 30 76#include "LInterp.h" 77#define LI_ELT 31 78#include "LInterp.h" 79#define LI_ELT 32 /* <- repeat these lines for more */ 80#include "LInterp.h" /* <- interps, with inc elt idxs */ 81/* ... */ 82#if LI_LEV < LI_BND && !defined( LI_EOI ) 83#error LInterp: Too many interps in one interval. Add mapping pts or decrease LI_INT 84#endif 85#undef LI_EOI 86#undef LI_LEV 87#undef LI_NXT 88 89#else /* define array params */ 90#if !defined( LI_P0 ) || !defined( LI_P1 ) || !defined( LI_P2 ) || !defined( LI_P3 ) 91#error LIinterp: define at least 4 mapping values LI_P0 to LI_Pnn (nn < 32) 92#endif 93#if !defined( LI_TYP ) 94#define LI_TYP float /* default array type */ 95#define LI_RND 0 /* no rounding */ 96#else /* all integer types! */ 97#define LI_RND 0.5 /* for float-int convs */ 98#endif 99#if !defined( LI_ARR ) 100#define LI_ARR LInterp /* default array name */ 101#endif 102#if !defined ( LI_RNG ) /* input ordinate range */ 103#error LInterp: Define an ordinate mapping scale LI_RNG 104#endif 105#if !defined( LI_CAL ) /* array indexing scale */ 106#error LInterp: Define an array-base indexing scale LI_CAL 107#endif 108#if !defined( LI_INT ) 109#define LI_INT 1 /* interp interval */ 110#endif 111#if !defined( LI_SCL ) 112#define LI_SCL 1 /* array value scale */ 113#endif 114#if !defined( LI_OFS ) 115#define LI_OFS 0 /* array value offset */ 116#endif 117#if !defined( LI_VAR ) 118#define LI_VAR const /* array value offset */ 119#endif 120 121#if !defined( LI_P4 ) 122#define LI_BND 3 123#elif !defined( LI_P5 ) 124#define LI_BND 4 125#elif !defined( LI_P6 ) 126#define LI_BND 5 127#elif !defined( LI_P7 ) 128#define LI_BND 6 129#elif !defined( LI_P8 ) 130#define LI_BND 7 131#elif !defined( LI_P9 ) 132#define LI_BND 8 133#elif !defined( LI_P10 ) 134#define LI_BND 9 135#elif !defined( LI_P11 ) 136#define LI_BND 10 137#elif !defined( LI_P12 ) 138#define LI_BND 11 139#elif !defined( LI_P13 ) 140#define LI_BND 12 141#elif !defined( LI_P14 ) 142#define LI_BND 13 143#elif !defined( LI_P15 ) 144#define LI_BND 14 145#elif !defined( LI_P16 ) 146#define LI_BND 15 147#elif !defined( LI_P17 ) 148#define LI_BND 16 149#elif !defined( LI_P18 ) 150#define LI_BND 17 151#elif !defined( LI_P19 ) 152#define LI_BND 18 153#elif !defined( LI_P20 ) 154#define LI_BND 19 155#elif !defined( LI_P21 ) 156#define LI_BND 20 157#elif !defined( LI_P22 ) 158#define LI_BND 21 159#elif !defined( LI_P23 ) 160#define LI_BND 22 161#elif !defined( LI_P24 ) 162#define LI_BND 23 163#elif !defined( LI_P25 ) 164#define LI_BND 24 165#elif !defined( LI_P26 ) 166#define LI_BND 25 167#elif !defined( LI_P27 ) 168#define LI_BND 26 169#elif !defined( LI_P28 ) 170#define LI_BND 27 171#elif !defined( LI_P29 ) 172#define LI_BND 28 173#elif !defined( LI_P30 ) 174#define LI_BND 29 175#elif !defined( LI_P31 ) 176#define LI_BND 30 177/* repeat following defn block for more mapping points */ 178/* increasing iterators as per sequence above */ 179/* also add matching interval inits at end of file */ 180#elif !defined( LI_P32 ) 181#define LI_BND 31 182/* ... */ 183#else 184#error LInterp: Too many mapping points defined. Add extra point defs to LInterp.h 185#endif 186 187#define LI_V( i ) ( LI_P##i ) /* compile pass */ 188#define LI_Q( i ) ( LI_V( i ) * 1L * LI_CAL ) 189#define LI_P( i, j ) ( ( LI_Q( j ) - LI_Q( i ) ) / ( 1L * LI_INT * LI_RNG ) ) 190#define LI_R( i, j ) ( (float) ( LI_Q( j ) - LI_Q( i ) ) / ( LI_INT * LI_RNG ) ) 191#define LI_S( i, j ) ( LI_P( 0, j ) - LI_P( 0, i ) ) 192 193#if defined( PROGMEM ) && !defined( LI_RAM ) 194const LI_TYP LI_ARR[] PROGMEM = { LI_OFS 195#else 196LI_VAR LI_TYP LI_ARR[] = { LI_OFS 197#endif 198#define LI_LEV 0 199#define LI_NXT 1 200#include "LInterp.h" 201#define LI_LEV 1 202#define LI_NXT 2 203#include "LInterp.h" 204#undef LI_P1 205#define LI_LEV 2 206#define LI_NXT 3 207#include "LInterp.h" 208#undef LI_P2 209#define LI_LEV 3 210#define LI_NXT 4 211#include "LInterp.h" 212#undef LI_P3 213#define LI_LEV 4 214#define LI_NXT 5 215#include "LInterp.h" 216#undef LI_P4 217#define LI_LEV 5 218#define LI_NXT 6 219#include "LInterp.h" 220#undef LI_P5 221#define LI_LEV 6 222#define LI_NXT 7 223#include "LInterp.h" 224#undef LI_P6 225#define LI_LEV 7 226#define LI_NXT 8 227#include "LInterp.h" 228#undef LI_P7 229#define LI_LEV 8 230#define LI_NXT 9 231#include "LInterp.h" 232#undef LI_P8 233#define LI_LEV 9 234#define LI_NXT 10 235#include "LInterp.h" 236#undef LI_P9 237#define LI_LEV 10 238#define LI_NXT 11 239#include "LInterp.h" 240#undef LI_P10 241#define LI_LEV 11 242#define LI_NXT 12 243#include "LInterp.h" 244#undef LI_P11 245#define LI_LEV 12 246#define LI_NXT 13 247#include "LInterp.h" 248#undef LI_P12 249#define LI_LEV 13 250#define LI_NXT 14 251#include "LInterp.h" 252#undef LI_P13 253#define LI_LEV 14 254#define LI_NXT 15 255#include "LInterp.h" 256#undef LI_P14 257#define LI_LEV 15 258#define LI_NXT 16 259#include "LInterp.h" 260#undef LI_P15 261#define LI_LEV 16 262#define LI_NXT 17 263#include "LInterp.h" 264#undef LI_P16 265#define LI_LEV 17 266#define LI_NXT 18 267#include "LInterp.h" 268#undef LI_P17 269#define LI_LEV 18 270#define LI_NXT 19 271#include "LInterp.h" 272#undef LI_P18 273#define LI_LEV 19 274#define LI_NXT 20 275#include "LInterp.h" 276#undef LI_P19 277#define LI_LEV 20 278#define LI_NXT 21 279#include "LInterp.h" 280#undef LI_P20 281#define LI_LEV 21 282#define LI_NXT 22 283#include "LInterp.h" 284#undef LI_P21 285#define LI_LEV 22 286#define LI_NXT 23 287#include "LInterp.h" 288#undef LI_P22 289#define LI_LEV 23 290#define LI_NXT 24 291#include "LInterp.h" 292#undef LI_P23 293#define LI_LEV 24 294#define LI_NXT 25 295#include "LInterp.h" 296#undef LI_P24 297#define LI_LEV 25 298#define LI_NXT 26 299#include "LInterp.h" 300#undef LI_P25 301#define LI_LEV 26 302#define LI_NXT 27 303#include "LInterp.h" 304#undef LI_P26 305#define LI_LEV 27 306#define LI_NXT 28 307#include "LInterp.h" 308#undef LI_P27 309#define LI_LEV 28 310#define LI_NXT 29 311#include "LInterp.h" 312#undef LI_P28 313#define LI_LEV 29 314#define LI_NXT 30 315#include "LInterp.h" 316#undef LI_P29 317#define LI_LEV 30 /* <- repeat this set of defs */ 318#define LI_NXT 31 /* <- with increasing indices */ 319#include "LInterp.h" /* <- for further map defn pts */ 320#undef LI_P30 /* <- */ 321/* ... */ 322#undef LI_P31 /* change to match LI_BND defn */ 323}; 324 325#undef LI_P0 326#undef LI_ARR 327#undef LI_SCL 328#undef LI_TYP 329#undef LI_RND 330#undef LI_RNG 331#undef LI_CAL 332#undef LI_INT 333#undef LI_SCL 334#undef LI_OFS 335#undef LI_BND 336#undef LI_RAM 337#undef LI_VAR 338#undef LI_V 339#undef LI_Q 340#undef LI_P 341#undef LI_R 342#undef LI_S 343#undef LI_N 344#undef LI_W 345#endif 346 347/* EOF LInterp.h */ 348
LInMagSens.h
h
LInterp function header to virtualise a non-linear magnetic position sensor as a linear function call
1/* LInMagSens.h <<c) dxb 2022 */ 2#include <avr/pgmspace.h> /* declare array in PROGMEM */ 3 4/* define our particular Arduino card ADC specs: */ 5#define MAG_SENS_CHAN A0 /* (analog channel) */ 6#define ANALOG_RNG 5000 /* (mV) NOMINAL! */ 7#define ADC_LEV_RNG 1024 /* (ADC levels) */ 8 9/* analog device INPUT range definitions */ 10#define MAG_SENS_IN_MIN 2600L /* (mV) */ 11#define MAG_SENS_IN_MAX 4210L /* (mV) */ 12 13/* Analog device OUTPUT range definitions */ 14#define MAG_SENS_OUT_MIN 0.0 /* (degrees) */ 15#define MAG_SENS_OUT_MAX 12.0 /* (degrees) */ 16#define MAG_SENS_OUT_SCL 1.0 /* (degrees) */ 17 18/* retained LInterp array definitions */ 19#define MAG_SENS_INTERP 4 /* (ADC levels) */ 20 21/* temporary LInterp array definitions */ 22#define LI_ARR MAG_SENS_LI_ARR /* device array name */ 23#define LI_CAL ADC_LEV_RNG 24#define LI_RNG ANALOG_RNG 25#define LI_SCL ( MAG_SENS_OUT_MAX - MAG_SENS_OUT_MIN ) 26#define LI_OFS MAG_SENS_OUT_MIN 27#define LI_INT MAG_SENS_INTERP 28 29/* device ordinate map */ 30#define LI_P0 2620 /* (mV) */ 31#define LI_P1 2640 32#define LI_P2 2670 33#define LI_P3 2720 34#define LI_P4 2740 35#define LI_P5 2900 36#define LI_P6 3060 37#define LI_P7 3600 38#define LI_P8 3790 39#define LI_P9 3950 40#define LI_P10 4090 41#define LI_P11 4180 42#define LI_P12 4210 43 44#include "LInterp.h" /* declare and init array */ 45 46/* Array read macros */ 47#define MAG_SENS_ARRAY( i ) pgm_read_float( MAG_SENS_LI_ARR + i ) 48 49/* Linearise supplied device output */ 50float MAG_SENS_Linear( long level ) { 51float interp_start, interp_end; 52long dev_mV, index; 53dev_mV = level * ANALOG_RNG / ADC_LEV_RNG; 54if ( dev_mV < MAG_SENS_IN_MIN ) /* bottom end of range */ 55 level = MAG_SENS_IN_MIN * ADC_LEV_RNG / ANALOG_RNG; 56else if ( dev_mV >= MAG_SENS_IN_MAX ) /* top end of range */ 57 level = MAG_SENS_IN_MAX * ADC_LEV_RNG / ANALOG_RNG; 58level -= (long) MAG_SENS_IN_MIN * ADC_LEV_RNG / ANALOG_RNG; 59index = level / MAG_SENS_INTERP; 60interp_start = MAG_SENS_ARRAY( index ); 61interp_end = MAG_SENS_ARRAY( index + 1 ); 62return ( interp_start + ( interp_end - interp_start ) * 63 ( level % MAG_SENS_INTERP ) / MAG_SENS_INTERP ); 64} 65 66/* read analog device and linearise output */ 67float MAG_SENS_Read() { 68return( MAG_SENS_Linear( analogRead( MAG_SENS_CHAN ) ) ); 69} 70 71/* read averaged analog device and linearise output */ 72float MAG_SENS_ReadAvg() { 73int samps; 74long ADClevel = 0; 75/* take an average of several analog channel readings */ 76for ( samps = 0; samps < 20; samps++ ) { 77 ADClevel += analogRead( MAG_SENS_CHAN ); 78 delay( 5 ); 79 } 80return ( MAG_SENS_Linear( ADClevel / samps ) ); 81} 82 83/* virtalise selected device function */ 84#define Position MAG_SENS_ReadAvg 85
Downloadable files
UGN3503 Hall-effect magnetic flux sensor
A magnetic flux sensor connected to an Arduino board analog input
UGN3503 Hall-effect magnetic flux sensor
UGN3503 Hall-effect magnetic flux sensor
A magnetic flux sensor connected to an Arduino board analog input
UGN3503 Hall-effect magnetic flux sensor
Comments
Only logged in users can leave comments