For loop counter value resetting, data smoothing logging

I’m working on a little robotics project, using some cheap servos

To improve the accuracy of the servos, I’ve connected an extra position feedback wire to the servo position pot, in order to properly check and set its range.

But first I wanted to check the noise on the signal using both raw and smoothed data

in this program, the analog pin a0 - 02 corresponds to the position feedback signal wire from servo[0] - [2]

I’m having no problem polling the raw data, but when I run the smoothed data loop, the loop counter l sets its value to the polled smoothed data value (usually around 300) - and counts on from there - forever!

I’ve been back and forth through this code, and I can’t figure out any reason why this would be, any help would be appreciated - it’s doing my head in!

I’m running on an Arduino Nano 328

#include <Servo.h> //import servo library

const int numServos = 3; //how many servos do you have?
const int sPin[numServos] = {3, 5, 6}; //what pins do they correlate to?
Servo servo[numServos]; //declare the servo array



float x[numServos]; //angle converted to radians to derive cosine wave

int Button = 2;

int btwReadings = 20; //delay time between
int whlReading = 3; //delay time between analog readings of internal pot

boolean rangeTest = false;
boolean doneMove[numServos];




void setup()
{

 Serial.begin(9600); // initialize serial output
 Serial.println("it's on!");
 analogReference(EXTERNAL);
 pinMode (Button, INPUT);
 for (int i = 0; i < numServos; i++)
 {
 pinMode (sPin[i], OUTPUT);
 }


 if (rangeTest == false)
 {
 for (int i = 0; i < numServos; i++)
 {
 logPosData(i); //go test the range and set the values
 doneMove[i] = true;
 }
 rangeTest = true;

 }
 delay(1000);
}

void loop()
{

}

void logPosData(int x)  //parameter passed is array position of servo
{
 int pb = 0; //used to hold push button reading
 int l = 0;
 
 int j;
 int mean;
 int result;
 int test;
 int reading[20];
 boolean done;

 int pos;

 int pulse = 750; //first uS pulse used in range test
 Serial.print("Press button to set range of Servo[");
 Serial.print(x);
 Serial.println("].");
 while(!pb)
 {
 pb = digitalRead(Button);
 }
 pb = 0;
 Serial.print("Recording raw position data in ..3");
 for (int i = 2; i >= 0; i--) //count down three seconds
 {
 delay(1000);
 Serial.print("..");
 Serial.print(i);
 }
 Serial.println();
 servo[x].attach(sPin[x]);
 delay(20);
 servo[x].writeMicroseconds(pulse); //send servo to start of range
 delay(2000); //wait for it to get there
 for (int i = 0; i < 10; i++) // this loop just sends 50 CSV raw pos reading for each pulse increment
 {
 servo[x].writeMicroseconds(pulse);
 Serial.print("Pulse:, ");
 Serial.print(pulse);
 Serial.print(", ");
 for (int i = 0; i < 50; i++)
 {
 pos = analogRead(x);
 Serial.print(pos);
 Serial.print(",");

 delay(20);

 }

 pos = analogRead(x);
 Serial.print(pos);

 pulse += 30;
 Serial.println();
 }

 Serial.print("Press button to record averaged readings of Servo[");
 Serial.print(x);
 Serial.println("].");
 while(!pb)
 {
 pb = digitalRead(Button);
 }
 pb = 0;
 Serial.print("Recording smoothed position data in ..3");
 for (int i = 2; i >= 0; i--) //count down three seconds
 {
 delay(1000);
 Serial.print("..");
 Serial.print(i);
 }
 Serial.println();
 pulse = 1000;
 delay(20);
 servo[x].writeMicroseconds(pulse); //send servo to start of range
 delay(2000); //wait for it to get there
 for (int i = 0; i < 50; i++) // this loop just sends 50 CSV raw pos reading for each pulse increment
 {
 servo[x].writeMicroseconds(pulse);
 Serial.print("Pulse:,");
 Serial.print(pulse);
 Serial.print(",");
 for (int counter = 0; counter < 50; counter++)
 
 {
 
 pos = getFeedback(x);
 Serial.print(pos);
 Serial.print(",");
 

 }

 pos = getFeedback(x);
 Serial.print(pos);

 pulse += 30;

 Serial.println();
 }



}//end setRange()



/*
THIS FUNCTION READS THE INTERNAL SERVO POTENTIOMETER and smooths the data
*/
int getFeedback(int a)
{
 int j;
 long mean;
 int result;
 int test;
 int reading[20];
 boolean done;

 for (j = 0; j < 20; j++)
 {
 reading[j] = analogRead(a); //get raw data from servo potentiometer
 delay(whlReading);
 } // sort the readings low to high in array
 done = false; // clear sorting flag
 while(done != true)  // simple swap sort, sorts numbers from lowest to highest
 {
 done = true;
 for (j = 0; j < 20; j++)
 {
 if (reading[j] > reading[j + 1])  // sorting numbers here
 {
 test = reading[j + 1];
 reading [j + 1] = reading[j] ;
 reading[j] = test;
 done = false;
 }
 }
 }
 mean = 0;
 for (int k = 6; k < 14; k++) //discard the 6 highest and 6 lowest readings
 {
 mean += reading[k];
 }
 result = mean / 8; //average useful readings
 return(result);
}    // END GET FEEDBACK

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Update:

when I ran the logging routine as the main body of the sketch rather than as a function, this problem disappeared

so, my new question is - I'm assuming the for loop counter variable from the logPosData() function and the data logger loop variable, j, from the getFeedback function are using the same address to store their value - why is this and how can I avoid it in future?

Thanks

Dillon

SOLVED(ish)

It seems the code was fine when using the standard Arduino IDE to upload

I was using the codebender.cc online IDE when I was having this issue

Has anyone else had issues with this site?

Without compiling your code, I see that you’re re-using int i - in nested for-loops.
While it /may/ work in local scope, it’s very bad style

for (int i = 0; i < 10; i++) // <=======
 {
   ::::
   ::::
 for (int i = 0; i < 50; i++)   // <=======
    {
    pos = analogRead(x);
    Serial.print(pos);

Thanks for the tip - will clean that up

though for this issue with codebenders - I tried different count variables, it was clearly an issue with the function using the same register as the loop

DillonMEK:
Thanks for the tip - will clean that up

though for this issue with codebenders - I tried different count variables, it was clearly an issue with the function using the same register as the loop

It is clearly NOT a problem with codebenders! In your other post ( http://forum.arduino.cc/index.php?topic=418308.0 ) I have shown that the problem is yours and not codebenders! It is nothing at all to do with codebender's register allocation. Your code is indexing the array to a position outside of the array and then writing to that position and therefore corrupting other variables.

In my experience it is pretty much always my code that is at fault and I am very very slow to blame the compiler. Don't be so quick to blame the compiler.