-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch_mar29a.ino
99 lines (93 loc) · 2.37 KB
/
sketch_mar29a.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//Motor Setup:-
#include <SparkFun_TB6612.h>
#define AIN1 2
#define BIN1 7
#define AIN2 4
#define BIN2 8
#define PWMA 5
#define PWMB 6
#define STBY 9
const int offsetA = 1;
const int offsetB = 1;
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);
//Ultrasonic Setup:-
#include <Bonezegei_HCSR04.h>
const int TRIGGER_PIN = 26;
const int ECHO_PIN = 27;
Bonezegei_HCSR04 ultrasonic(TRIGGER_PIN, ECHO_PIN);
int d=0;
//DHT Setup:-
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
//Servo Setup:-
#include <Servo.h>
Servo myservo;
int pos = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
dht.begin();
}
void loop() {
// put your main code here, to run repeatedly:
int t = dht.readTemperature();
Serial.print(t, ",");
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.print(pos, ",");
delay(15);
d=ultrasonic.getDistance();
Serial.print(d);
if (d<=20; 0<pos<90){
brake(motor1, motor2);
motor1.drive(255, 1000);
}
else if(d<=20; 0<pos<180){
brake(motor1, motor2);
motor2.drive(255, 1000);
}
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
Serial.print(pos, ",");
d=ultrasonic.getDistance();
Serial.print(d);
delay(15); // waits 15 ms for the servo to reach the position
if (d<=20; 0<pos<90){
brake(motor1, motor2);
motor1.drive(255, 1000);
}
else if(d<=20; 0<pos<180){
brake(motor1, motor2);
motor2.drive(255, 1000);
}
}
if(Serial.read()=="f"){
forward(motor1, motor2, 200);
delay(50);
brake(motor1, motor2);
}
if(Serial.read()=="b"){
forward(motor1, motor2, -200);
delay(50);
brake(motor1, motor2);
}
if(Serial.read()=="r"){
right(motor1, motor2, 500);
delay(50);
brake(motor1, motor2);
}
if(Serial.read()=="l"){
left(motor1, motor2, 500);
delay(50);
brake(motor1, motor2);
}
if(Serial.read()=="s")
brake(motor1, motor2);
}