Computer Science Make a C++ programming ARDUINO UNO where the 1 Ultrasonic Sensor is the input and 1 servo motor(180 degree) is output. // Do no use delay function.(Only if it involved interupt function.) // Only use millis function or Interupt Function. // At a certain distance, the servo motor will spin 0 to 90 degree(Make it stop there for 3 seconds) then back to 0 degree. // At a certain distance, the servo motor will spin 0 to 135 degree (Make it stop there for 3 seconds) to 0 degree. // Make sure it is multipurpose. // U may modify the code here. // #include Servo srv; int servo_angle =0; #define maxdistance 15 unsigned long startTime_servo=0; unsigned long interval_servo =20; int time1=1; void setup() { Serial.begin(9600); pinMode(6, OUTPUT); pinMode(5, INPUT); pinMode(2, OUTPUT); pinMode(3, OUTPUT); srv.attach(7); } void loop() { unsigned currentTime= millis(); digitalWrite(6, LOW); digitalWrite(6, HIGH); int d=pulseIn(5,HIGH); d=d/29/2; digitalWrite(13, HIGH); Serial.println(d); Serial.println(time1); srv.write(0); if(d<=10 && d>1) { servo1(); } else if(d<=maxdistance && d>=10) { servo2(); } } void servo1() { digitalWrite(3, HIGH); digitalWrite(2, LOW); delay(1000); srv.write(135); delay(2500); srv.write(0); digitalWrite(3, LOW); digitalWrite(2, HIGH); digitalWrite(4, LOW); delay(3000); } void servo2() { delay(5000); digitalWrite(3, HIGH); digitalWrite(2, LOW); delay(1000); srv.write(135); delay(2500); srv.write(0); digitalWrite(3, LOW); digitalWrite(2, HIGH); digitalWrite(4, LOW); delay(3000);
Computer Science
Make a C++ programming ARDUINO UNO where the 1 Ultrasonic Sensor is the input and 1 servo motor(180 degree) is output.
//
Do no use delay function.(Only if it involved interupt function.)
//
Only use millis function or Interupt Function.
//
At a certain distance, the servo motor will spin 0 to 90 degree(Make it stop there for 3 seconds) then back to 0 degree.
//
At a certain distance, the servo motor will spin 0 to 135 degree (Make it stop there for 3 seconds) to 0 degree.
//
Make sure it is multipurpose.
//
U may modify the code here.
//
#include<Servo.h>
Servo srv;
int servo_angle =0;
#define maxdistance 15
unsigned long startTime_servo=0;
unsigned long interval_servo =20;
int time1=1;
void setup()
{
Serial.begin(9600);
pinMode(6, OUTPUT);
pinMode(5, INPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
srv.attach(7);
}
void loop()
{
unsigned currentTime= millis();
digitalWrite(6, LOW);
digitalWrite(6, HIGH);
int d=pulseIn(5,HIGH);
d=d/29/2;
digitalWrite(13, HIGH);
Serial.println(d);
Serial.println(time1);
srv.write(0);
if(d<=10 && d>1)
{
servo1();
}
else if(d<=maxdistance && d>=10)
{
servo2();
}
}
void servo1()
{
digitalWrite(3, HIGH);
digitalWrite(2, LOW);
delay(1000);
srv.write(135);
delay(2500);
srv.write(0);
digitalWrite(3, LOW);
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
delay(3000);
}
void servo2()
{
delay(5000);
digitalWrite(3, HIGH);
digitalWrite(2, LOW);
delay(1000);
srv.write(135);
delay(2500);
srv.write(0);
digitalWrite(3, LOW);
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
delay(3000);
}
Step by step
Solved in 2 steps