1

I have the following code in loop() to collect values from an electret microphone

for(int i=0; i<sampleSize; i++){
  analogVal[i]= analogRead(ADC_Pin);
  sumOfSquares= sumOfSquares+sq(analogVal[i]);
}
Serial.print("Sum of squares: ");
Serial.print(sumOfSquares);

where sampleSize is an int variable, I've tried numbers from 3 to 1000, which is my desired value. ADC_Pin is an int variable set to be A1 and has been properly initialized through pinMode. analogVal is an int array of size sampleSize.

My circuit appears to be fine because the output produces a reasonable amount of voltage. I've tried eliminating the line with sumOfSquares and just having the first line in the for loop. However, the Serial Monitor hangs and does not display any values. The serial communication is at 9600 bps.

5
  • 2
    You claim “the Serial Monitor hangs and does not display any values” even though the code doesn't have any references to Serial output! Obviously that's the problem! ¶ Please edit your question to include a Minimal, Complete, and Verifiable example of code, not just snippets. Feb 20, 2017 at 4:22
  • I've added the serial prints that immediately follow the for loop. It does not matter whether the Serial.print is inside or outside the for loop. The serial monitor does not show anything.
    – Skipher
    Feb 20, 2017 at 4:48
  • 3
    Now the obvious problem is that you didn't say Serial.begin(115200); [or whatever the data rate is that you have your serial monitor set at] ¶ Anyway, I don't know what the problem is, and if you don't edit your question to include a Minimal, Complete, and Verifiable example of code -- with visible definitions of every variable used, etc -- perhaps nobody else will be able to tell either. Feb 20, 2017 at 5:15
  • 2
    Yes, MCVE please. There's nothing wrong with the code in your question which would result in this particular problem. (There's another problem but never mind that for now.) The problem is with code you haven't shown.
    – Mark Smith
    Feb 20, 2017 at 7:18
  • What is analogVal? What is sumOfSquares? What is the sq() function? In short: DO NOT POST SNIPPETS LIKE THIS - POST A COMPLETE PROGRAM SO WE CAN SEE THE ENTIRE PROGRAM AND VARIABLE DECLARATIONS.
    – Majenko
    Feb 20, 2017 at 9:55

1 Answer 1

0

Arduino UNO have 2K RAM (many other too). That is 2048 bytes of RAM.

I suppose, that analogVals[] is declared as int and having 1000 entries it takes 2000 bytes of RAM, leaving wholesome 48 bytes for all your others variables (including int i in the for cycle and Serial outside it) and stack for calling functions (like analogRead and sq) and all theirs variables and functions and arguments.

So int i = 2B, Serial = 2B, sumOfSquares = 2B, "Sum of squares: " = 17B of what I see takes anothe 23 bytes of those 48 bytes leaving us with only 25 bytes for all else.

I bet, that you try to use more REAM, than you have and so your stack overflows and

  1. destroys some variable content on the vay

  2. is overwriten by assigning to some variables, which then leads to all sort of problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.