Ejemplo n.º 1
0
unsigned int NewPing::ping() {
	ping_trigger();                   // Trigger a ping.
	if (!ping_wait()) return NO_ECHO; // Wait for ping to start, if it returns false, return NO_ECHO to the calling function.

	while (*_echoInput & _echoBit)                      // Wait for the ping echo.
		if (micros() > _max_time) return NO_ECHO;       // Stop the loop and return NO_ECHO (false) if we're beyond the set maximum distance.
		return (micros() - (_max_time - _maxEchoTime) - 5); // Calculate ping time, 5uS of overhead.
}
Ejemplo n.º 2
0
unsigned int NewPing::ping() {
	if (!ping_trigger()) return NO_ECHO; // Trigger a ping, if it returns false, return NO_ECHO to the calling function.

#if URM37_ENABLED == true
	while (!(*_echoInput & _echoBit))             // Wait for the ping echo.
		if (micros() > _max_time) return NO_ECHO;   // Stop the loop and return NO_ECHO (false) if we're beyond the set maximum distance.
#else
	while (*_echoInput & _echoBit)                // Wait for the ping echo.
		if (micros() > _max_time) return NO_ECHO;   // Stop the loop and return NO_ECHO (false) if we're beyond the set maximum distance.
#endif

	return (micros() - (_max_time - _maxEchoTime) - PING_OVERHEAD); // Calculate ping time, include overhead.
}
Ejemplo n.º 3
0
Archivo: main.c Proyecto: alohr/avr
unsigned long ping(int max_cm_distance)
{
    unsigned long t0 = 0;

    // Calculate the maximum distance in uS.
    if (max_cm_distance > MAX_SENSOR_DISTANCE_CM)
	max_cm_distance = MAX_SENSOR_DISTANCE_CM;

    unsigned long tmax =
	max_cm_distance * ROUNDTRIP_CM_US + (ROUNDTRIP_CM_US / 2);

    if ((t0 = ping_trigger()) <= 10)
	return ULONG_MAX;

    while (bit_is_set(PINB, PB4)) {
    	if (micros() - t0 > tmax)
    	    return ULONG_MAX;
    }

    // Calculate ping time, 5uS of overhead.
    return (micros() - t0 - 5);
}
Ejemplo n.º 4
0
unsigned int NewPing::ping(unsigned int max_cm_distance) {
	if (max_cm_distance > 0) set_max_distance(max_cm_distance); // Call function to set a new max sensor distance.

	if (!ping_trigger()) return NO_ECHO; // Trigger a ping, if it returns false, return NO_ECHO to the calling function.

#if URM37_ENABLED == true
	#if DO_BITWISE == true
		while (!(*_echoInput & _echoBit))             // Wait for the ping echo.
	#else
		while (!digitalRead(_echoPin))                // Wait for the ping echo.
	#endif
			if (micros() > _max_time) return NO_ECHO; // Stop the loop and return NO_ECHO (false) if we're beyond the set maximum distance.
#else
	#if DO_BITWISE == true
		while (*_echoInput & _echoBit)                // Wait for the ping echo.
	#else
		while (digitalRead(_echoPin))                 // Wait for the ping echo.
	#endif
			if (micros() > _max_time) return NO_ECHO; // Stop the loop and return NO_ECHO (false) if we're beyond the set maximum distance.
#endif

	return (micros() - (_max_time - _maxEchoTime) - PING_OVERHEAD); // Calculate ping time, include overhead.
}
Ejemplo n.º 5
0
void NewPing::ping_timer(void (*userFunc)(void), unsigned int updateInterval_us,unsigned int maxTime_us) {
	if (!ping_trigger(maxTime_us)) return;         // Trigger a ping, if it returns false, return without starting the echo timer.
	timer_us(updateInterval_us, userFunc); // Set ping echo timer check every ECHO_TIMER_FREQ uS.
    _isRunning=true;
}
Ejemplo n.º 6
0
	void NewPing::ping_timer(void (*userFunc)(void), unsigned int max_cm_distance) {
		if (max_cm_distance > 0) set_max_distance(max_cm_distance); // Call function to set a new max sensor distance.

		if (!ping_trigger()) return;         // Trigger a ping, if it returns false, return without starting the echo timer.
		timer_us(ECHO_TIMER_FREQ, userFunc); // Set ping echo timer check every ECHO_TIMER_FREQ uS.
	}
Ejemplo n.º 7
0
void NewPing::ping_timer(void (*userFunc)(void)) {
	ping_trigger();                      // Trigger a ping.
	if (!ping_wait()) return;            // Wait for ping to start, if it returns false, return without starting the echo timer.
	timer_us(ECHO_TIMER_FREQ, userFunc); // Set ping echo timer check every ECHO_TIMER_FREQ uS.
}