Non Linear output from ADC, Program haywire.

Hi, I am working on a program containing an array of 1023 values (one value per ADC output). I have constructed the array. The ADC is fed by a potentiometer of value 10k.

const int unAngleValue[] =  { // Contains all the values, not copying it here}
int unAnglePointer, unAnalogValue = 0;
int unStore;
// float unAngleActual, unMultipler = 12.08;

void setup()
{
  Serial.begin(9600);
  pinMode(9, OUTPUT); 
}

void loop()
{
  unAnalogValue = analogRead(A0);
  unAnglePointer = unAnalogValue;
  Serial.println(unAnglePointer);
  unStore = unAngleValue[unAnglePointer];
  delay(1000);
}

Till the time I do not write "unStore = unAngleValue[unAnglePointer];" instruction, everything runs smoothly.

After I write that line of code, code size suddenly increases by a rough 2kB, output of the ADC becomes non linear, and there's no output on the serial monitor (or there are garbage values). I have omitted the serial print lines of code here.

Please help, what am I not doing right?

Are there enough values in the array
const int unAngleValue = {
to account for unAnglePointer, which could go up to 1023?

Looks like a straithtforward enough lookup array to me.

Yes, there are exactly 1024 values (0 to 1023) for unAnglePointer. They are distributed as 32 rows of 32 elements each for ease of reading.

const int unAngleValue[] =  
// Line 1
{   0, 176, 250, 307, 355, 398, 436, 471, 504, 535, 564, 592, 619, 644, 669, 693, 716, 738, 759, 780, 801, 821, 840, 860, 878, 897, 915, 932, 950, 967, 983, 1000, 
// Line 2
    1016, 1032, 1048, 1063, 1079, 1094, 1109, 1123, 1138, 1152, 1166, 1181, 1194, 1208, 1222, 1235, 1249, 1262, 1275, 1288, 1301, 1313, 1326, 1338, 1351, 1363, 1375, 1387, 1399, 1411, 1423, 1435, 
// Line 3
    1446, 1458, 1469, 1480, 1492, 1503, 1514, 1525, 1536, 1547, 1558, 1569, 1579, 1590, 1601, 1611, 1622, 1632, 1642, 1653, 1663, 1673, 1683, 1693, 1703, 1713, 1723, 1733, 1743, 1753, 1762, 1772,
// Line 4
    1782, 1791, 1801, 1810, 1820, 1829, 1839, 1848, 1857, 1867, 1876, 1885, 1894, 1903, 1912, 1921, 1930, 1939, 1948, 1957, 1966, 1975, 1984, 1992, 2001, 2010, 2018, 2027, 2036, 2044, 2053, 2061,
// Line 5
    2070, 2078, 2087, 2095, 2103, 2112, 2120, 2128, 2137, 2145, 2153, 2161, 2169, 2178, 2186, 2194, 2202, 2210, 2218, 2226, 2234, 2242, 2250, 2258, 2266, 2273, 2271, 2289, 2297, 2305, 2312, 2320,
// Line 6
    2328, 2336, 2343, 2341, 2359, 2366, 2374, 2381, 2389, 2397, 2404, 2412, 2419, 2427, 2434, 2442, 2449, 2456, 2464, 2471, 2479, 2486, 2493, 2501, 2508, 2515, 2522, 2530, 2537, 2544, 2551, 2559, 
// Line 7
    2566, 2573, 2580, 2587, 2594, 2601, 2609, 2616, 2623, 2630, 2637, 2644, 2651, 2658, 2665, 2672, 2679, 2686, 2693, 2700, 2707, 2714, 2720, 2727, 2734, 2741, 2748, 2755, 2762, 2768, 2775, 2782,
// Line 8
    2789, 2796, 2802, 2809, 2816, 2822, 2829, 2836, 2843, 2849, 2856, 2863, 2869, 2876, 2883, 2889, 2896, 2902, 2909, 2916, 2922, 2929, 2935, 2942, 2948, 2955, 2962, 2968, 2975, 2981, 2988, 2994,
// Line 9
    3000, 3007, 3013, 3020, 3026, 3033, 3039, 3046, 3052, 3058, 3065, 3071, 3078, 3084, 3090, 3097, 3103, 3109, 3116, 3122, 3128, 3135, 3141, 3147, 3153, 3160, 3166, 3172, 3179, 3185, 3191, 3197, 
// Line 10
    3204, 3210, 3216, 3222, 3228, 3235, 3240, 3247, 3253, 3259, 3266, 3272, 3278, 3284, 3290, 3296, 3302, 3309, 3315, 3321, 3327, 3333, 3339, 3345, 3351, 3357, 3363, 3370, 3376, 3382, 3388, 3394, 
// Line 11
    3400, 3406, 3412, 3418, 3424, 3430, 3436, 3442, 3448, 3454, 3460, 3466, 3472, 3478, 3484, 3490, 3496, 3502, 3508, 3514, 3520, 3526, 3532, 3538, 3544, 3549, 3555, 3561, 3567, 3573, 3579, 3585,
// Line 12
    3591, 3597, 3603, 3609, 3614, 3620, 3626, 3632, 3638, 3644, 3650, 3655, 3661, 3667, 3673, 3679, 3685, 3691, 3696, 3702, 3708, 3714, 3720, 3726, 3731, 3737, 3743, 3749, 3755, 3760, 3766, 3772,
// Line 13
    3778, 3784, 3789, 3795, 3801, 3807, 3812, 3818, 3824, 3830, 3835, 3841, 3847, 3853, 3858, 3864, 3870, 3876, 3881, 3887, 3893, 3899, 3904, 3910, 3916, 3922, 3927, 3933, 3939, 3944, 3950, 3956,
// Line 14
    3962, 3967, 3973, 3979, 2884, 3990, 3996, 4001, 4001, 4013, 4019, 4024, 4030, 4036, 4041, 4047, 4053, 4058, 4064, 4070, 4075, 4081, 4087, 4092, 4098, 4104, 4109, 4115, 4121, 4126, 4132, 4138,
// Line 15
    4143, 4149, 4154, 4160, 4166, 4171, 4177, 4183, 4188, 4194, 4200, 4205, 4211, 4216, 4222, 4228, 4233, 4239, 4245, 4250, 4256, 4262, 4267, 4273, 4278, 4284, 4290, 4295, 4301, 4306, 4312, 4318, 
// Line 16
    4323, 4329, 4335, 4343, 4346, 4351, 4357, 4363, 4368, 4374, 4379, 4385, 4391, 4396, 4402, 4408, 4413, 4419, 4424, 4430, 4436, 4441, 4447, 4452, 4458, 4464, 4469, 4475, 4480, 4486, 4492, 4497,
// Line 17
    4503, 4508, 4514, 4520, 4525, 4591, 4536, 4542, 4548, 4553, 4559, 4564, 4570, 4576, 4581, 4587, 4592, 4598, 4604, 4609, 4615, 4621, 4626, 4692, 4637, 4643, 4649, 4654, 4660, 4665, 4671, 4677, 
// Line 18
    4682, 4688, 4694, 4699, 4705, 4710, 4716, 4722, 4727, 4733, 4738, 4744, 4750, 4755, 4761, 4767, 4772, 4778, 4784, 4789, 4795, 4800, 4806, 4812, 4817, 4823, 4829, 4834, 4840, 4846, 4851, 4857,
// Line 19
    4862, 4868, 4874, 4879, 4885, 4891, 4896, 4902, 4908, 4913, 4919, 4925, 4930, 4936, 4942, 4947, 4953, 4959, 4964, 4970, 4976, 4981, 4987, 4993, 4999, 5004, 5010, 5016, 5021, 5027, 5033, 5038, 
// Line 20
    5044, 5050, 5056, 5061, 5067, 5073, 5078, 5084, 5090, 5096, 5101, 5107, 5113, 5119, 5124, 5130, 5136, 5142, 5147, 5153, 5159, 5165, 5170, 5176, 5182, 5188, 5193, 5199, 5205, 5211, 5216, 5222, 
// Line 21
    5228, 5234, 5240, 5245, 5251, 5257, 5263, 5269, 5274, 5280, 5286, 5292, 5298, 5304, 5309, 5315, 5321, 5327, 5333, 5339, 5345, 5350, 5356, 5362, 5368, 5374, 5380, 5386, 5391, 5397, 5403, 5409, 
// Line 22
    5415, 5421, 5427, 5433, 5439, 5445, 5451, 5456, 5462, 5468, 5474, 5480, 5486, 5492, 5498, 5504, 5510, 5516, 5522, 5528, 5534, 5540, 5546, 5552, 5558, 5564, 5570, 5576, 5582, 5588, 5594, 5600,     
// Line 23
    5606, 5612, 5618, 5624, 5630, 5637, 5643, 5649, 5655, 5661, 5667, 5673, 5679, 5685, 5691, 5698, 5704, 5710, 5716, 5722, 5728, 5734, 5741, 5747, 5753, 5759, 5765, 5772, 5778, 5784, 5790, 5796,     
// Line 24
    5803, 5809, 5815, 5821, 5828, 5834, 5840, 5847, 5853, 5859, 5865, 5872, 5878, 5884, 5891, 5897, 5903, 5910, 5916, 5922, 5929, 5935, 5942, 5948, 5954, 5961, 5967, 5974, 5980, 5987, 5993, 6000,
// Line 25
    6006, 6012, 6019, 6025, 6032, 6038, 6045, 6052, 6058, 6065, 6071, 6078, 6084, 6091, 6098, 6104, 6111, 6117, 6124, 6131, 6137, 6144, 6151, 6157, 6164, 6171, 6178, 6184, 6191, 6198, 6204, 6211, 
// Line 26
    6218, 6225, 6232, 6238, 6245, 6252, 6259, 6266, 6273, 6280, 6286, 6293, 6300, 6307, 6314, 6321, 6328, 6335, 6342, 6349, 6356, 6363, 6370, 6377, 6384, 6391, 6399, 6406, 6413, 6420, 6427, 6434, 
// Line 27
    6441, 6449, 6456, 6463, 6470, 6478, 6485, 6492, 6499, 6507, 6514, 6521, 6529, 6536, 6544, 6551, 6558, 6566, 6573, 6581, 6588, 6596, 6603, 6611, 6619, 6626, 6634, 6641, 6649, 6657, 6664, 6672, 
// Line 28
    6680, 6688, 6695, 6703, 6711, 6719, 6727, 6734, 6742, 6750, 6758, 6766, 6774, 6782, 6790, 6798, 6806, 6814, 6822, 6831, 6839, 6847, 6855, 6863, 6872, 6880, 6888, 6897, 6905, 6913, 6922, 6930,
// Line 29
    6939, 6947, 6956, 6964, 6973, 6982, 6990, 6999, 7008, 7016, 7025, 7034, 7043, 7052, 7061, 7070, 7079, 7088, 7097, 7106, 7115, 7124, 7133, 7143, 7152, 7161, 7171, 7180, 7190, 7199, 7209, 7218, 
// Line 30
    7228, 7238, 7247, 7257, 7267, 7277, 7287, 7297, 7307, 7317, 7327, 7337, 7347, 7358, 7368, 7378, 7389, 7399, 7410, 7421, 7431, 7442, 7453, 7464, 7475, 7486, 7497, 7508, 7520, 7531, 7542, 7554, 
// Line 31
    7565, 7577, 7589, 7601, 7613, 7625, 7637, 7649, 7662, 7674, 7687, 7699, 7712, 7725, 7738, 7751, 7765, 7778, 7792, 7806, 7819, 7834, 7848, 7862, 7877, 7891, 7906, 7921, 7937, 7952, 7968, 7984, 
// Line 32
    8000, 8017, 8033, 8050, 8068, 8085, 8103, 8122, 8140, 8160, 8179, 8199, 8220, 8241, 8262, 8284, 8307, 8331, 8356, 8381, 8408, 8436, 8465, 8496, 8529, 8564, 8602, 8645, 8693, 8750, 8824, 9003};

Please help.

And what is the reason for the ADC to become non linear after I write that code line?

I am working on an Arduino Uno with an ATMEGA328.

Ah - you have 1024 ints, which is 2048 bytes, in a uC with 2048 bytes of SRAM. If these values are not changing, then I suggest you put them in PROGMEM instead, which will store them in Flash for looking up, leaving SRAM free for the rest of your sketch to use.

Aah, thanks! That must be it. I was wondering that after I saw that the addition of 1024 values did not change my compiled code size. I was thinking that adding a "const" before the variable automatically puts that set of data into the flash. But later on all these funny things started happening. I shall check with what you have said and let you know tomorrow but I feel certain that what you said is the issue. Thanks a lot!

No problem. I did something similar with a scrolling display, putting all the fonts in PROGMEM, and then using a big array to let users create a big message. Played with expanding it until the program crashed, then backed off a little. There's the limit!

Actually, I got myself a little mixed up in the process, as I allowed the message to be stored in EEPROM too. But forgot there was only 1K of EEPROM, so after 1K the message was overwriting the 0,1 bytes of EEPROM where I was storing info that kept track of whether a message was stored or not, made for some interesting debugging. So 1022 became the limit ;) Next pass, will change what is stored in EEPROM to be a pointer to a letter thats in PROGMEM, so 1022 letters/spaces can be stored by user, vs 1/5 of that as right now the entire font data that was entered is stored in EEPROM for playback.

Had to dig in a little bit to understand the PROGMEM directive but yes, that was the issue and the program works brilliantly now. Thanks a million for your assistance!

I still cannot figure out why the ADC had become non linear. Because if there was data in the SRAM and the ADC value was being copied to a variable, then the data in that SRAM location would be over written, am I right?

Anyway, thanks a lot for the help! Everything is working correctly and I can go forth working on the rest of the code :)