GPT based improved void loop()
ishkapoor2000 opened this issue · comments
void loop() {
delay(40);
// Read the distance from the ultrasonic sensor
distance = readPing();
// Check if there is an obstacle within 8 cm
if (distance <= 8) {
Serial.println(distance);
// Stop the robot
moveStop();
delay(100);
// Read the flame sensor and map the reading to a range of 0-3
sensorReading = analogRead(A0);
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
if (range == 0) {
// Extinguish the fire
Serial.println("Flame");
moveStop();
fire_ex();
delay(500);
} else {
// Move backward and look left and right to determine which direction to turn
moveBackward();
delay(300);
moveStop();
delay(200);
int distanceR = lookRight();
delay(200);
int distanceL = lookLeft();
delay(200);
// Turn in the direction with the most clearance
if (distanceR >= distanceL) {
turnRight();
moveStop();
} else {
turnLeft();
moveStop();
}
}
} else {
// Move forward
Serial.println(distance);
moveForward();
}
}