HC sr04 and arduino obstacle detection


making project let blind people know of distance before them...so principle is>>>calculate distance , let blind person know of obstacle how closer object haptic feedback.....i.e...if object closer...the haptic feedback should more strong..and if obstacle far away, feedback shud of less magnitude..

p.s: haptic feedback---touch vibration motor in different patterns user...

if obstacle in range
calculated distance through hc sr04 (with sonar principle). , arduino..


our code arduino:
logic:making motor vibrate more when obstacle closer..
the following program :some doubts in it:

code: [select]
#define trigpin 12
#define echopin 11
#define motorpin 3

void setup() {
 serial.begin (9600);
 pinmode(echopin,input);
 pinmode(trigpin,output);
 }

void loop() {
 int duration, distance;
 digitalwrite(trigpin, high);
  delaymicroseconds(1000);        //what do..waits   //1ms..rite? when trigger sensor hc sr04 first             //give low signal of 2us , 10us high pulse , low //signal .will logic gonna work..as not working //sometimes program..

 digitalwrite(trigpin, low);
 duration = pulsein(echopin, high);
 distance = (duration/2) / 29.1;
 if (distance <400 && distance >300)
 {
   analogwrite(motorpin,0);
   serial.print("out of range");// here in serial monitor..it prints //distance of 6/14 cm repeatedly if obstacle out of range.and //sometimes displays out of range desired result
   serial.println("  ");
 }
   
  else if (distance <=300 && distance >250)
 {
   analogwrite(motorpin,50);
 }
  else if (distance <=250 && distance >200)
 {
   analogwrite(motorpin,100);
 }
  else if (distance <=200 && distance >150)
 {
   analogwrite(motorpin,145);
 }
  else if (distance <=150 && distance >100)
 {
   analogwrite(motorpin,190);
 }
  else if (distance <=100 && distance >80)
 {
   analogwrite(motorpin,205);
 }
  else if (distance <=80 && distance >10)
 {
  analogwrite(motorpin,240);
 }
 
 else
 {
   analogwrite(motorpin,255);
 }
 serial.print(distance);
 
 serial.println(" cm");
 
  delay(500);   //explain me line of code..
}


problem:now ,in serial monitor observing analog distance values , comparing vibration  whether it's strong or less..
but, after range..like 250 cm,even when's there no obstacle, getting random output(distance in serial monitor) of  6/8/12/14/16/18/1140 cm....sometimes.
donno..why happening....can u tweak code..and suggest me needful..please.project submission within week


moderator edit: [code] ... [/code] tags added. (nick gammon)

code: [select]
delay(500); //explain me line of code..
it stops processor doing except respond interrupts 500 milliseconds.
i'd guess there allow echoes previous pulse die away, limits update rate less 2hz, fast blind walker might painful.


Arduino Forum > Using Arduino > Project Guidance > HC sr04 and arduino obstacle detection


arduino

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial