Buy an Axon, Axon II, or Axon Mote and build a great robot, while helping to support SoR.
0 Members and 1 Guest are viewing this topic.
#include <pic.h>__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \ & UNPROTECT & BORDIS & IESODIS & FCMDIS);main(){ PORTA = 0; CMCON0 = 7; // Turn off Comparators ANSEL = 0b0000011; // Defining Analog inputs (RA0 and RA1) ADCON0 = 0b00000001; // Turn on the ADC ADCON1 = 0b00010000; // Select the Clock as Fosc/8 (4MHz) TRISA = 0b001011; while(1) { if statement goes here...
I can give you a general idea.Use the code that you posted above. You will also need to declare some variables there.So you will need to declare :threshhold //you will want to preset a value for this. The value depends on how sensitive to change the robot is.leftreadingrightreadingthen before the if statement you need to read the left sensor and store the value in the leftreading variable and the same with the right.then the if statements.
//set up the program as you did above.unsigned int leftReading;unsigned int rightReading;unsigned int threshhold = 150; //start with this value and adjust as necessarrywhile (1){ //then begin the infinite while loop here
//assume the left sensor is plugged into ad0ADCON0 = 0b00000001;// x_______ 0 = left justified / 1 = right justified // _x______ 0 = voltage reference from vdd / 1 voltage reference from aref pin// __x_____ not configured (always set to 0)// ___xxx__ Adress of pins to read (000 = an0 / 001 = an1)// ______x_ GO/Done Bit// _______x adc enable bit 1 = on / 0 = off (consumes no power in off state)//the commenting above is useful to use. Once its put into code you dont have to keep referencing the datasheet.// the above part selects the AD0 pin and makes sure the a/d module is turned on.//the ADCON1 register just sets the speed of aquisition... set it up like you did originally and change it. //The aquisition speed can affect the reading.//Now we need to tell the a/d module to take a reading. ADIE = 0; //prevent a global interrupt on aquisition ADIF = 0; //reset the a/d interrupt flag ADRESL = 0; //reset the a/d low bits register ADRESH = 0; //reset the a/d high bits register ADGO = 1; //tell the a/d to take a reading// The reading doesnt happen instantly, it takes a little bit of time so we need to put a small while loop in...while(!ADIF){ //while the interrupt flag is cleared// do nothing}// yay, the interrupt flag has been set to 1, the reading is complete!!!//now we need to put the reading into our variable. The reading is stored in 2 seperate registers.//its a 10 bit module and the registers are only 8 bits wide so....leftReading = ADRESL; //ADRESL is where the lower 8 bits areleftReading += (ADRESH << 8); //ADRESH is where the higher 2 bits are so we needed to shift them 8 bits left//so now we have the left reading value. The right reading below is the same without commenting.ADCON0 = 0b00000101; //notice the value here has changed so we are now setting ad0ADIE = 0; ADIF = 0; ADRESL = 0; ADRESH = 0; ADGO = 1; while(!ADIF){ }rightReading = ADRESL;rightReading += (ADRESH << 8);//Now you can go ahead with the if statements.....