-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMICROMOUSE
More file actions
70 lines (56 loc) · 1.7 KB
/
Copy pathMICROMOUSE
File metadata and controls
70 lines (56 loc) · 1.7 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
#include <Servo.h>
// Define pin numbers for motor control
const int leftMotorPin = 9;
const int rightMotorPin = 10;
// Define pin numbers for sensor inputs
const int leftSensorPin = A0;
const int rightSensorPin = A1;
// Define threshold values for line following
const int threshold = 500; // Adjust this value based on your sensors
// Define servo for direction control
Servo servo;
void setup() {
// Initialize motors
pinMode(leftMotorPin, OUTPUT);
pinMode(rightMotorPin, OUTPUT);
// Initialize servo
servo.attach(8); // Use the appropriate pin for servo control
// Initialize serial communication if needed
// Serial.begin(9600);
}
void loop() {
// Read sensor values
int leftSensorValue = analogRead(leftSensorPin);
int rightSensorValue = analogRead(rightSensorPin);
// Check if both sensors detect the line
if (leftSensorValue > threshold && rightSensorValue > threshold) {
// Both sensors on the line, move forward
moveForward();
} else if (leftSensorValue > threshold) {
// Only left sensor on the line, turn right
turnRight();
} else if (rightSensorValue > threshold) {
// Only right sensor on the line, turn left
turnLeft();
} else {
// No sensors on the line, stop or perform other actions
stopMotors();
}
}
void moveForward() {
digitalWrite(leftMotorPin, HIGH);
digitalWrite(rightMotorPin, HIGH);
}
void turnLeft() {
digitalWrite(leftMotorPin, LOW);
digitalWrite(rightMotorPin, HIGH);
}
void turnRight() {
digitalWrite(leftMotorPin, HIGH);
digitalWrite(rightMotorPin, LOW);
}
void stopMotors() {
digitalWrite(leftMotorPin, LOW);
digitalWrite(rightMotorPin, LOW);
}
// Add maze-solving algorithms and other functionality as needed