I am trying to have a Ultrasonic sensor (the sensor that uses sonar) to move a servo motor when it detects anything within about 24 inches. I thought the best way to program that would be to use a If Else statement (if i said that correctly) but I can't figure out what primary-expression goes after the "if" in between the parenthesis.
I went to the Reference page on this website and it said pinFiveInput < 500, but I'm not sure if it has to be different for my program.
Help would be great! I'm a beginner to programming arduino code and I am only 14 so if I don't understand something sorry.
I Didn't know where to put the function for the trigger and echo pins. (so I haven't gotten that far)
Here's my code:
#include <Servo.h>
Servo myservo;
int pos = 0;
void setup() {
myservo.attach(9);
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) {
// in steps of 1 degree
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
}
Try finding some code which uses the ultrasonic sensor. Find the name of the variable which records distance. Try to construct an if-else statement using that variable instead of pinFiveInput.
Next time, read the how-to-use-this-forum and use code tags around your code. The forum software eats some of your code if you don't.
Try finding some code which uses the ultrasonic sensor. Find the name of the variable which records distance. Try to construct an if-else statement using that variable instead of pinFiveInput.
Next time, read the how-to-use-this-forum and use code tags around your code. The forum software eats some of your code if you don't.
Ok, I'll try that and I'll look at the how to. Thanks for the tip/help
As well as trying to add some of the ultrasonic sensor code you might also want to say how your servo should move because I'm not convinced that going from 0 to 180 and back continuously is what you need.
BTW you won't make many friends by claiming that the Sweep example from the IDE with some of the comments (including the author's names) taken out is YOUR code.