Building A Robot: The Proximity Sensor

Program Download:

Main C File/Project Archive
Robot_Proximity_Sensor.c
Robot_Proximity_Sensor.zip

The Software
           The main portion of firmware that we will focus on is:

    -The Proximity Sensor A/D Evaluation



Compared to the first program from the motor control article, this program has become much more complex, adding a digital timer + interrupt to generate speaker tones and evaluating an analog voltage output from a proximity sensor. The code below shows a short version of how the proximity sensor a/d conversion happens and is evaluated to tell the robot what to do.

Proximity Sensor Analog-to-Digital Evaluation

------------« Begin Code »------------
//Turn On ADC - PIC PIN 2
OpenADC( ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_1ANA_0REF,
ADC_CH0 & ADC_INT_OFF );

//Turn On Timer0 with Interrupt, 16 bit mode
OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_1 );

while(1){

	Delay10TCYx( 5 ); // Delay for 50TCY
	ConvertADC(); // Start conversion
	while( BusyADC() ); // Wait for completion
	result = ReadADC(); // Read result

	if(result >= 384){	//Less Than 15cm Away
		PORTB = 0x06;
		T0CONbits.TMR0ON = 1;//enable_tone = 1;

		/*******Move Backward***********/
		SetDCPWM1(74);
		SetDCPWM2(74);

		PORTCbits.RC4 = 1;
		PORTCbits.RC5 = 1;
	
		//0.25 Second Delay
		Delay10KTCYx(250);
		/***************************/
		...
		....
		...
		/*******Full Stop***********/
		SetDCPWM1(38);
		SetDCPWM2(38);

		PORTCbits.RC4 = 0;
		PORTCbits.RC5 = 0;
		/***************************/

		T0CONbits.TMR0ON = 0;//enable_tone = 0;

	}
	else if(result < 384 && result > 128){	//Between 15-45cm Away
		PORTB = 0x06;
		T0CONbits.TMR0ON = 0;//enable_tone = 0;
	}
	else if(result <= 128){	//Greater Than 45cm Away
		PORTB = 0x09;
		T0CONbits.TMR0ON = 0;//	enable_tone = 0;
	}

}
------------« End Code »------------

           You can see the result variable holds the converted digital value and we then used the chart from the theory section to choose the values placed in the if/else statement (15cm vs 15-45cm vs 45cm+). There is also a substantial amount of firmware used to create the tone heard when the robot backs away, but that functionality in the robot is purely for entertainment. Get the proximity sensor working first, then move to make sure the speaker works.



;