 what is my problem in array Multiply !!!!

i try to write code for calculate rms
but i have problem in my code.

how can i solve my problem?

const unsigned int numReadings = 100;
float val2;
float sum_squared_voltage;
float mean_square_voltage;
float root_mean_square_voltage;
void setup()
{

Serial.begin(2400);
}

void loop()
{
for (unsigned int i=0; i<numReadings; i++)
{

• delay(1000); //wait 1 sec*

• }*
analogVals=analogVals_analogVals;_
sum_squared_voltage += analogVals;*

mean_square_voltage = sum_squared_voltage / number_of_samples;
root_mean_square_voltage = sqrt(mean_square_voltage);

* Serial.println(root_mean_square_voltage);*

}

but i have problem in my code.

We could maybe see the problem easier if you put the code in code tags, unless half of it is meant to be in italics

read this before posting a programming question

It would also help if you told us what the problem is.

Even as it is I can see a problem

A 2 dimensional array

A 1 dimensional array.

To compound the problem, even if you used

there is no 1 level of the array, only a 0 level.

Did I mention that you have not declared number_of_samples ?

I WANT TO READ ANALOG DATA AND SAVE IN ARRAY
THEN DOING SOME MATHEMATICS ON MY DATA
LIKE MULTIPLY AND…

//let’s say you want to read up to 100 values
const unsigned int numReadings = 100;
float val2;
float sum_squared_voltage;
float mean_square_voltage;
float root_mean_square_voltage;
void setup()
{

Serial.begin(2400);
}

void loop()
{
for (unsigned int i=0; i<numReadings; i++)
{

• delay(1000); //wait 1 sec*

• }*
analogVals=analogVals_analogVals;_
sum_squared_voltage += analogVals;*

mean_square_voltage = sum_squared_voltage / number_of_samples;
root_mean_square_voltage = sqrt(mean_square_voltage);

* Serial.println(root_mean_square_voltage);*

}

FOR GOODNESS SAKE, PUT YOUR CODE IN CODE TAGS AND STOP SHOUTING !

oh sorry
sure i do it

[code]
//let's say you want to read up to 100 values
const unsigned int numReadings = 100;
float val2;
float sum_squared_voltage;
float mean_square_voltage;
float root_mean_square_voltage;
void setup()
{

Serial.begin(2400);
}

void loop()
{
for (unsigned int i=0; i<numReadings; i++)
{
delay(1000); //wait 1 sec

}
analogVals[i]=analogVals[i]*analogVals[i];
sum_squared_voltage += analogVals[i];

mean_square_voltage = sum_squared_voltage / number_of_samples;
root_mean_square_voltage = sqrt(mean_square_voltage);

Serial.println(root_mean_square_voltage);

}

[/code]

Okay, so what is wrong?

With code tags. Much better thanks.

Does analogVals* actually exist ? The array has been declared as having 1 element and the index to that element is 0, not 1. Why do you need an array with one element anyway ?*
analogVals[i] = analogVals[i] * analogVals[i];What will the value of i be when this command is executed ? Bear in mind that the for loop has ended by then.

Why are you multiplying integer values? The result will overflow as analog values range from 0 to 2^10-1,
so a product will range upto nearly 2^20 which overflows an int which is 16 bits.

NOW I modify my code
but the output data is not correct

and have delay

//let's say you want to read up to 100 values
const unsigned int numReadings = 40;
float val2;
float sum_squared_voltage;
float mean_square_voltage;
float root_mean_square_voltage;
void setup()
{

Serial.begin(2400);
}

void loop()
{
for (unsigned int i=0; i<numReadings; i++)
{
delay(100); //wait 1 sec

}
for (unsigned int i=0; i<numReadings; i++){
analogVals[i]=  analogVals[i]-2.48;
analogVals[i]=analogVals[i]*analogVals[i];
sum_squared_voltage += analogVals[i];
}
root_mean_square_voltage = sqrt(mean_square_voltage);

Serial.println(root_mean_square_voltage);

}

You don't say exactly what is wrong but
analogVals[i] =  analogVals[i] - 2.48;is not doing what you want because analogVals is declared as an int

NO I MODIFY MY CODE AGAIN
THE OUTPUT MUST BE ZERO CUZ I READ JUST 2.48VDC

THIS IS MY CODE

//let's say you want to read up to 100 values
const unsigned int numReadings = 40;
float val2;
float sum_squared_voltage;
float mean_square_voltage;
float root_mean_square_voltage;
void setup()
{

Serial.begin(9600);
}

void loop()
{
for (unsigned int i=0; i<numReadings; i++)
{
delay(100); //wait 1 sec

}
for (unsigned int i=0; i<numReadings; i++){
analogVals[i]= analogVals[i]/1024;
analogVals[i]= analogVals[i]*5;
analogVals[i]=  analogVals[i]-2.48;// Subtraction THE OFFSET VOLTAGE FROM ANALOGE READ
analogVals[i]=analogVals[i]*analogVals[i];
sum_squared_voltage += analogVals[i];
}
root_mean_square_voltage = sqrt(mean_square_voltage);

Serial.println(   sum_squared_voltage);

}

analogRead() produces integers in the range 0-1023. It doesn’t produce 2.48. You can’t add or subtract 2.48 to an integer.

Start with the simplest analog example in the examples folder. Look at the results it’s producing. You will probably find that your 2.48 volt input is producing values around 507. If you want to subtract this offset, then subtract 507, not 2.48.

aryanpour:
NO I MODIFY MY CODE AGAIN
THE OUTPUT MUST BE ZERO CUZ I READ JUST 2.48VDC

THIS IS MY CODE

If you keep using all capitals I will close this thread and ban you.