question about averaging sensor inputs

I put this program together but I am concerned aboutif it will correctly average sensor readings from 4 potentiometers in separate indexes. I need it to average the first then act on the reading, move onto the second and so forth. Does it look like it will work correctly?

edited 1-11-12 new code

const int numReadings = 10;
 
int lfindex[numReadings];      // the readings from the analog input
 int index = 0;                  // the index of the current reading
 int total = 0;                  // the running total
 int average = 0;                // the average
 
 int rfindex[numReadings];                 
 int total1 = 0;
 int lrindex[numReadings];        
 int total2 = 0;
int rrindex[numReadings];     
 int total3 = 0;
 int index1 = 0;
int index2 = 0;
int index3 = 0;
int average1 = 0;
int average2 = 0;
int average3 = 0;

// pin assignments
int switchpin = 4;
int lfin = A0;
int rfin = A1;
int lrin = A2;
int rrin = A3;
int man = A4;
int lfout = 3;
int rfout = 5;
int lrout = 6;
int rrout = 9;

// variables start at zero

int switchstate = 0;
int lfpwm = 0;
int rfpwm = 0;
int lrpwm = 0;
int rrpwm = 0;
float manualfactor = 0;

void setup()
 {
 
   Serial.begin(9600);                   
 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
     lfindex[thisReading] = 0; 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
     rfindex[thisReading] = 0;
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
     lrindex[thisReading] = 0;
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
     rrindex[thisReading] = 0;
     
pinMode (lfout, OUTPUT);
pinMode (rfout, OUTPUT);
pinMode (lrout, OUTPUT);
pinMode (rrout, OUTPUT);
pinMode (switchpin, INPUT);
pinMode (lfin, INPUT);
pinMode (rfin, INPUT);
pinMode (lrin, INPUT);
pinMode (rrin, INPUT);
pinMode (man, INPUT);

     
 }
 
void loop() 
{
int manual = analogRead(man); // operator input potentiomter

manualfactor = float(manual) / 1023;
switchstate = digitalRead(switchpin); // operator on off switch

  total= total - lfindex[index];          
   lfindex[index] = analogRead(lfin); 
   total= total + lfindex[index];         
   index = index + 1;                    
   if (index >= numReadings)              
    index = 0;                           
   average = total / numReadings; 
   
  lfpwm = average * manualfactor / 4;
  
  if (switchstate == HIGH) {
  analogWrite (lfout, lfpwm); 
  }
  else {
  digitalWrite(lfout, LOW);
  }
  total1= total1 - rfindex[index1];          
   rfindex[index1] = analogRead(rfin); 
   total1= total1 + rfindex[index1];         
   index1 = index1 + 1;                    
   if (index1 >= numReadings)              
    index1 = 0;                           
   average1 = total1 / numReadings; 
   
   rfpwm = average1 * manualfactor / 4;
   
   if (switchstate == HIGH)   {
   analogWrite (rfout, rfpwm);
   }
   else {
  digitalWrite(rfout, LOW);
  }
  
   total2= total2 - lrindex[index2];          
   lrindex[index2] = analogRead(lrin); 
   total2= total2 + lrindex[index2];         
   index2 = index2 + 1;                    
   if (index2 >= numReadings)              
    index2 = 0;                           
   average2 = total2 / numReadings; 
  
  lrpwm = average2 * manualfactor / 4;
  
  if (switchstate == HIGH)   {
  analogWrite (lrout, lrpwm);
  }
  else {
  digitalWrite(lrout, LOW);
  }
  
  total3= total3 - rrindex[index3];          
   rrindex[index3] = analogRead(rrin); 
   total3= total3 + rrindex[index3];         
   index3 = index3 + 1;                    
   if (index3 >= numReadings)              
    index3 = 0;                           
   average3 = total3 / numReadings; 
 
 rrpwm = average3 * manualfactor / 4;
 
 if (switchstate == HIGH)  {
  analogWrite (rrout, rrpwm);
 }
 else {
  digitalWrite(rrout, LOW);
  }
 
  
  Serial.print(lfpwm);
  Serial.print("\t");
  Serial.print(rfpwm);
  Serial.print("\t");
  Serial.print(lrpwm);
  Serial.print("\t");
  Serial.print(rrpwm);
  Serial.print("\t" );
  Serial.print(manual);
  Serial.print("\t");
  Serial.print("");
  Serial.println("left front, right front, left rear, right rear, manual input");
  
  
}

Please modify your post, select the code and press the # button to get the proper code tags. Looks so much better!

manual = analogRead(man) / 1023; // operator input potentiomter

as the math is done in ints the result will allways be zero except when analogRead() returns 1023. Think you need to reconsider this code line.

You now use the var total for every array, while you should use the appropiate var e.g. total1 or total2 for the appropiate array.

Check my - Arduino Playground - Statistics - class for easier calculating averages of multiple values.

YOu write

lfpwm = average * manual / 4;
 
  if (switchstate == HIGH) {
  analogWrite (lfout, average * manual / 4);
  }
  else {
  digitalWrite(lfout, LOW);
  }

why not use the var you calculated? // but be aware that manual was allways zero in the first place ...

lfpwm = average * manual / 4;
 
  if (switchstate == HIGH) {
  analogWrite (lfout, lfpwm);
  }
  else {
  digitalWrite(lfout, LOW);
  }

or even shorter

analogWrite (lfout, switchstate * average * manual / 4);  // trick as  HIGH == 1 and LOW == 0

hope this helps you a step further

When you find yourself writing almost the same thing over and over again, think "function"

robtillaart:
Please modify your post, select the code and press the # button to get the proper code tags. Looks so much better!

Good advice, but the main reason to use [ code ] [ / code ] tags is to stop the forum software from munging the code and not just cosmetics. Since you have already posted it without the code tags, any munging has already happened. So, I suggest that you edit your post, delete the code that was in there, click on the '#' button to insert a pair of code tags and then paste a new copy of your code in between the tags.

OK edited first post.

added another variable name and these lines

maninput = analogRead(man)  // operator input potentiomter
manual = maninput / 1023;  // operator input potentiometer multiplier

lfpwm = average * manual / 4;

if (switchstate == HIGH) {
  analogWrite (lfout, average * manual / 4); 
  }
  else {
  digitalWrite(lfout, LOW);
  }

So what I want is to have the variable "manual" be a multiplier fraction. So if the operator pot is full up I should have a value of 1023/1023, and at 50% be about 512/1023 correct?

Or am I looking at it reversed?

From your first post:

int maninput = 0;
manual = maninput / 1023;  // operator input potentiometer multiplier

The manual variable will always contain either 0 or 1. Probably not what you want.

I guess I don't quite follow why my variable would remain 0. If I remove the variable from the code untill the analogread command will that change things?

As it was originally they were in this order

int manual = 0;

manual= analogRead(man) / 1023;

Should that not change the "manual" value from 0 to an (interger read from the analog pin "man" divided by 1023)? If not then what am I needing to change?

You're using integer division. AnalogRead will return a value between 0 and 1023. With integer division, 1023/1023 is 1, but for all other returned values, value/1023 will be zero. You could use float instead. Don't forget to cast the analogread value to float and make 1023 a float constant 1023.0.

  total3= total3 - rrindex[index];          
   rrindex[index] = analogRead(rrin); 
   total3= total3 + rrindex[index];         
   index = index + 1;                    
   if (index >= numReadings)              
    index = 0;

Looks like you need to review your use of "index".

I made some changes. I appreciate the help. This was the first programming I've done since back when I was a kid on a Commodore64 and Apple2.