Serial monitor not displaying at high number of data points

I am reading analog data from a circuit and trying to view it on the serial monitor. The baud rates are matched. It works if I take 100 data points. However, if I increase to 200 it stops displaying data. What can I do to fix this?

Thanks!

cmokeefe:
I am reading analog data from a circuit and trying to view it on the serial monitor. The baud rates are matched. It works if I take 100 data points. However, if I increase to 200 it stops displaying data. What can I do to fix this?

Thanks!

Start by posting your sketch code and telling us what model board you are using. Use the code tags (#) to create a code window and copy and paste the sketch.

Lefty

Here is the code. I am using Arduino UNO board connected through USB

const int sensorPin = A0;    // select the input pin for the potentiometer
// *** will eventually need 3 of these  ***
const int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
int led = 13;

const int numPoints= 100; 
const int waitTime= 5;
int readings1[numPoints];

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
    // initialize serial communication at 2400 bits per second:
  Serial.begin(9600);
  pinMode(2,OUTPUT); // pin 2 as output to connect to RFID
  digitalWrite(2,LOW); // activate RFID reader
  
   for(int i=0; i<numPoints; i++) {  // initialize readings
    readings1[i] = 0;



   }
}

void loop() {
  // read the input on analog pin 0 into array:
//  int tester= 0;

int sensorMax1= 0;


  Serial.println("readings 1");
  for(int ii=0; ii<numPoints; ii++) {
   readings1[ii] = analogRead(sensorPin); 
   
       if (readings1[ii] > sensorMax1); {
        sensorMax1= readings1[ii]; }
        
        
//   Serial.println(readings1[ii]);
 
  delay(waitTime);        // delay in between reads for stability
  }
  
  
    Serial.print("max1: ");
    Serial.println(sensorMax1);

  if (maxavg>0)  {
      digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  } else {
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW 
  }
    }

I'll let the software gurus give you a better read on your sketch, but could it be that you are using too much of the 2K byte maximum SRAM capacity when you increase your array size from 100 to 200 ints? Maybe you could gradually increasing the array size, say 20 ints at a time, to see if the problem is triggered just at some specific array size increase?

Lefty

I tried to compile your code but it is didn't compile. So I'm assuming that this is only a fragment and your actual code is much larger. Please, ensure that when you post code, that it compiles and that it exhibits the same behaviour as the problem you are trying to solve.

Given that, assuming you are allocating too much space in your object file, instead of allocating the memory statically (which increases the size of the object and hex file) try allocating the readings1 array dynamically, like this:

const int numPoints= 100; 
int *readings1 = new int[numPoints];

It'll act almost the same way as an array, with the only exception having to do with template functions taking in an array. Something you probably don't have to worry about.

Good luck

Adrian

adrian_h:

const int numPoints= 100; 

int *readings1 = new int[numPoints];

new is not supported by avr-libc and so without defining it first, this code won't work.

if (readings1[ii] > sensorMax1);

Oops.

Oh, that's interesting. I didn't think that it was disabled. I've not tried it. Is it due the bug in the malloc()/free() I've been hearing about?

Well, I think the main prob with malloc and free is due to some sort of memory leak, you can use malloc() and don't free it.

So do this instead:

const int numPoints= 100; 
int *readings1 = (int*)malloc(sizeof(int)*numPoints);

The release notes for IDE 1.0.4 contains this:

ARDUINO 1.0.4 - 2013.03.11

[core]

  • Fixed malloc bug (Paul Stoffregen)

Lefty