Need to calculate RMS of each array of voltage input MEGA2560

I have a simple circuit. a 10k potentiometer connected to A0, GND, 5V. I have to:

  • Convert voltage analog input to digital. (the easy part)
  • Set the sampling rate to 960 samples per second. (someone helped me with that but I am not sure if it is correct)
  • Save each 16 samples in an array. (the real struggle)
  • Calculate the voltage RMS (root mean square) of each array. (I have the code but it does not count only 16 samples)

Can you please help me with my code? Thank you!

The code I have so far (attached):

const int Vol_pin = A0;

void setup(){
void loop() {
  int value;
  float voltage;
  float anArray[16];
  byte arrayIndex = 0;
  anArray[arrayIndex] = voltage;
  float squared_voltage = 0;
  float sum_squared_voltage;

 value = analogRead(Vol_pin);

 voltage = value * 5 / 1023;

  for (int i=0; i < 16; i=i+1)
  anArray[i] = voltage;

  squared_voltage = anArray[i] * anArray[i];

  sum_squared_voltage += squared_voltage;
   unsigned int tempData = 0;

  long duration = micros();
  for(int i=0; i<930; i++){
    tempData = analogRead(A0);
  duration = micros() - duration;
  float frequency = 100000./(duration/1000.);

  float   mean_square_voltage = sum_squared_voltage / 16;
  float  root_mean_square_voltage = sqrt(mean_square_voltage);

  Serial.print("RMS : ");
  Serial.println( root_mean_square_voltage );
  Serial.print("Sample Frequency = ");Serial.println(frequency,3);
  Serial.print( "Value: " );
  Serial.println( value );
  Serial.print( "Voltage: " );
  Serial.println ( voltage );

Cameron_Code.ino (1.04 KB)

When is your homework assignment due?

There are language tutorials for each component of your assignment on the web. Take advantage of them!

For example, when I google "arrays in C/C++" to learn how to declare and store data in arrays, this tutorial is the top hit.

Check out range based for loops.

It's due in two weeks. Thanks for the links!