I wrote a function with return:
float AccError (int St, float Diminisher )
{ float Cumm ;
float D [50];
for (int i = 1; i <= 20; i++) {
//Serial.println (i);
D [i] = Diminisher * i;
// Serial.println (D[i], 10);
if (i == St)
{ return Cumm = D[1] + D[2] + D[3] + D[4] + D[5] + D[6] + D[7] + D[8] + D[9] + D[10] + D[11] + D[12] + D[13] + D[14] + D[15] + D[16] + D[17] + D[18] + D[19] + D[20];
//Serial.print ("Cummulation= ");
//Serial.println (Cumm, 10);
}
}
}
In a different function I want to print the result.
The first line works, but the printf returns some nonsense.
Serial.println (AccError(20, Diminisher), 6);
Serial.printf ("AccError M%u AccError %.4° \n", motor_id, AccError(20, Diminisher));
why would that be?
Thanks
gfvalvo
November 17, 2020, 11:28pm
2
laptophead:
why would that be?
Your posted code in incomplete and does not compile. So, we have no way to tell.
What do you think the format specifier "%.4°" is supposed to do?
That would print a float with 4 dec points. Used it before like this
Serial.printf ( "GoTo_Gauss_M First %u :%.4f°, %d \n", motor_id, GoTo_Gauss[motor_id] , i);
Notice a difference?
Serial.printf ("AccError M%u AccError %.4° \n", motor_id, AccError(20, Diminisher));
Serial.printf ( "GoTo_Gauss_M First %u :%.4[color=red][b]f[/b][/color]°, %d \n", motor_id, GoTo_Gauss[motor_id] , i);
OMG,
Silly me, I ve been coding 6 hrs today. Should walk in the park.
THANKS A LOT
also
Is there a way to replace this?
return Cumm = D[1] + D[2] + D[3] + D[4] + D[5] + D[6] + D[7] + D[8] + D[9] + D[10] + D[11] + D[12] + D[13] + D[14] + D[15] + D[16] + D[17] + D[18] + D[19] + D[20];
//Serial.print ("Cummulation= ");
With some "sum" and range function?
I need to go to 50 index....
Thanks
I would use a for loop to sum 20 array elements.
So I tried
float AccError (int St, float Diminisher )
{ float Cumm = 0 ;
float D [30];
for (int i = 1; i <= St; i++) {
//Serial.println (i);
D [i] = Diminisher * i;
// Serial.println (D[i], 10);
if (i == St)
{ //return Cumm = D[1] + D[2] + D[3] + D[4] + D[5] + D[6] + D[7] + D[8] + D[9] +
//D[10] + D[11] + D[12] + D[13] + D[14] + D[15] + D[16] + D[17] + D[18] + D[19] + D[20]
//+ D[21] + D[22] + D[23] + D[24] + D[25] + D[26] + D[27] + D[28] + D[29] + D[30];
for (int index = 0; index <= 30; index++)
{
return Cumm += D[index];
}
}
}
}
but I am getting a different result than the D[1] + D[2] + D[3] etc. addition.
Any ideas?
Thanks
This line says to sum 31 elements of D[].
for (int index = 0; index <= 30; index++)
This line sums 20 elements, so I would not be surprised if the results are different.
return Cumm = D[1] + D[2] + D[3] + D[4] + D[5] + D[6] + D[7] + D[8] + D[9] + D[10] + D[11] + D[12] + D[13] + D[14] + D[15] + D[16] + D[17] + D[18] + D[19] + D[20];
but I am getting a different result than the D[1] + D[2] + D[3] etc. addition.
Any ideas?
float D [30];
The elements in that array have indexes 0 to index 29.
There is no you do not control the value located at D[30], and you could read be anything at D[30].
It appears that you did not take the walk in the park.
Look at response no 8. I added from 1 to 30. It was the correct answer.
for (int index = 1; index <= 30; index++)
{
return Cumm += D[index];
}
I would want the lines above to get there also. Where am I wrong?
Sorry, Beginner here.
Thanks
Where am I wrong?
It was pointed out to you in reply #10 that there is no element D[30] in your array. The indexes are 0 referenced, and run from 0 to 29 for an array declared as float D[30].
OK, I made D a 31 index array, still wrong answer.
float AccError (int St, float Diminisher )
{ float Cumm = 0 ;
float D [31];
for (int i = 1; i <= St; i++) {
//Serial.println (i);
D [i] = Diminisher * i;
// Serial.println (D[i], 10);
if (i == St)
{ //return Cumm = D[1] + D[2] + D[3] + D[4] + D[5] + D[6] + D[7] + D[8] + D[9] +
//D[10] + D[11] + D[12] + D[13] + D[14] + D[15] + D[16] + D[17] + D[18] + D[19] + D[20]
//+ D[21] + D[22] + D[23] + D[24] + D[25] + D[26] + D[27] + D[28] + D[29] + D[30];
for (int index = 1; index <= 30; index++)
{
return Cumm += D[index];
}
}
}
}
You populate only st elements of the array, but you sum all of them. If st isn't 30, the others are not guaranteed to be zero.
Sorry, still don't know what to do.
All D's are having values bigger than zero inside. I will just live with the long form.
I wish I had a mentor
This does not initialize the D values to zero:
float D [31];
This does:
float D [31]={0};
Tried that , it does not sum up. Just prints the D val
I executed:
if (FirstKbd == 'd')
{
Serial.print ("AC Err = ");
Serial.println (AccError (30, 2 ));
}
Got back in the sec monitor
AC Err = 2.00
Thanks for not giving up on me
Your return instruction is at the wrong level for the summation.
void setup() {
Serial.begin(115200);
Serial.println (AccError (30, 2 ));
}
void loop() {
// put your main code here, to run repeatedly:
}
float AccError (int St, float Diminisher )
{ float Cumm = 0 ;
float D [31] = {0};
for (int i = 1; i <= St; i++) {
//Serial.println (i);
D [i] = Diminisher * i;
// Serial.println (D[i], 10);
if (i == St)
{ //return Cumm = D[1] + D[2] + D[3] + D[4] + D[5] + D[6] + D[7] + D[8] + D[9] +
//D[10] + D[11] + D[12] + D[13] + D[14] + D[15] + D[16] + D[17] + D[18] + D[19] + D[20]
//+ D[21] + D[22] + D[23] + D[24] + D[25] + D[26] + D[27] + D[28] + D[29] + D[30];
for (int index = 1; index <= 30; index++)
{
//return Cumm += D[index];
Cumm += D[index];
}
return Cumm;
}
}
}
Thanks
That worked, so obvious in retrospect... Gave you karma
Mitch