Issue with codebender.cc compiler

I recently came across the codebender website in a techcrunch article from afew years back now, and thought I'd give it a go
Their IDE seems nice to use, as does their whole user interface, so I started developing some projects on it, however I found a bit of a serious issue with their compiler/uploader

I posted previously on this, thinking it was a problem with my code, however, the problem was occurring when I ran a digital smoothing function from either within another function, from within a loop, or sometimes if I just called it twice in succession. What seems to occur is that it over writes the loop count variable(s) with values generated by the digital smooth function - which either traps the program in an infinite loop (when the loop logic isn't met) or crashes the program (when the loop variable is being used for other operations)

I figured it was a problem at their end (eventually, after going nuts trying to find it at my end) when I cut and paste the same code back into the standard Arduino IDE and uploaded it from there.

I have alerted them to the issues I was having, but was wondering if anyone else had had similar issues with that site, and also whether any one with a more CS-y background could offer a possible explanation.

Post the code in question.

Here's the code

it works fine when uploaded from the arduino IDE, but I keep having problems with the codebenders online IDE

/*
Program outputs a range of position readings for a range of positions on
3 servos for a hexapod leg to a terminal
CoolTerm was used to save that data to a file for analysis
For 50 pulse positions, it outputs 50 raw and 50 smothed readings
The purpose is to analyse the noise and accuracy of the pos feedback
This code can be addapted for other servo configurations using pos feedback
by changing numServo, and the joint name array
Dillon MacEwan 13/8/16
*/


#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
String joint[numServos] = {"Coxia", "Femur", "Tibia"}; //name of each servo




#define Button 2

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


int pb = 0; //used to hold push button reading


int pos;

int pulse = 750; //first uS pulse used in range test



void setup()
{

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



}

void loop()
{

	Serial.print("vRef 2.48V");
	Serial.println("Polling 40ms, no caps");
	Serial.println();
	for (int x = 0; x < numServos; x++)
	{
		Serial.print("Press button to set range of Servo: ");
		servo[x].writeMicroseconds(pulse); //send servo to start of range
		Serial.print(joint[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();
		Serial.print(joint[x]);
		pulse = 750;
		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++)
		{
			servo[x].writeMicroseconds(pulse);
			Serial.println();
			Serial.print("Pulse:");
			Serial.print(pulse);

			Serial.println();
			Serial.print("Raw Data");
			Serial.println();
			for (int i = 0; i < 50; i++) // this loop just sends 50 CSV raw pos reading for each pulse increment
			{

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

				delay(30);

			}
			pos = analogRead(x);
			Serial.print(pos);
			Serial.println();
			Serial.print("Smoothed Data");
			Serial.println();
			for (int i = 0; i < 50; i++) // this loop just sends 50 CSV raw pos reading for each pulse increment
			{

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


				
			}

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

}





/*
THIS FUNCTION READS THE INTERNAL SERVO POTENTIOMETER and smooths the data
*/
int getFeedback(int a)
{
	int j;
	int 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

You declare 'reading' as an array of length 20. You iterate through the array
for (j = 0; j < 20; j++)
which takes j from 0 to 19. You then compare reading[j] with reading[j+1] and swap if necessary. BUT BUT BUT when j == 19 then j+1 == 20 and there in no reading 20. This means the sort may overwrite the next memory location! The loop should be

for (j = 0; j < 19; j++)

When one overwrites memory by indexing out of the correct range of an array one can never be sure of the consequences since it depends on both the data and the memory allocation algorithm used by the compiler. Fix this flaw and test again.

P.S. That is a dreadful sort method. Even poorer than bubble sort.

Ah! thanks

and can you suggest a better sort method?

tbh, I have been modifying this from someone else's code - I've ended up abandoning it and starting from scratch as it has been a bit full of similar problems and errors.

DillonMEK:
Ah! thanks

Have you informed "codebender" that you were mistaken about the bug?

and can you suggest a better sort method?

Just about any of the standard algorithms would be better but having fixed the bug in your sorting you could continue with your existing sort approach.

Since it is essentially random data you are sorting Quicksort is probably best but I like Heapsort. There are numerous implementations of both published on the WWW so I won't add clutter by posting my versions; I'm betting someone has published one or other for the Arduino but before you abandon your existing sort make sure you thoroughly test the new algorithm outside of your main program.

tbh, I have been modifying this from someone else's code - I've ended up abandoning it and starting from scratch as it has been a bit full of similar problems and errors.

There is a load of crap out there on the WWW and borrowing code is dangerous unless it is from a reputable source.

"Have you informed "codebender" that you were mistaken about the bug?"

yes, I did

Thanks again for your help