user13651: First a little background on me and my project. Most of my programming experience is with PHP and javascript. I have just recently started looking at C and C++ for this Arduino project. My project is a weather station that feeds its data to a web based database in real time. I will be writing the code for a web site that will access this data base and present the data in realtime. I am currently working on the temperature section of my weather station. It is an RTD coupled to a constant current circuit and feeds a varying voltage to analog pin 5 on my Arduino Uno. My problem is with the code. I have a working code but I decided to make it object oriented so that I could reuse it in other projects. Here is the code that works.
const unsigned int RTD_PIN = A5;
const float MVOLTS_CTS = 4.882813;
const float OHMS_DEGREE = 0.3851;
const unsigned int SAMPLES = 10;
const float MILLI_AMPS = 19.125;
unsigned long currentMillis;
unsigned long oldMillis = 0;
unsigned long timeBetweenSamples = 1000; // milliseconds between samples
int measurement[SAMPLES];
int read_index = 0;
int total = 0;
int average_cts = 0;
boolean initialized = false;
float degC = 0;
float degF = 0;


void setup() {
  Serial.begin(9600);
  for (int i = 0; i < SAMPLES; i++) {
    measurement[i] = 0;
  }
}

void loop() {

  // Reset the counter when we go over the
  // set number of samples
  if (read_index >= SAMPLES) {
    read_index = 0;
    initialized = true;
  }

  // only take a measurement if one second has passed
  currentMillis = millis();

  if (currentMillis - oldMillis >= timeBetweenSamples) {
    // subtract one element from the total
    total = total - measurement[read_index];

    // take a measurement
    measurement[read_index] = analogRead(RTD_PIN);

    // add the new measurement back into the total
    total = total + measurement[read_index];

    // take an average of the readings
    average_cts = total / SAMPLES;

    // calculate degrees
    degC = (average_cts * MVOLTS_CTS / MILLI_AMPS -100) / OHMS_DEGREE;
    degF = degC * 1.8 + 32;

    // print results only if all elements of measurement[] are filled
    if (initialized) {
      Serial.print(degC);
      Serial.print(" degrees C ");
      Serial.print(degF);
      Serial.print(" degrees F ");
      Serial.println(average_cts);
    }

    read_index++;
    oldMillis = currentMillis;


  } 
}

And this is the code that doesn't work.
#include "Arduino.h"
#include "Sensor.h"

    Sensor::Sensor (int analogPin, int numSamplesInAverage, unsigned long sampleRate) {

     sensorPin = analogPin;
     samples = numSamplesInAverage;
    _sampleRate = sampleRate;
     int measurement[samples];

     // initialize the measurement array
     for (int i = 0; i >= samples; i++) {
       measurement[i] = 0;
     }

  }

  int Sensor::update() {

    if (read_index >= samples) {
       read_index = 0;
     }

     // only take a measurement if sample rate has been exceeded
     // and return a rolling average
     currentMillis = millis();

     if (currentMillis - oldMillis >= _sampleRate) {
       //subtract one element from the total
       total = total - measurement[read_index];

       // take a measurement
     measurement[read_index] = analogRead(sensorPin);

       // add the new measurement into the total
       total = total + measurement[read_index];

       // take an average of the readings
       average_cts = total / samples;

       // increment the read_index
       read_index++;
       oldMillis = currentMillis;

      return average_cts;
     }

     // return false if the sample rate has not expired 
     return false;     

  }

It seems like the issue is with this one line of code on line 34.
measurement[read_index] = analogRead(sensorPin);

When this line has not been remarked out the read_index only goes to 4 before it resets to 0 and it does it at the rate of program execution instead of every second like expected.
If this line is remarked out the the read_index is incremented from 0 to 9 once every second as expected.
Here is where the read_index is defined.
#ifndef Sensor_h
#define Sensor_h

#include "Arduino.h"

class Sensor
{
   public:
     Sensor(int analogPin, int numSamplesInAverage, unsigned long sampleRate);
     int update();

   private:
     unsigned int sensorPin;
     unsigned long currentMillis;
     unsigned long oldMillis;
     unsigned long sampleRate; // milliseconds between samples
     int read_index = 0;
     int total = 0;
     int average_cts = 0;
     int samples;
     unsigned long _sampleRate;
     int measurement[];
};

#endif

and this is the Arduino sketch that calls update()
#include <Sensor.h>


