this is not my code this is what i found on makersguide i tried to use the same but it doesn't work
#include <Servo.h>
// Define Trig and Echo pin:
#define trigPin 10
#define echoPin 9
// Define variables:
long duration;
int distance;
void setup() {
// Define inputs and outputs:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in
microseconds:
duration = pulseIn(echoPin, HIGH);
// Calculate the distance:
distance = duration * 0.034 / 2;
// Print the distance on the Serial Monitor (Ctrl+Shift+M):
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(50);
}
// Include the servo library:
#include <Servo.h>
// Create a new servo object:
Servo myservo;
// Define the servo pin:
#define servoPin 9
// Create a variable to store the servo position:
int angle = 0;
voidsetup() {
// Attach the Servo variable to a pin:
myservo.attach(servoPin);
}
void loop(){
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
myservo.write(0);
delay(1000);
// Sweep from 0 to 180 degrees:
for (angle = 0; angle <= 180; angle += 1) {
myservo.write(angle);
delay(0);
}
for (angle = 180; angle <= 0; angle -= 1) {
myservo.write(angle);
delay(0);
}
delay(1000);
}
loopfunctions, and avoidsetupwhatever that is. – Nick Gammon Nov 27 '20 at 05:33