 NewPing sonar4(Ping4, Ping4, 200);
 
 //global variables
-int boxcar[MovingAverageWidth];
-int boxcar_index = 0;
+int boxcar[MovingAverageWidth*NoAverageDataSets];
+int boxcar_index[NoAverageDataSets];
 
 void setup() {
   

  pinMode(SharpIrEnable, OUTPUT);
  pinMode(LEDRed, OUTPUT);
  
  Serial.begin(9600);  
  
  //ini the moving average array
  for(int i = 0; i < MovingAverageWidth; i++){      
      boxcar_addValue(analogRead(LDRTop));
      delay(2);
  }
  
}


void loop(){
    boxcar_addValue(analogRead(LDRTop));
    Serial.print("LDRTop = ");
    Serial.println(boxcar_getAverage());
    sensitivity();
    delay(100);
}

void sensitivity(){
    int brightness = boxcar_getAverage();
    if ( brightness < Dark){
        motor_basic(0);
    }else if(brightness > Light){
        motor_basic(2);
    }else{
        motor_basic(1);
    }
        
    
}

void remote(int val) {
  switch (val) {

}    


void boxcar_addValue(int add,int set){
    int offset = set*MovingAverageWidth;
    if(boxcar_index[set] == MovingAverageWidth+offset){
        boxcar_index[set] = 0;
        boxcar[boxcar_index[set]+offset] = add/MovingAverageWidth;
    } else{
        boxcar_index[set]++;
        boxcar[boxcar_index[set]+offset] = add/MovingAverageWidth;
    }    
}
        

int boxcar_getAverage(int set) {
    int mean = 0;
    int offset = set*MovingAverageWidth;
    for (int i =0; i < MovingAverageWidth; i++){
        mean = mean + boxcar[i+offset];       
    }    
    return mean;
}
