Hi this is currently my first post so i dont know if i am doing this right.
This is the code i am working on it is for testing the voltage of 16 different analog inputs.
const int inputpins[16] = {A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15}; // define the pins being measured
int pin_count = sizeof(inputpins)/sizeof(inputpins[0]); // Count the number of pins
const int numReadings = 5; // value to determine the size of the readings array.
const int numChannels = 16;
int readings[numChannels][numReadings]; // the readings from the analog input
int index; // the index of the current reading
int total [numChannels]; // the running total
int average [numChannels]; // the average
float Vcc = 5.01; // Variable to match the analogReference, this is added mostly for design
void setup() {
analogReference(DEFAULT);
// analogReference(INTERNAL2V56); // Use interal 2.56v ref
Serial.begin(9600);
for (int pin = 0; pin < pin_count; pin++){
Serial.print(" | ");
Serial.print("A");
Serial.print(inputpins[pin] - A0);
Serial.print(" | ");
}
Serial.println();
}
void loop() {
String values;
for (int pin = 0; pin < pin_count;pin++){
float V = analogRead(inputpins[pin])*(Vcc/1023.0); // read voltage from pin (have to convert the string entry into 'int' first by removing the 0)
values += "|";
values += V;
values += "| "; // append the readings to the string of values followed by a "|"
}
for (int chan = 0; chan < numChannels; ++chan ) {
total[chan] = total[chan] - readings[chan][index];
for (int pin = 0; pin < pin_count;pin++){
total[pin] += analogRead(inputpins[pin])*(Vcc/1023.0); // read voltage from pin (have to convert the string entry into 'int' first by removing the 0)
total[chan] = total[pin];
}
// total[chan] +=analogRead(inputpins[chan])*(Vcc/1023.0);
// total[chan] = total[chan] + readings[chan][index];
index = index + 1; // advance to the next position in the array
if (index >= numReadings){ // if we’re at the end of the array
index = 0; //wrap around to the beginning
}
average[chan] = total[chan] / 5; //Averages the readings
}
for (int pin = 0; pin < pin_count; pin++){
Serial.print(" | ");
Serial.print("A");
Serial.print(inputpins[pin] - A0);
Serial.print(" | ");
}
Serial.println();
delay(1); // delay in between reads for stability
Serial.print(values); // send values to serial
Serial.println();
delay(500); // delay in between reads for stability
for (int pin = 0; pin < pin_count; pin++){
Serial.print(" Sum:A");
Serial.print(inputpins[pin] - A0);
}
Serial.println();
for (int chan = 0; chan < numChannels; ++chan ) {
Serial.print(" | ");
Serial.print(average[chan]);
Serial.print(" | ");
}
Serial.println();
delay(1); // delay in between reads for stability
}
The issue i am having is with:
for (int chan = 0; chan < numChannels; ++chan ) {
total[chan] = total[chan] - readings[chan][index];
for (int pin = 0; pin < pin_count;pin++){
total[pin] += analogRead(inputpins[pin])*(Vcc/1023.0); // read voltage from pin (have to convert the string entry into 'int' first by removing the 0)
total[chan] = total[pin];
}
// total[chan] +=analogRead(inputpins[chan])*(Vcc/1023.0);
// total[chan] = total[chan] + readings[chan][index];
index = index + 1; // advance to the next position in the array
if (index >= numReadings){ // if we’re at the end of the array
index = 0; //wrap around to the beginning
}
average[chan] = total[chan] / 5; //Averages the readings
}
I have looked at the smoothing example from arduino but the total[chan] just keeps growing therefore i think that the - readings[chan][index] but i am having a hard time figuring it out. Can anyone tell me what i am doing wrong ?
On the serial monitor sum keeps growing:
|1.18| |1.27| |1.25| |1.23| |1.16| |1.11| |1.11| |1.07| |1.05| |1.01| |1.01| |0.97| |0.99| |0.95| |0.96| |0.94|
Sum:A0 Sum:A1 Sum:A2 Sum:A3 Sum:A4 Sum:A5 Sum:A6 Sum:A7 Sum:A8 Sum:A9 Sum:A10 Sum:A11 Sum:A12 Sum:A13 Sum:A14 Sum:A15
| 6 | | 6 | | 6 | | 6 | | 6 | | 7 | | 7 | | 7 | | 7 | | 7 | | 7 | | 7 | | 7 | | 7 | | 7 | | 7 |
| A0 | | A1 | | A2 | | A3 | | A4 | | A5 | | A6 | | A7 | | A8 | | A9 | | A10 | | A11 | | A12 | | A13 | | A14 | | A15 |
|1.48| |1.58| |1.56| |1.54| |1.47| |1.42| |1.40| |1.31| |1.26| |1.24| |1.24| |1.21| |1.22| |1.17| |1.16| |1.07|
Sum:A0 Sum:A1 Sum:A2 Sum:A3 Sum:A4 Sum:A5 Sum:A6 Sum:A7 Sum:A8 Sum:A9 Sum:A10 Sum:A11 Sum:A12 Sum:A13 Sum:A14 Sum:A15
| 7 | | 7 | | 7 | | 7 | | 8 | | 8 | | 8 | | 8 | | 8 | | 8 | | 8 | | 8 | | 8 | | 8 | | 8 | | 8 |
| A0 | | A1 | | A2 | | A3 | | A4 | | A5 | | A6 | | A7 | | A8 | | A9 | | A10 | | A11 | | A12 | | A13 | | A14 | | A15 |
|1.58| |1.69| |1.71| |1.72| |1.68| |1.67| |1.68| |1.63| |1.64| |1.65| |1.67| |1.63| |1.65| |1.58| |1.56| |1.43|
Sum:A0 Sum:A1 Sum:A2 Sum:A3 Sum:A4 Sum:A5 Sum:A6 Sum:A7 Sum:A8 Sum:A9 Sum:A10 Sum:A11 Sum:A12 Sum:A13 Sum:A14 Sum:A15
| 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 10 | | 10 |
| A0 | | A1 | | A2 | | A3 | | A4 | | A5 | | A6 | | A7 | | A8 | | A9 | | A10 | | A11 | | A12 | | A13 | | A14 | | A15 |
|1.57| |1.68| |1.69| |1.70| |1.67| |1.66| |1.68| |1.67| |1.71| |1.72| |1.74| |1.72| |1.74| |1.68| |1.66| |1.53|
Sum:A0 Sum:A1 Sum:A2 Sum:A3 Sum:A4 Sum:A5 Sum:A6 Sum:A7 Sum:A8 Sum:A9 Sum:A10 Sum:A11 Sum:A12 Sum:A13 Sum:A14 Sum:A15
| 10 | | 10 | | 10 | | 10 | | 10 | | 10 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 12 |