Sensor

Having a bit of a problem two days now. I cannot get my Daventech ranger finder to work. I think its working but my lcd string doesn’t seem it wants to update.

logic for range finder:
1st - send a signal out for a minimum of 10us thorugh pd1.
2nd - When this happens if there is an object infront it will send the signal back to pd0.
**But all i see is “loop again on my lcd” see code. I took that line out and it said “Signal happened” which (indicates that a signal came back) over and over. Just to see if it works withou worring out distance calculator. Was just wondering if i am overlooking something.

#include <avr/io.h>
#include "LCD.h"
#include "SPI.h"
#include <util/delay.h>

#define F_CPU 1000000UL   // Orangutan frequency (2MHz)!!!

int main(void)
{

	SPIInit();			// allows the mega644 to send commands to the mega168
	LCDInit();

	DDRD &= ~(1 << PD0); //make PD0 an input to receive echo
	DDRD |= 1 << PD1; // PD1 an output to send signal
	
	while (1)
	{

		PORTD |= 1 << PD1; //turn on to produce pulse
		delay_us(15); //make 15 us elapse
		PORTD &= ~(1 << PD1); // clear

		while (!(PIND&(1<<PC0)))
		{
			LCDString("No signal has returned as yet");

		}

		LCDString("Signal happened");

		delay_ms(15); 

		LCDClear();

		if (!(PIND&(1<<PC0)))
		{
			LCDString("Signal yes it wasnt n e hoax!");
			delay_ms(1000);
		}
			LCDString("Loop again");
			delay_ms(1000);
	}
}

I also attached a picture to show how the signals work graphically


It sounds like you’re using an Orangutan X2, is that correct? It would also be helpful to know specifically which Devantech rangefinder you’re trying to use, and how it is wired to your Orangutan.

Anyway, there are a couple of confusing things about your code. First, although bare ATMega644 chips ship with their fuses set for an internal system clock frequency of 1MHz, the Orangutan X2 ships with a 20MHz external crystal oscillator, and has its fuses configured to use it. Unless you specifically changed your ATMega644 fuses to use the internal oscillator and divide it’s 8MHz output by 8, your Orangutan is actually running at 20MHz, and you should change your F_CPU definition:

#define F_CPU 20000000

This would result in your delays being unusually short, and possibly not triggering your rangefinder at all!

The other immediate problems I can see is that you’re never positioning the cursor before sending strings, and most of your delays aren’t long enough to actually see a printed message. It sounds like PD0 is always high (it must be high at some point to get past the “while(!(PIND&(1<<PC0)))” loop), which would produce the behavior you see. Your LCD actually displays “Signal happened” very breifly, then the screen clears and it displays “Loop again” for a longer delay. When you print again without repositioning the cursor your screen is probably actually displaying “Loop againSignal” on the first line and " happened" on the fourth line briefly, but almost immediately the screen is cleared and “Loop again” is printed.

I hope this gives you some ideas, are you still having trouble?

-Adam

P.S. The WinAVR delay.h functions only work for sufficiently short delays. You can read more about this in its inline comments, but the quick rule is that the _delay_ms function can only produce delays of up to 2^18/F_CPU. So, at 20MHz, the longest delay it can produce is just over 13ms. The latest version claims to automatically produce lower resolution delays of up to 6.5 seconds, but older versions will just produce a short delay. In general, I try to call delays of 10ms or less multiple times in a loop.

Thanks I fixed it :slight_smile: