OK here is pretty much my code:

Code:

#include <hidef.h> //common defines and macros
#include "derivative.h" //derivative-specific definitions
#include "ADC.h" //allows convenient use of ADC
#include <assert.h>
void DisSensors(int SampleSize, int OnThresh, int OffThresh, int* result, size_t size);
void main(void)
{
int DS[3]; //Distance Sensors
int PS1=0, PS2=0, PS3=0, PS4=0; //Photo sensors
int i;
int average=0;
EnableInterrupts;
ADCInit(); //initialises the ADC before first use
/* delay(5000); // 5 second delay before start of the round NEEDS ACCURATE CALIBRATION */
DDRT = (0xFF);
PTT = (0x00);
for(;;)
{
DisSensors(10, 100, 70, DS,sizeof(DS)/ sizeof(DS[0]) );
} /* loop forever */
/* please make sure that you never leave main */
}
void DisSensors(int SampleSize, int OnThresh, int OffThresh, int* result, size_t size)
{
int i;
assert(size >= 4);
result[0] = 0;
result[1] = 0;
result[2] = 0;
result[3] = 0;
for(i=1;i<=SampleSize;i++)
{
ADCDoConversions(); // trigger convertion
result[0] = result[0] + ATDDR2;
result[1] = result[1] + ATDDR3;
result[2] = result[2] + ATDDR4;
result[3] = result[3] + ATDDR5;
}
result[0] = result[0]/SampleSize;
result[1] = result[1]/SampleSize;
result[2] = result[2]/SampleSize;
result[3] = result[3]/SampleSize;
for(i=0;i<=0;i++)
{
if(result[i] >= OnThresh)
PTT = 0x01;//result[i] = TRUE;
else if(result[i]<=OffThresh)
PTT = 0x00;//result[i] = FALSE;
}
}