A demo of arduino controlling the servo

A demo of arduino controlling the servo

Module: steering gear ultrasonic bluetooth
function: when the distance of the object is less than 10cm, the clamp is controlled to clamp the object, and the measured distance is transmitted outward.
Program:
#include <Servo.h>
int TrgPin = A0;
int EcoPin = A1;
int dist;

Servo myservo; // Define Servo object to control
int pos = 0; // Angle storage variable

void setup() { Serial.begin(9600); myservo.attach(9);// The control line is connected to the digital 9 pinMode(TrgPin, OUTPUT); // Set EcoPin to input state pinMode(EcoPin, INPUT); }





void loop() { digitalWrite(TrgPin, LOW); delayMicroseconds(8); digitalWrite(TrgPin, HIGH); // Maintain a high level for 10 milliseconds to generate a pulse delayMicroseconds(10); digitalWrite(TrgPin, LOW); / / Read the width of the pulse and convert it to the distance dist = pulseIn(EcoPin, HIGH) / 58.00; Serial.println(dist);








if(dist<10){ for (pos; pos> 0; pos --) {// from 180° to 0° myservo.write(pos); // write the servo angle delay(5); // wait Rotate to the specified angle } }




if(dist>10){ for (pos; pos <90; pos ++) {// 0° to 180° // in steps of 1 degree myservo.write(pos); // write servo angle delay( 5); // Waiting to rotate to the specified angle } }





delay(700);
}

contact us

Scan the QR code to get more.
Insert picture description here
This article is organized or written by the BCIduino brain-computer interface open source community. The BCIduino brain-computer interface community was initiated and established by masters and doctors from Beihang University, Cornell University, Peking University, Capital Medical University, etc. Welcome to join the community by scanning the code and note "BCI", and also welcome to purchase BCIduino EEG modules (some Search for treasure)

Welcome everyone to follow us
Insert picture description here

Guess you like

Origin blog.csdn.net/nvsirgn/article/details/113933127