Avergaing Two Potentiometers Help

Hello, I am an engineering student at westhill high school and we are working towards averaging the outputs of 4 temperature probes so our first step was to learn how to average two potentiometers and this is causing us several problems. We are trying to use a smoothing code to do this but are relatively new to Arduino. Any help is greatly appreciated!
This is my code:

``````onst int numReadings = 10;

int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int averageA = 0;                // the average

int inputPin = A0;

void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
}

void loop() {
// advance to the next position in the array:
index = index + 1;

// if we're at the end of the array...
// ...wrap around to the beginning:
index = 0;

// calculate the average:
// send it to the computer as ASCII digits
Serial.println(averageA);
delay(1);        // delay in between reads for stability
value1: averageA

int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int averageB = 0;                // the average

int inputPin = A1;

void setup()

// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
}

void loop() {
// advance to the next position in the array:
index = index + 1;

// if we're at the end of the array...
// ...wrap around to the beginning:
index = 0;

// calculate the average:
// send it to the computer as ASCII digits
Serial.println(averageB);
delay(1);        // delay in between reads for stability

value2: averageB

value3:(averageA + averageB)

Serial.println(value3)
``````
``````average_smooth_sketch_1.ino: In function 'void loop()':
average_smooth_sketch_1:57: error: expected initializer before 'Serial'
average_smooth_sketch_1.ino: In function 'void loop()':
average_smooth_sketch_1:63: error: redefinition of 'void loop()'
average_smooth_sketch_1:23: error: 'void loop()' previously defined here
average_smooth_sketch_1:79: error: 'averageB' was not declared in this scope
average_smooth_sketch_1:86: error: expected `;' before 'value3'
average_smooth_sketch_1:88: error: expected `}' at end of input
``````

``````value1: averageA

``````

Explain what you think this is doing.

Is there some part of having two (incomplete) loop() functions that is hard to understand. Is there some part of that not being allowed that is hard to understand?

``````value2: averageB

value3:(averageA + averageB)
``````

What is this stuff supposed to do?

Looks as if you have accidentally posted two copies of your code - I suggest you edit your post to correct it.

From your description I expected you to be trying to average the readings from four inputs (or two inputs). What that code is doing is averaging ten consecutive readings from a single input. It looks like a credible implementation of that, but is no use to you if you're trying to average the inputs from multiple sensors. Can you clarify what you're trying to achieve?

It actually looks like you have copied some code to average the reading of one sensor. Then copied it again and tried to sum the average. That isn't going to work for obvious reasons.

You cannot just copy that same code twice and change the pin input variable and expect it to work.

I also have to wonder what, if anything, you understand about programming in C. Certainly you cannot have two functions called loop. You can have two loop functions, but they both cannot be named loop.(Not that what you wish to do requires more than one anyway.)

All variables need to be initialized at once. Not twice, not three times, one time.

Declared variables

``````const int numReadings = 10;
``````

Probably better if the comments clearly state that this is a set of data. It's clear to people who already understand. Any teacher is going to want to know that you understand this. Remember that you need to store two setts of data though. Not just one. How will you accomplish this with this array? Add another dimension to the array is what I would do.

``````int index = 0;                          // the index of the current reading
``````

Same here. How will you differentiate the array index of the other set of data? I feel I could go into arrays and their many uses, but it wouldn't do any good.

``````int total = 0;                           // the running total
int averageA = 0;                     // the average
int inputPin = A0;
``````

This is all much the same. All of the variables for each set of data that corresponds to the analog input you are reading needs to be handled in turn. You will have more than one running total, average, input pin, Etc.

I am beginning to get a mental picture of this situation. I dearly hope I'm wrong and that you haven't been squandering your learning opportunity. I believe you have finally come to a point in the lesson plan where you can't just copy pasta your way to victory. From the state of what I see before me, you need to begin remedial learning and start back with the basics of programming. I would say your best course of action is to start from the beginning with a tutor.

Manipulating data with arrays is something that you will do a lot in computer programming. You can't expect us here to teach you in text how to program anymore than you can expect to learn it through osmosis.

CoolJ: It actually looks like you have copied some code to average the reading of one sensor. Then copied it again and tried to sum the average. That isn't going to work for obvious reasons.

You cannot just copy that same code twice and change the pin input variable and expect it to work.

I also have to wonder what, if anything, you understand about programming in C. Certainly you cannot have two functions called loop. You can have two loop functions, but they both cannot be named loop.(Not that what you wish to do requires more than one anyway.)

All variables need to be initialized at once. Not twice, not three times, one time.

Declared variables

``````const int numReadings = 10;
``````

Probably better if the comments clearly state that this is a set of data. It's clear to people who already understand. Any teacher is going to want to know that you understand this. Remember that you need to store two setts of data though. Not just one. How will you accomplish this with this array? Add another dimension to the array is what I would do.

``````int index = 0;                          // the index of the current reading
``````

Same here. How will you differentiate the array index of the other set of data? I feel I could go into arrays and their many uses, but it wouldn't do any good.

``````int total = 0;                           // the running total
int averageA = 0;                     // the average
int inputPin = A0;
``````

This is all much the same. All of the variables for each set of data that corresponds to the analog input you are reading needs to be handled in turn. You will have more than one running total, average, input pin, Etc.

I am beginning to get a mental picture of this situation. I dearly hope I'm wrong and that you haven't been squandering your learning opportunity. I believe you have finally come to a point in the lesson plan where you can't just copy pasta your way to victory. From the state of what I see before me, you need to begin remedial learning and start back with the basics of programming. I would say your best course of action is to start from the beginning with a tutor.

Manipulating data with arrays is something that you will do a lot in computer programming. You can't expect us here to teach you in text how to program anymore than you can expect to learn it through osmosis.

Thanks for the input and I'm sorry for acting like that is the way I am but I am just frustrated trying to learn Arduino and my teacher suggested to ask the forum for help. I know that I am trying to "copy and paste my way to victory" in this situation but I would just like to let you know that this in no way describes my work ethic as a future electrical engineer. Thank you again for your criticism I am going to work harder to learn Arduino and C programming methods.

I’d concentrate on averaging for just one input, and get that working.

The thing is, there are lots of ways of skinning this particular cat, and also lots of opportunities to learn.
You could factor the code for a single input into a function, and pass the function a pin number, or an input index.
You could decide that both inputs will be averaged over the same length, so a single “index” will work.

``````for (int thisReading = 0; thisReading < numReadings; thisReading++)
``````

is pointless; the compiler already thoughtfully initialised that global array to all zeroes for you.
``````int readings[numReadings];      // the readings from the analog input