controlling servo motor based on the speed measured by the ultrasonic sensor in tinker cad

 


code:

controlling servo motor based on the speed measured by the ultrasonic sensor in tinker cad

#include<math.h> #include <Servo.h> Servo servo1; int trigPin = 8; int echoPin = 8; float distance; float duration; void setup() { Serial.begin(9600); servo1.attach(7); servo1.write(0);// put your setup code here, to run once: } void loop() { float dist1=ultra(); delay(1000); float dist2=ultra(); float speed =0; speed = abs(dist2-dist1); Serial.println(speed); if(speed>20) { servo1.write(90); Serial.println(dist1); } } float ultra(){ pinMode(trigPin, OUTPUT); digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH); distance = duration*0.034/2; return distance; }

for any queries related to this , contact me on instagram:

https://www.instagram.com/vamsikrishna_852/

Comments

Popular posts from this blog

smart AGRICULTURE part1|| vcb ea.

basic interfacing with servo motor.