-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCode for Robot Gear Motor (RIDA).txt
142 lines (117 loc) · 2.72 KB
/
Code for Robot Gear Motor (RIDA).txt
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Code for Robot Gear Motor (Tyres):
#include <AFMotor.h>
const int trigPin = A0;
const int echoPin = A1;
const int trigPin1 = A2;
const int echoPin1 = A3;
int mpin = A4;
int mout;
long duration, duration1;
int distance, distance1;
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR12_1KHZ);
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
motor1.setSpeed(255);
motor2.setSpeed(255);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = duration1 * 0.034 / 2;
if (distance >= 15) {
Forward();
delay(100);
}
else if (distance < 15) {
Stop();
delay(70);
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = duration1 * 0.034 / 2;
}
mout = analogRead(mpin);
if (distance1 < 6 && mout <= 500) {
Stop();
delay(100);
Forward();
delay(1000);
}
if (distance1 < 6 && mout > 500) {
while (mout > 500) {
motor4.run(FORWARD);
mout = analogRead(mpin);
if (mout <= 500) {
motor4.run(RELEASE);
delay(100);
break;
}
}
}
}
void Stop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
}
void Forward() {
motor1.run(FORWARD);
motor2.run(FORWARD);
}
Code for Ultrasonic Sensor to Stop the Robot/ detect obstacle
const int trigPin = A0;
const int echoPin = A1;
int distance;
long duration;
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
motor1.setSpeed(255);
motor2.setSpeed(255);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
if (distance >= 15) {
Forward();
delay(100);
} else {
Stop();
delay(70);
}
}
void Stop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
}
void Forward() {
motor1.run(FORWARD);
motor2.run(FORWARD);
}