-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMotorDriversWithXbee.ino
More file actions
138 lines (110 loc) · 3.13 KB
/
MotorDriversWithXbee.ino
File metadata and controls
138 lines (110 loc) · 3.13 KB
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
#include <Wire.h>
#define LIDARLite_ADDRESS 0x62 // Default I2C Address of LIDAR-Lite.
#define RegisterMeasure 0x00 // Register to write to initiate ranging.
#define MeasureValue 0x04 // Value to initiate ranging.
#define RegisterHighLowB 0x8f // Register to get both High and Low bytes in 1 call.
int reading = 0;
int speed = 250;
int motorpin1 = 2 ;
int motorpin2 = 4 ;
int speedpin1 = 3 ;
int speedpin2 = 5 ;
int beeb = 8 ;
void setup() {
pinMode(motorpin1, OUTPUT);
pinMode(motorpin2, OUTPUT);
pinMode(speedpin1, OUTPUT);
pinMode(speedpin2, OUTPUT);
pinMode(beeb, OUTPUT);
Wire.begin(); // join i2c bus
Serial.begin(9600);
}
void loop()
{
while (Serial.available() > 0) {
char incomingByte = Serial.read();
Serial.println(incomingByte);
if (incomingByte == 'w') {
forward();
}
else if (incomingByte == 's') {
backward();
}
else if (incomingByte == 'd') {
right();
}
else if (incomingByte == 'a') {
left();
}
else if (incomingByte == 'i') {
if ( speed <= 240 ) {
speed += 10;
}
}
else if (incomingByte == 'p') {
if ( speed >= 10 ) {
speed -= 10;
}
}
else if (incomingByte == 'z')
{
analogWrite(speedpin1, 0);
analogWrite(speedpin2, 0);
}
else if (incomingByte == 'b')
{
digitalWrite(beeb, 1);
delay(120);
digitalWrite(beeb, 0);
}
}
Serial.println(distance());
if (distance() < 30 ) {
analogWrite(speedpin1, 0);
analogWrite(speedpin2, 0);
}
}
void backward() {
digitalWrite (motorpin1, LOW);
digitalWrite (motorpin2, HIGH);
analogWrite(speedpin1, speed);
analogWrite(speedpin2, speed);
}
void forward() {
digitalWrite (motorpin1, HIGH);
digitalWrite (motorpin2, LOW);
analogWrite(speedpin1, speed);
analogWrite(speedpin2, speed);
}
void right() {
digitalWrite (motorpin1, HIGH);
digitalWrite (motorpin2, HIGH);
analogWrite(speedpin1, speed);
analogWrite(speedpin2, speed);
}
void left() {
digitalWrite (motorpin1, LOW);
digitalWrite (motorpin2, LOW);
analogWrite(speedpin1, speed);
analogWrite(speedpin2, speed);
}
int distance() {
Wire.beginTransmission((int)LIDARLite_ADDRESS); // transmit to LIDAR-Lite
Wire.write((int)RegisterMeasure); // sets register pointer to (0x00)
Wire.write((int)MeasureValue); // sets register pointer to (0x00)
Wire.endTransmission(); // stop transmitting
delay(20); // Wait 20ms for transmit
Wire.beginTransmission((int)LIDARLite_ADDRESS); // transmit to LIDAR-Lite
Wire.write((int)RegisterHighLowB); // sets register pointer to (0x8f)
Wire.endTransmission(); // stop transmitting
delay(20); // Wait 20ms for transmit
Wire.requestFrom((int)LIDARLite_ADDRESS, 2); // request 2 bytes from LIDAR-Lite
if (2 <= Wire.available()) // if two bytes were received
{
reading = Wire.read(); // receive high byte (overwrites previous reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
reading = reading - 15 ;
}
return reading;
}