Hello.
I am collecting samples from four sensors, placing them in a 2 dimensional array ReadingArray. The for loop sets the current to the PCB and then the inner loop collects samples for each. Each time the arrays are built I then call the average() function. I would expect the printlnf in average to print four values per call of average(). Instead I see what you see below…
Some definitions:
global
#define NUMBER_SAMPLES 10//80
#define NUMBER_SENSORS 4
uint16_t ReadingArray[4][80];
uint8_t readCycle = 0;
uint8_t current = 0;
I have the following code block in a state machine that lives in the loop:
case READING:
for (current = 0; current < 4; current++){
DAC_Load_Value(dacValues[current]);
for (uint8_t i = 0; i < NUMBER_SAMPLES; i++){
ReadingArray[0][i] = analogRead(Vbias);
ReadingArray[1][i] = analogRead(SiPM_current);
ReadingArray[2][i] = analogRead(LED1_current);
ReadingArray[3][i] = analogRead(LED2_current);
}
SensorData.w_temp[readCycle][current] = ReadWaterTemp();
SensorData.bd_temp[readCycle][current] = ReadIntTemp();
average();
}
readCycle++;
sm = SOLENOID_POWER;
break;
The function average() …see below:
void average(){
unsigned long sum = 0;
float avg = 0;
Serial.printlnf("%d --- Cycle, %d ---- Current", readCycle, current);
for (uint8_t sensor = 0; sensor < NUMBER_SENSORS; sensor++){
for(uint8_t sample = 0; sample < NUMBER_SAMPLES; sample++){
sum += ReadingArray[sensor][sample];
}
avg = ((float)(sum))/NUMBER_SAMPLES * 3.3/4096;
sum = 0;
switch (sensor){
case 0:
SensorData.Vbias[readCycle][sensor] = avg;
break;
case 1:
SensorData.SiPMCurrent[readCycle][sensor] = avg;
break;
case 2:
SensorData.LEDCurrent[readCycle][0][sensor] = avg;
break;
case 3:
SensorData.LEDCurrent[readCycle][1][sensor] = avg;
break;
default:
break;
}
}
for (uint8_t sensor = 0; sensor < NUMBER_SENSORS; sensor++){
Serial.printlnf("%.2f", SensorData.Vbias[readCycle][sensor]);
Serial.printlnf("%.2f", SensorData.SiPMCurrent[readCycle][sensor]);
Serial.printlnf("%.2f", SensorData.LEDCurrent[readCycle][0][sensor]);
Serial.printlnf("%.2f", SensorData.LEDCurrent[readCycle][1][sensor]);
}
}
The printout I receive looks like the following:
0 — Cycle, 0 ---- Current
2.36
0.00
0.00
0.00
0.00
3.19
0.00
0.00
0.00
0.00
0.10
0.00
0 — Cycle, 1 ---- Current
2.36
0.00
0.00
0.00
0.00
3.18
0.00
0.00
0.00
0.00
0.00
0.00
0 — Cycle, 2 ---- Current
2.36
0.00
0.00
0.00
0.00
3.18
0.00
0.00
0.00
0.00
1.21
0.00
0 — Cycle, 3 ---- Current
2.36
0.00
0.00
0.00
0.00
3.18
0.00
0.00
0.00
0.00
0.76
0.00