Hi all
I am a bit new to Arduino, so the other day I was trying planted the code on a robot (already made) having 5 IR sensors attached in different directions for movement. I am following some code from a class I took; so far I am not able to figure out this part: (the ones with //****)
int maxRead; // The highest IR reading.
int maxIndex; // The index (sensor number) of the highest IR reading.
if(!timeup){
// Reset tracking variables.
minRead = 1023;
maxRead = -1;
maxIndex = -1;
// Loop through each sensor to obtain the readings.
for(i=0; i<5; i++){
irRead = 1023 - analogRead(irPin_); // ** if( irRead<minRead ){ // _ _minRead = irRead; } if( irRead>maxRead ){ // maxIndex = i;_ _maxRead = irRead; } }_
The bit you might not be understanding is the irPin bit. This uses an array of numbers that define what pin to use for the analogue pins. You don't post all the code so you will have missed out the bit where it gets set up. But in this code it is largely redundant because you could equally well use analogRead(i); which reads the analogue pin given by the value of the variable i. As these values of i are 0 to 5 and the values of the analogue pins are the same. If however you had say a Mega and were using higher numbers of analogue in then:- irPin[] = { 6, 7, 8, 9, 10 } would allow the code:- analogRead(irPin*);* to access these higher codes. Look up about arrays.