I am using the following ultrasonic sensor. Sunrom Electronics...
I tried to program it using arduino atmega8. First what i did is for the 5v pin in ultrasonic sensor i powered it using the digitalpin. Then i read the serial port and converted the buffer to ascii. but the problem what i get is values remaining constant though obstacles placed at different positions.here is the code what ive used:
int ser=0;
char s;
char st[10];
int pin=2;
int a;
int i=0;
int range;
void setup()
{
pinMode(pin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(pin,HIGH);
delay(1000);
digitalWrite(pin,LOW);
delay(1000);
/* for(i=0;i<10;i++)
{
s*=Serial.read();*
_ range=(s[0]-0x30)100;// I also tried these lines as given in the datasheet._
_ range=range+(s[1]-0x30)10;_
_ range=range+(s[2]-0x30)1;_
_ Serial.println(s);_
_}_ _}/_
Could you modify your post, highlight all the code in it and then hit the "#" button on the editor toolbar, please?
If you're powering the device for a second, how much data does it return in that time?
The serial buffer is only 128 bytes long, so you may well be overflowing.
int ser=0;
char s;
char st[10];
int pin=2;
int a;
int i=0;
int range;
void setup()
{
pinMode(pin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(pin,HIGH);
delay(1000);
digitalWrite(pin,LOW);
delay(1000);
/* for(i=0;i<10;i++)
{
s[i]=Serial.read();
range=(s[0]-0x30)*100;// I also tried these lines as given in the datasheet.
range=range+(s[1]-0x30)*10;
range=range+(s[2]-0x30)*1;
Serial.println(s[i]);
}
}*/
if(Serial.available())
{
s=Serial.read();
if(i<10&&s!='c'&&s!='m')
{
st[i]=s;
i++;
}
i=0;
int val=atoi(st);
Serial.println(val);
}
}
for a second it returns one data.. i mean the 9 characters(distance) as specified in the datasheet. the problem is that i am getting random values and at one instant the values become constant irrespective of the distance. if the serial buffer gets full as you say, what modification should i do in the code?pls help me
The link is still broken.
One way of preventing the buffer overflowing is to not allow it to.
You could switch on the rangefinder for a shorter period of time, but there may be a minimum time that it needs to be on to calibrate and start-up.
You could maybe flush the input buffer.
But witout a description of the device, who knows?