Click here to Skip to main content
15,851,711 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#define echopin 11 //set echopin
#define trigpin 12 //set trigpin
#include <servo.h>;
Servo robotArm;
#include <newping.h>
#define MAX_DISTANCE 400
NewPing sonar(trigpin, echopin, MAX_DISTANCE);
int distance;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  robotArm.attach(9); //attach our servo
  robotArm.writeMicroseconds(150);
}

void loop() {
  // put your main code here, to run repeatedly:
  robotArm.write(90); //always set to servo 90 to position it to the middle
  //codes of ultrasonic sensor
  distance=digitalRead(echopin);
  if (distance <= 20) //if ultrasonic sensor detects on obstacle less than 20 cm in 90 degree angle
  {
    robotArm.write(0); //dervo rotates at full speed to the right
    delay(60);
  }
  else
  {
    robotArm.write(90); //else servo stays at 90 degree angle
    delay(60);
  }
  Serial.print(distance); //print distance
  Serial.println("cm"); //print distance unit cm
}


What I have tried:

I'm working about arduino and HC_SR04. I searced most of documents, but I didnt solve our problem. My question is that how to read a value that is taken from library NewPing. Thanks
Posted
Updated 1-Dec-16 1:54am
v2
Comments
[no name] 29-Nov-16 19:19pm    
What have you tried? What is not clear here: http://playground.arduino.cc/Code/NewPing
hairy_hats 30-Nov-16 7:35am    
For Arduino-specific problems you might be better off asking on the Arduino forums.

1 solution

It is not clear, what you are asking for, but the first point where you should search is in the NewPing documentation.

Tip: for clear code it is state of art to write the headers at top, and than implement and initialize like that:

C++
#include <servo.h>;
#include <newping.h>
//start with constants
#define echopin 11 //set echopin (I hope you know why that value)
#define trigpin 12 //set trigpin (I hope you know why that value)

const int MAX_DISTANCE = 400;//better for type checks
//implement global objects
Servo robotArm;
NewPing sonar(trigpin, echopin, MAX_DISTANCE);
int distance = MAX_DISTANCE;//initialize</newping.h></servo.h>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900