//********************************************** // IR Read Routine // version 1.0 // reads three IR sensors and returns a signed int // telling the turret which way to turn, and fast or // slow, or stop. (-2,-1,0,1, or 2) // //********************************************** //libraries #include#include "ad_lib.h" //global variables #define AD_Chan1 1 #define AD_Chan2 2 #define AD_Chan3 3 #define DEBUG 1 //to get debugging output, set to 1 and uncomment appropriate lines int PrevMaxSense = 0; //void main(void) signed int IR_read(void) { // declare variables (not global unless listed before "main".) int IR_sense1 = 0, IR_sense2 = 0, IR_sense3 = 0; int IR_sense1a = 0, IR_sense2a = 0, IR_sense3a = 0; int IR_sense1b = 0, IR_sense2b = 0, IR_sense3b = 0; int IR_sense1c = 0, IR_sense2c = 0, IR_sense3c = 0; int MaxSense = 0; unsigned char IRcounter = 0; signed int rotation = 0; //AD_Init(); //NOTE: commented out to be module in larger program //while (1) { //NOTE: commented out to be module in larger program IR_sense1a = AD_Read(AD_Chan1); //taking 3 samples of each sensor, to average IR_sense2a = AD_Read(AD_Chan2); IR_sense3a = AD_Read(AD_Chan3); IR_sense1b = AD_Read(AD_Chan1); IR_sense2b = AD_Read(AD_Chan2); IR_sense3b = AD_Read(AD_Chan3); IR_sense1c = AD_Read(AD_Chan1); IR_sense2c = AD_Read(AD_Chan2); IR_sense3c = AD_Read(AD_Chan3); IR_sense1 = (IR_sense1a + IR_sense1b + IR_sense1c)/3; IR_sense2 = 1.2*(IR_sense2a + IR_sense2b + IR_sense2c)/3; //the 1.1 is to keep the robot from jittering. IR_sense3 = (IR_sense3a + IR_sense3b + IR_sense3c)/3; if ((IR_sense1 > IR_sense2) && (IR_sense1 > IR_sense3)) { MaxSense = 1; PrevMaxSense = MaxSense;} else if ((IR_sense2 > IR_sense1) && (IR_sense2 > IR_sense3)) { MaxSense = 2; PrevMaxSense = MaxSense;} else if ((IR_sense3 > IR_sense1) && (IR_sense3 > IR_sense2)) { MaxSense = 3; PrevMaxSense = MaxSense;} else { MaxSense = PrevMaxSense; }; //default case for no sensors, or 2 sensors equal: it gives pevious MaxSense reading. if (DEBUG) { printf ("\n IR_sense1= %d", IR_sense1); printf (" IR_sense2= %d", IR_sense2); printf (" IR_sense3= %d", IR_sense3); printf (" MaxSense= %d\n", MaxSense); } if (MaxSense == 1) { rotation = 2;} else if (MaxSense == 2) { rotation = 0;} else if (MaxSense == 3) { rotation = -2;} else { rotation = 77; printf("\nbad MaxSense value in IR_Read");} if (DEBUG) printf ("rotation= %d\n", rotation); return rotation; // } //bracket for while loop //end }