Teensy3 Serial value writing errors... ish?

Status
Not open for further replies.

Eascen

New member
Okay, so strange issue, I'm working on creating cell voltage monitoring which requires for this 12s pack requires 12 ADC samplings. Using the code I found where someone tested the ADC, I'm averaging 10k samples at 16 bit res (yes, I realize deviation can be large, but with what I'm doing accuracy is... not quite as necessary).

So, code setup goes as follows:

Code:
	long a1sum, a2sum, a3sum, a4sum, a5sum, a6sum, a7sum, a8sum, a9sum, a10sum, a11sum, a12sum;
	n++;
	a1sum += analogRead(35);
	a2sum += analogRead(34);
	a3sum += analogRead(23);
	a4sum += analogRead(22);
	a5sum += analogRead(21);
	a6sum += analogRead(20);
	a7sum += analogRead(19);
	a8sum += analogRead(18);
	a9sum += analogRead(17);
	a10sum += analogRead(16);
	a11sum += analogRead(15);
	a12sum += analogRead(14);
	} 
	long durT = millis() - oldT;
	
	Serial.print((1000.0*n/durT),2);

	Serial.print(" A0: ");     Serial.print(((1.0*a1sum))/n,2);
	Serial.print(" A1: ");     Serial.print(((1.0*a2sum))/n,2);
etc...

Now, all simple enough except the output:

Code:
6891.80 A0: 19187.26 A1: 24935.47 A2: 48268.20 A3: 44111.05 A4: 48012.56 A5: 47133.35 A6: 49325.83 A7: -72890.23 A8: -45976.47 A9: 11202.86 A10: -88842.77 A11: -183154.95
6887.05 A0: 19145.72 A1: 24987.55 A2: 48300.83 A3: 44115.11 A4: 48016.87 A5: 47164.73 A6: 49338.12 A7: -26631.12 A8: 1849.90 A9: 59244.38 A10: -43542.94 A11: -171842.68
6891.80 A0: 19267.16 A1: 25081.17 A2: 48303.55 A3: 44111.86 A4: 48013.50 A5: 47143.04 A6: 49331.36 A7: 19618.39 A8: 49657.29 A9: 107269.72 A10: 1757.90 A11: -160487.34
6887.05 A0: 19262.19 A1: 25072.95 A2: 48296.72 A3: 44105.51 A4: 48017.32 A5: 47152.05 A6: 49324.69 A7: 65843.45 A8: 97473.30 A9: 155287.98 A10: 47048.69 A11: -149139.74
6891.80 A0: 19279.68 A1: 25120.30 A2: 48285.80 A3: 44094.17 A4: 47984.47 A5: 47164.89 A6: 49334.77 A7: 112086.17 A8: 145291.96 A9: 203299.95 A10: 92337.32 A11: -137830.00
6887.05 A0: 19297.13 A1: 24994.40 A2: 48272.36 A3: 44118.03 A4: 48013.43 A5: 47148.95 A6: 49333.12 A7: 158349.18 A8: 193128.40 A9: -178148.28 A10: 137637.22 A11: -126497.


So, the first 6 are accurate, then it seems to go into this ... odd loop that would make me think somehow addresses are wrong. So, then I tried ruling out the ADC giving back bad values by adding:

Code:
	Serial.print(" "); Serial.print(a12sum/n); Serial.print( " " );

Which then left me with an actual accurate reading for A11, but then A6 started misbehaving:
Code:
6887.05 A0: 22161.88 A1: 28990.44 A2: 50307.50 A3: 46048.64 A4: 49742.89 A5: 49482.99 A6: 102591.18 A7: 97247.70 A8: 153806.56 A9: 100427.89 A10: 149287.09 13088  A11: 13088.95

Sad to say this is beyond my debugging capability. Any ideas?
 
And, the reply with a workaround... after reading about the teensy3 double type it occurred to me to try moving the variable declarations to program space outside of the loop() and simply resetting them to 0 in the loop and I get appropriate values:

Code:
long a1sum, a2sum, a3sum, a4sum, a5sum, a6sum, a7sum, a8sum, a9sum, a10sum, a11sum, a12sum;
void loop() {
	a1sum = 0;
	a2sum = 0;
...etc
And we get:
Code:
18518.52 A0: 19744.07 A1: 21608.60 A2: 45347.83 A3: 45950.60 A4: 48421.53 A5: 49921.37 A6: 51024.57 A7: 50025.00 A8: 50672.53 A9: 50976.15 A10: 49887.73 A11: 20387.37
as expected.

(Please note I don't have a0, a1, or a11 hooked to the pack at the moment due to a fried trace an forgetting two traces in the board design).
 
Status
Not open for further replies.
Back
Top