I am fairly new to programming, specifically using arrays to store values and condense code.
I am working on putting together a code that averages 5 readings for each of 10 different sensors and want to make sure my logic is making sense. The code is below.
What I want it to do is take 2 IR readings ("ambient" and "obstacle") and calculate another variable ("distance"). This will happen 5 times, then the 5 readings are averaged and stored in a single array (distance[10]) for each sensor. Using a 10 value array where each stored value is referenced in another code and each location is associated with a single pod. (i.e. location 0=pod 1, location 1=pod 2.....)
This code is just a portion of a much larger code that uses the values for each pod to control lights associated with each sensor.
Also, when I say pod, it is in reference to the light and IR combined circuit. See attached PCB design.
Please feel free to comment or ask questions.
Thanks in advance!
Equipment: Arduino Mega2560, TCRT5000 sensors
//=========================//
//=====IR Sensor Setup=====//
//=========================//
int ambient[5]; // array to store the IR coming from the ambient
int obstacle[5]; // array to store the IR coming from the obstacle
int distance[10]; // array to store if a cup is on each pod
int podIR[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9}; //defines pin to retrieve IR value from for each pod
int CupCount[10]; //Still trying to figure out how to get a count of the pods that have an object on them. This line is not used at this point.
#define IRemitters 2 //IR emitters on digital pin 2. All 10 pods IR emitters are controlled by this one line
void setup()
{
Serial.begin(9600); // initializing Serial monitor
pinMode(IRemitters,OUTPUT); //IRemitters (digital pin 2) as output
digitalWrite(IRemitters,LOW); //Turns IR LEDs off
void loop()
{
Serial.println(distance[0]); // writing value of pod 1 for debugging
for(int y=0; y<10; y++){ //Loops through all 10 pods
for(int x=0; x<5; x++){ //Collects 5 readings from sensor for averaging
digitalWrite(IRemitters,LOW); // turning the IR LEDs off to read the IR coming from the ambient
ambient[x] = analogRead(podIR[y]); // storing IR coming from the ambient
digitalWrite(IRemitters,HIGH); // turning the IR LEDs on to read the IR coming from the obstacle
obstacle[x] = analogRead(podIR[y]); // storing IR coming from the obstacle
distance[y] += ambient[x]-obstacle[x]; // calculating changes in IR values and storing it for future average
}
distance[y] /= 5; //average of 5 readings from each pod
}
}
PCB_LED Pods w silkscreen.pdf (13 KB)