Sensor rtd(4, 10, 1000);  // instantiate a Sensor object
int cts;

void setup() {
  Serial.begin(9600);
}

void loop() {

  rtd.update();

  Serial.println(rtd.read_index);
}

can anyone tell me why this works in my procedural code but not the objected oriented code?

user4143: You have made a few fundamental errors with your class.
Firstly you have not provided a storage size (number of elements) for the measurement array class member variable:
class Sensor
{
   public:
     Sensor(int analogPin, int numSamplesInAverage, unsigned long sampleRate);
     int update();

   private:
     // ... cut ...
     int measurement[];           // <<-- How big is this?!
};

Then it seems like you try to dynamically allocate the space for it in your constructor:
Sensor::Sensor (int analogPin, int numSamplesInAverage, unsigned long sampleRate) {

    sensorPin = analogPin;
    samples = numSamplesInAverage;
    _sampleRate = sampleRate;
    int measurement[samples];    // <<-- Here
    // ... cut ...
}

BUT that's not what you are doing.  What you are doing there is creating a new local variable (local to just that one single function) called measurement with an array size of samples.  As soon as you leave the constructor that variable is thrown away.
You haven't touched the class member variable measurement at all.  So, when you do:
measurement[read_index] = analogRead(sensorPin);

you are storing the read value into an array of no size that points nowhere.  And of course that then causes a crash because you're writing to memory that you shouldn't be writing to.
If you want to be specifying the number of array elements in the constructor you need to be learning about dynamic memory allocation using malloc() and free() and all the pitfalls that go along with using those on a small microcontroller with very tight RAM constraints.
For an example of how it can be done you should take a look at my Average Library which uses dynamic allocation to allocate the array memory in the constructor and free it in the destructor.

13651: <p>First a little background on me and my project. Most of my programming experience is with PHP and javascript. I have just recently started looking at C and C++ for this Arduino project. My project is a weather station that feeds its data to a web based database in real time. I will be writing the code for a web site that will access this data base and present the data in realtime. I am currently working on the temperature section of my weather station. It is an RTD coupled to a constant current circuit and feeds a varying voltage to analog pin 5 on my Arduino Uno. My problem is with the code. I have a working code but I decided to make it object oriented so that I could reuse it in other projects. Here is the code that works.</p>

<pre><code>const unsigned int RTD_PIN = A5;
const float MVOLTS_CTS = 4.882813;
const float OHMS_DEGREE = 0.3851;
const unsigned int SAMPLES = 10;
const float MILLI_AMPS = 19.125;
unsigned long currentMillis;
unsigned long oldMillis = 0;
unsigned long timeBetweenSamples = 1000; // milliseconds between samples
int measurement[SAMPLES];
int read_index = 0;
int total = 0;
int average_cts = 0;
boolean initialized = false;
float degC = 0;
float degF = 0;


void setup() {
  Serial.begin(9600);
  for (int i = 0; i &lt; SAMPLES; i++) {
    measurement[i] = 0;
  }
}

void loop() {

  // Reset the counter when we go over the
  // set number of samples
  if (read_index &gt;= SAMPLES) {
    read_index = 0;
    initialized = true;
  }

  // only take a measurement if one second has passed
  currentMillis = millis();

  if (currentMillis - oldMillis &gt;= timeBetweenSamples) {
    // subtract one element from the total
    total = total - measurement[read_index];

    // take a measurement
    measurement[read_index] = analogRead(RTD_PIN);

    // add the new measurement back into the total
    total = total + measurement[read_index];

    // take an average of the readings
    average_cts = total / SAMPLES;

    // calculate degrees
    degC = (average_cts * MVOLTS_CTS / MILLI_AMPS -100) / OHMS_DEGREE;
    degF = degC * 1.8 + 32;

    // print results only if all elements of measurement[] are filled
    if (initialized) {
      Serial.print(degC);
      Serial.print(" degrees C ");
      Serial.print(degF);
      Serial.print(" degrees F ");
      Serial.println(average_cts);
    }

    read_index++;
    oldMillis = currentMillis;


  } 
}
</code></pre>

<p>And this is the code that doesn't work.</p>

