int ObstacleDetect = 0; // Example sensor reading
int valueX = 0; // Future Motor to move forward
int valueY = 0; // Future Motor to move left or right
int valueZ = 0; // Future Flight Mode
int distance = 0;
int steps = 0;
int counter = 0;
int Energy = 2; // Replace with your integer variable
int Solar = 0; //Future Recharger when the sun is out.
int Battery = 2; //Future Battery
void setup() {
// Your setup code here
// This function runs once when the Arduino is powered on or reset.
Serial.begin(9600); // Initialize serial communication at 9600 bps.
// randomSeed(analogRead(A0)); // Seed the random number generator with an analog reading.
// Print the values to the serial monitor.
Serial.print("TRAVEL XY Z v3 ");
Serial.print(".............................");
delay(5000); // Delay for 5 seconds.
// Clear the screen by printing several newline characters
for (int i = 0; i < 50; i++) {
Serial.println();
}
}
void loop() {
// This function runs repeatedly as long as the Arduino is powered on.
// This a used to randomly add virtual obstacles infront of the Arduino.
//Readings above 900 and below 100 trigger and obstacle. 100-900 no obstacle detected
int randomNumber1 = random(1, 1001); // Generate a random number between 1 and 100.
ObstacleDetect = randomNumber1;
int randomNumber2 = random(1, 5); // Generate a random number between 1 and 5.
Solar = randomNumber2;
// Check if Fuel Tank is Empty
if (Energy <= 0) {
// Tank IS EMPTY
// Serial.print(Energy);
Serial.print(" Distance Traveled: ");
Serial.println(distance);
Serial.println(" : Power Supply Depleted: FULL STOP");
Energy = 0;
} else {
// Passed Tank NOT Empty test
//Moving to next LOOP
// Obstacle detection and Avoidance System v1.
// Future Utrasound sensor Distance reading
if (ObstacleDetect > 950) {
//Obstacle Head default to turn right and go forward
// Will add 1 extra unit of fuel
Serial.print(ObstacleDetect);
Serial.println(" Obstacle detected ahead, scanning alternative route.");
Serial.println(" Turning Right and proceeding ahead to avoid obstacle.");
counter = counter =1;
Energy = Energy - 2;
valueY = valueY + 1; //motor
valueX = valueX + 1; //motor
distance = distance + 1;
// Print the values to the serial monitor.
Serial.print("Energy Level: ");
Serial.print(Energy);
Serial.print("| Solar Cloudy: ");
Serial.print(Solar/50);
Serial.print("| Battery: ");
Battery= Battery/25; //Power drain on system
Serial.println(Battery);
Serial.print(" Distance Traveled: ");
Serial.println(distance);
Serial.print("X Axis: ");
Serial.print(valueX);
Serial.print("| Y Axis: ");
Serial.print(valueY);
Serial.print("| Z Axis: ");
Serial.println(valueZ);
delay(10000); // Wait for 10 seconds before looping again.
} else if (ObstacleDetect > 50) {
//No Obstacles detected ahead go forward
Serial.print(ObstacleDetect);
Serial.println(" Course is clear proceeding ahead.");
counter = counter =1;
Energy = Energy - 1;
valueX = valueX + 1;
distance = distance + 1;
Solar = Solar + 3;
Serial.print("Energy Level: ");
Serial.print(Energy);
Serial.print("| Solar Charging: ");
Serial.print(Solar);
Serial.print("| Battery: ");
Serial.println(Battery);
Serial.print(" Distance Traveled: ");
Serial.println(distance);
Serial.print("X Axis: ");
Serial.print(valueX);
Serial.print("| Y Axis: ");
Serial.print(valueY);
Serial.print("| Z Axis: ");
Serial.println(valueZ);
Battery = Battery + Solar; //Future Battery
Energy = Energy + (Battery/2);
Battery = Battery/2;
Solar = 0; //Reset for darkness
delay(10000); // Wait for 10 seconds before looping again.
} else {
//Obstacle Head turn left and go forward.
// Will add 1 extra unit of fuel
Serial.print(ObstacleDetect);
Serial.println(" Obstacle detected ahead, scanning alternative route.");
Serial.println(" Turning Left and proceeding ahead to avoid obstacle.");
counter = counter =1;
Energy = Energy - 2;
valueY = valueY - 1;
valueX = valueX + 1;
distance = distance + 1;
Solar=0;
// Print the values to the serial monitor.
Serial.print("Energy Level: ");
Serial.print(Energy);
Serial.print("| Solar Dark: ");
Serial.print(Solar/95);
Serial.print("| Battery: ");
Battery= Battery/25; //Power drain on system darkness
Serial.println(Battery);
Serial.print(" Distance Traveled: ");
Serial.println(distance);
Serial.print("X Axis: ");
Serial.print(valueX);
Serial.print("| Y Axis: ");
Serial.print(valueY);
Serial.print("| Z Axis: ");
Serial.println(valueZ);
delay(10000); // Wait for 10 seconds before printing again.
}
// Energy = Energy - 1;
delay(5000); // Delay for 5 seconds.
// Clear the screen by printing several newline characters
for (int i = 0; i < 50; i++) {
Serial.println();
}
}
// Serial.println(" : END : ");
// delay(5000); // Delay for 5 seconds.
}