1+ //
2+ // FILE: multimap_NTC_int_FAIL.ino
3+ // AUTHOR: Rob Tillaart
4+ // VERSION: 0.1.0
5+ // PURPOSE: demo of faulty optimizing
6+ // DATE: 2020-04-09
7+ // (c) : MIT
8+ //
9+
10+ // NOTE:
11+ // use integers instead of floats to minimize RAM. uses ~320 bytes PROGMEM ~120 bytes RAM less on UNO than float version
12+ //
13+ // this example is added to show how to reduce memory but also how it can FAIL due to math overflow
14+ // E.g. see around 196-200; 340-400
15+ // to prevent this one must have more values which increases the memory usage again.
16+ //
17+
18+
19+ #include " multimap.h"
20+
21+ uint32_t start;
22+ uint32_t stop;
23+
24+ volatile float x, y, z;
25+
26+ int in[] = {
27+ 0 , 1 , 3 , 8 , 13 , 20 , 25 , 32 , 50 , 60 , 72 , 85 , 100 , 145 , 200 , 250 , 300 , 400 , 500 , 600 , 650 , 700 , 753 , 800 , 830 , 870 , 900 , 936 , 964 , 985 , 1000 , 1017 , 1023
28+ };
29+ int out[] = {
30+ -27315 , -7165 , -6069 , -4981 , -4397 , -3850 , -3554 , -3216 , -2572 , -2295 , -2008 , -1737 , -1462 , -790 , -143 , 357 , 808 , 1634 , 2430 , 3264 ,
31+ 3717 , 4213 , 4805 , 5419 , 5875 , 6603 , 7287 , 8385 , 9651 , 11146 , 12949 , 18282 , 30182
32+ };
33+
34+ int sz = 33 ;
35+
36+ void setup ()
37+ {
38+ Serial.begin (115200 );
39+ Serial.println (__FILE__);
40+ Serial.println ();
41+ delay (10 ); // make sure print has ended
42+
43+ start = micros ();
44+ x = val (z);
45+ stop = micros ();
46+ Serial.println (stop - start);
47+ delay (10 ); // make sure print has ended
48+
49+ start = micros ();
50+ x = 0.01 * multiMap<int >(z, in, out, sz);
51+ stop = micros ();
52+ Serial.println (stop - start);
53+ delay (10 ); // make sure print has ended
54+
55+ for (int i = 0 ; i < 1024 ; i++)
56+ {
57+ x = val (i);
58+ y = 0.01 * multiMap<int >(i, in, out, sz);
59+ z = abs (x - y);
60+
61+ Serial.print (i);
62+ Serial.print (' \t ' );
63+ Serial.print (x);
64+ Serial.print (' \t ' );
65+ Serial.print (y);
66+ Serial.print (' \t ' );
67+ Serial.print (z);
68+ Serial.println ();
69+ }
70+
71+ }
72+
73+ void loop ()
74+ {
75+ }
76+
77+ // NTC formula
78+ float val (int sensorValueA1)
79+ {
80+ int R10k_ntc = 9870 ;
81+ float U10k_ntc = sensorValueA1 * (5.0 / 1024.0 );
82+ float Untc = 5.0 - U10k_ntc;
83+ float Rntc = (R10k_ntc * Untc) / U10k_ntc;
84+ float Temp = (298.15 / (1 - (298.15 / 4300.0 ) * log (10000.0 / Rntc))) - (273.15 + 0 );
85+
86+ return Temp;
87+ }
88+
89+ // -- END OF FILE --
0 commit comments