Go Down

Topic: problem again with ultrasonic sensor (Read 793 times) previous topic - next topic

badri89

Hi everyone,
          I previously got help from the forum.. regarding my ultrasonic sensor..from then it was working well..now the problem what i have is sometimes it detects the distance of the obstacle accurately...while at times its giving out negative and values like 14989 in the serial monitor..even at those times distances below 10cm is detected perfectly..this is the url of the ultrasonic sensor what i m  using..

http://www.sunrom.com/index.php?main_page=product_info&cPath=55&products_id=710

here is my code..
int pin=3;
int i=0;
char *s[2];
int range=0;
void setup()
{
 pinMode(pin,OUTPUT);
 Serial.begin(9600);
}
void loop()
{
 digitalWrite(pin,HIGH);
 delay(1000);
 Serial.flush();
 while(Serial.read()!=13);
   range=0;
 for(i=0;i<3;i++)
 {
   while(!Serial.available());
   {
     *s=Serial.read();
     range=range*10+atoi(s);
 }
 }
 Serial.println(range);
  digitalWrite(pin,LOW);
  delay(1000);
 }

GrooveFlotilla

#1
Mar 09, 2010, 08:44 am Last Edit: Mar 09, 2010, 12:00 pm by GrooveFlotilla Reason: 1
Code: [Select]
range=range*10+atoi(s[i]);


I think your ASCII to decimal conversion is a little confused.
Check the definition of "atoi".

Code: [Select]
char *s[2];
Whoa!

This whole section is ill-advised.
Code: [Select]
while(Serial.read()!=13);
  range=0;
for(i=0;i<3;i++)
{
  while(!Serial.available());
  {
    *s[i]=Serial.read();
    range=range*10+atoi(s[i]);
}
}

Please use the Code (#) button when posting code.
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

babuenir

hello badri,
        thankyou for the code... i was looking for a rite code to connect my ultrasonic sensor.. now i got it connected with my arduino...

thank you again...

ciao...

Go Up