I don't think my issue is necessarily Arduino related, probably just poor C code, but it is code running on an Arduino. I'm trying to sort an array that's storing DS18B20 temperature readings. I use the DS18B20's high/low alarm set points to store each sensor's bin and sensor ID number. The Arduino is reading multiple bins and if you've worked with these 1-wire sensors before, you know that they are read in a random order. I have a 2d byte array storing their bin ID, sensor ID and temperature reading and for display purposes, I'd like to sort them in order of bin ID, then sensor ID using as little RAM as possible. Here's what I've got. TempSensorReadings is declared globally.
int TempSensorReadings[100][3];
void SortTempSensors(){
Serial.print(F("\r\nUnsorted list:"));
PrintSensorList();
byte temp[3];
//first sort by least important criteria, in this case sensor ID number
for (byte i=0;i<NumTempSensors-1;i++){
for (byte t=i+1;t<NumTempSensors;t++){
if (TempSensorReadings[t][1]<TempSensorReadings[i][1]){
temp[0]=TempSensorReadings[t][0];
temp[1]=TempSensorReadings[t][1];
temp[2]=TempSensorReadings[t][2];
TempSensorReadings[t][0]=TempSensorReadings[i][0];
TempSensorReadings[t][1]=TempSensorReadings[i][1];
TempSensorReadings[t][2]=TempSensorReadings[i][2];
TempSensorReadings[i][0]=temp[0];
TempSensorReadings[i][1]=temp[1];
TempSensorReadings[i][2]=temp[2];
}
}
}
Serial.print(F("\r\nHalf sorted list:"));
PrintSensorList();
//then sort by most important criteria, in this case bin ID number
for (byte i=0;i<NumTempSensors-1;i++){
for (byte t=i+1;t<NumTempSensors;t++){
if (TempSensorReadings[t][0]<TempSensorReadings[i][0]){
temp[0]=TempSensorReadings[t][0];
temp[1]=TempSensorReadings[t][1];
temp[2]=TempSensorReadings[t][2];
TempSensorReadings[t][0]=TempSensorReadings[i][0];
TempSensorReadings[t][1]=TempSensorReadings[i][1];
TempSensorReadings[t][2]=TempSensorReadings[i][2];
TempSensorReadings[i][0]=temp[0];
TempSensorReadings[i][1]=temp[1];
TempSensorReadings[i][2]=temp[2];
}
}
}
Serial.print(F("\r\nSorted list:"));
PrintSensorList();
}
void PrintSensorList(){
for (byte i=0;i<NumTempSensors;i++){
Serial.print(F("\r\nSensor "));
Serial.print(TempSensorReadings[i][0]);
Serial.print(F(" : "));
Serial.print(TempSensorReadings[i][1]);
Serial.print(F(" = "));
Serial.print(TempSensorReadings[i][2],DEC);
}
}
There are some strange, random happenings...usually just one or two sensors' temperature is corrupted, sometimes a few. One common theme I've observed is the second last sensor in the list often see's it's data corrupted, almost always during the second sort and often during the first sort too. Sorry for the long post, I'm not sure how else to explain my problem. Hopefully my entire post does not get quoted. I'm sure there's better ways of doing this.
Unsorted list:
Sensor 1 : 2 = 236
Sensor 1 : 3 = 240
Sensor 1 : 4 = 236
Sensor 1 : 5 = 236
Sensor 1 : 1 = 236
Sensor 1 : 6 = 261
Sensor 33 : 4 = 253
Sensor 33 : 6 = 253
Sensor 9 : 2 = 263
Sensor 9 : 1 = 265
Sensor 10 : 2 = 252
Sensor 9 : 3 = 260
Sensor 10 : 1 = 252
Sensor 10 : 3 = 231
Half sorted list:
Sensor 1 : 1 = 236
Sensor 9 : 1 = 9
Sensor 10 : 1 = 252
Sensor 1 : 2 = 236
Sensor 10 : 2 = 252
Sensor 9 : 2 = 7
Sensor 9 : 3 = 4
Sensor 1 : 3 = 240
Sensor 10 : 3 = 231
Sensor 1 : 4 = 236
Sensor 33 : 4 = 253
Sensor 1 : 5 = 236
Sensor 1 : 6 = 261
Sensor 33 : 6 = 253
Sorted list:
Sensor 1 : 1 = 236
Sensor 1 : 2 = 236
Sensor 1 : 3 = 240
Sensor 1 : 4 = 236
Sensor 1 : 5 = 236
Sensor 1 : 6 = 5
Sensor 9 : 2 = 7
Sensor 9 : 3 = 4
Sensor 9 : 1 = 9
Sensor 10 : 2 = 252
Sensor 10 : 1 = 252
Sensor 10 : 3 = 231
Sensor 33 : 4 = 253
Sensor 33 : 6 = 253
Sensor 1 : 2 = 236
Sensor 1 : 3 = 240
Sensor 1 : 4 = 236
Sensor 1 : 5 = 236
Sensor 1 : 1 = 236
Sensor 1 : 6 = 261
Sensor 33 : 4 = 252
Sensor 33 : 6 = 250
Sensor 9 : 2 = 251
Sensor 9 : 1 = 253
Sensor 10 : 2 = 251
Sensor 9 : 3 = 249
Sensor 10 : 1 = 251
Sensor 10 : 3 = 229
Half sorted list:
Sensor 1 : 1 = 236
Sensor 9 : 1 = 253
Sensor 10 : 1 = 251
Sensor 1 : 2 = 236
Sensor 10 : 2 = 251
Sensor 9 : 2 = 251
Sensor 9 : 3 = 249
Sensor 1 : 3 = 240
Sensor 10 : 3 = 229
Sensor 1 : 4 = 236
Sensor 33 : 4 = 252
Sensor 1 : 5 = 236
Sensor 1 : 6 = 261
Sensor 33 : 6 = 250
Sorted list:
Sensor 1 : 1 = 236
Sensor 1 : 2 = 236
Sensor 1 : 3 = 240
Sensor 1 : 4 = 236
Sensor 1 : 5 = 236
Sensor 1 : 6 = 5
Sensor 9 : 2 = 251
Sensor 9 : 3 = 249
Sensor 9 : 1 = 253
Sensor 10 : 2 = 251
Sensor 10 : 1 = 251
Sensor 10 : 3 = 229
Sensor 33 : 4 = 252
Sensor 33 : 6 = 250