Hello,
I'm trying to store 10 values of sensor data to an array and for some reason the array is taking the last value and storing it as all 10 values in my array. If someone could help me understand what I'm doing wrong I would be exretemly appreciate.
#include <Wire.h>
int numReads=10;
int cont;
float sensor;
float sum;
float average;
float myArray[10];
const int ACPin = A2;
#define ACTectionRange 20;
#define VREF 3.3
float readACCurrentValue()
{
float ACCurrtntValue = 0;
float peakVoltage = 0;
float voltageVirtualValue = 0; //Vrms
for (int i = 0; i < 5; i++)
{
peakVoltage += analogRead(ACPin); //read peak voltage
delay(1);
}
peakVoltage = peakVoltage / 5;
voltageVirtualValue = peakVoltage * 0.707; //change the peak voltage to the Virtual Value of voltage
/*The circuit is amplified by 2 times, so it is divided by 2.*/
voltageVirtualValue = (voltageVirtualValue / 1024 * VREF ) / 2;
ACCurrtntValue = voltageVirtualValue * ACTectionRange;
return ACCurrtntValue;
}
void setup() {
Serial.begin(9600);
while(!Serial);
do {
float ACCurrentValue = readACCurrentValue();
Serial.println(ACCurrentValue);
//Serial.println(sum);
for (int k = 0; k < numReads; k++){
//sensor2 =
sensor = ACCurrentValue;
myArray[k] = sensor;
sum += sensor;
}
cont++;
} while(cont < numReads);{
average = sum/(numReads*10);
Serial.print("Average: "); Serial.print(average);
Serial.println(" ");
Serial.print("Print Array Value:");Serial.print(myArray[4]);//Serial.println(" ");
Serial.println(" ");
for(int i = 0; i < numReads; i++)
{
Serial.println(myArray[i]);
}
}
}
void loop() {
}
Here's screen shot of the serial monitor data