<pre><code>#include "Arduino.h"
#include "Sensor.h"

    Sensor::Sensor (int analogPin, int numSamplesInAverage, unsigned long sampleRate) {

     sensorPin = analogPin;
     samples = numSamplesInAverage;
    _sampleRate = sampleRate;
     int measurement[samples];

     // initialize the measurement array
     for (int i = 0; i &gt;= samples; i++) {
       measurement[i] = 0;
     }

  }

  int Sensor::update() {

    if (read_index &gt;= samples) {
       read_index = 0;
     }

     // only take a measurement if sample rate has been exceeded
     // and return a rolling average
     currentMillis = millis();

     if (currentMillis - oldMillis &gt;= _sampleRate) {
       //subtract one element from the total
       total = total - measurement[read_index];

       // take a measurement
     measurement[read_index] = analogRead(sensorPin);

       // add the new measurement into the total
       total = total + measurement[read_index];

       // take an average of the readings
       average_cts = total / samples;

       // increment the read_index
       read_index++;
       oldMillis = currentMillis;

      return average_cts;
     }

     // return false if the sample rate has not expired 
     return false;     

  }
</code></pre>

<p>It seems like the issue is with this one line of code on line 34.</p>

<pre><code>measurement[read_index] = analogRead(sensorPin);
</code></pre>

<p>When this line has not been remarked out the read_index only goes to 4 before it resets to 0 and it does it at the rate of program execution instead of every second like expected.</p>

<p>If this line is remarked out the the read_index is incremented from 0 to 9 once every second as expected.</p>

<p>Here is where the read_index is defined.</p>

<pre><code>#ifndef Sensor_h
#define Sensor_h

#include "Arduino.h"

class Sensor
{
   public:
     Sensor(int analogPin, int numSamplesInAverage, unsigned long sampleRate);
     int update();

   private:
     unsigned int sensorPin;
     unsigned long currentMillis;
     unsigned long oldMillis;
     unsigned long sampleRate; // milliseconds between samples
     int read_index = 0;
     int total = 0;
     int average_cts = 0;
     int samples;
     unsigned long _sampleRate;
     int measurement[];
};

#endif
</code></pre>

<p>and this is the Arduino sketch that calls update()</p>

<pre><code>#include &lt;Sensor.h&gt;


Sensor rtd(4, 10, 1000);  // instantiate a Sensor object
int cts;

void setup() {
  Serial.begin(9600);
}

void loop() {

  rtd.update();

  Serial.println(rtd.read_index);
}
</code></pre>

<p>can anyone tell me why this works in my procedural code but not the objected oriented code?</p>
 4143: <p>You have made a few fundamental errors with your class.</p>

<p>Firstly you have not provided a storage size (number of elements) for the <code>measurement</code> array class member variable:</p>

<pre><code>class Sensor
{
   public:
     Sensor(int analogPin, int numSamplesInAverage, unsigned long sampleRate);
     int update();

   private:
     // ... cut ...
     int measurement[];           // &lt;&lt;-- How big is this?!
};
</code></pre>

<p>Then it seems like you try to dynamically allocate the space for it in your constructor:</p>

<pre><code>Sensor::Sensor (int analogPin, int numSamplesInAverage, unsigned long sampleRate) {

    sensorPin = analogPin;
    samples = numSamplesInAverage;
    _sampleRate = sampleRate;
    int measurement[samples];    // &lt;&lt;-- Here
    // ... cut ...
}
</code></pre>

<p>BUT that's <em>not</em> what you are doing.  What you are doing there is creating a <em>new</em> local variable (local to <em>just that one single function</em>) called <code>measurement</code> with an array size of <code>samples</code>.  As soon as you leave the constructor that variable is thrown away.</p>

<p>You haven't touched the class member variable <code>measurement</code> at all.  So, when you do:</p>

<pre><code>measurement[read_index] = analogRead(sensorPin);
</code></pre>

<p>you are storing the read value into an array of no size that points nowhere.  And of course that then causes a crash because you're writing to memory that you shouldn't be writing to.</p>

<p>If you want to be specifying the number of array elements in the constructor you need to be learning about <em>dynamic memory allocation</em> using <code>malloc()</code> and <code>free()</code> and all the pitfalls that go along with using those on a small microcontroller with very tight RAM constraints.</p>

<p>For an example of how it can be done you should take a look at my <a href="https://github.com/MajenkoLibraries/Average" rel="nofollow">Average Library</a> which uses dynamic allocation to allocate the array memory in the constructor and free it in the destructor.</p>
