I really want to control a servo with an ultrasonic sensor. For instance the servo rotates 180 degrees and I was thinking multiplying the sensors reading by three and writing to servo buutttt I suck at writing code and really need help.
Cheers Gunt
buutttt I suck at writing code and really need help.
Have you started learning by trying the various example codes supplied in the arduino IDE?
Yeah i have tried to mix the potentiometer and new ping examples into one but with no luck.
Very simple servo test code.
// zoomkat 7-30-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.attach(9);
}
void loop() {
while (Serial.available()) {
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(3);
}
}
if (readString.length() >0) {
Serial.println(readString);
int n = readString.toInt();
Serial.println(n);
myservo.writeMicroseconds(n);
//myservo.write(n);
readString="";
}
}
Gunterpoof:
Yeah i have tried to mix the potentiometer and new ping examples into one but with no luck.
If you want help post your code. Please use the code button </>
so your code looks like this
Without that it is impossible to know exactly what is causing you trouble.
...R
thanks mate, i must admit the code looks horrible.
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
#include <NewPing.h>
#include <Servo.h>
Servo myservo;
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
int us = 11;
int val;
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
val = digitalRead(us); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 60, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15);
}
hope this helps?
This does not make sense for a few reasons
val = digitalRead(us); // reads the value of the potentiometer (value between 0 and 1023)
digitalRead() returns a value of 1 or 0.
You need analaogRead() and it must be on one of the analogPins if you want to read a potentiometer.
But I think you want the servo to move according to the value from sonar.ping() which you have, confusingly, given a nearly identical name of uS. If so you need neither digital- nor analogRead.
What range of values do you get in the variable uS ?
What range of movement do you want from the servo ?
...R
Thanks Guys,
i was hoping for 3 degrees per 1cm sorry again for my bed coding skills.
Gunter.
Sorry i don't really understand your question about the range of values uS returns.
Gunterpoof:
Sorry i don't really understand your question about the range of values uS returns.
What is the smallest value and what is the largest value ?
...R
It says max range is 200cm and smallest 3-4cm, i remember my computer teacher said something about it being from 0-1023 (not cm) one less than analogue.
Gunt.
Gunterpoof:
It says max range is 200cm and smallest 3-4cm,
I was not asking "what it says".
I want to know what is the maximum and minimum value of the variable that you will get in your project when it is working properly. Use Serial.println() to display the values.
...R