int maximumRange = 2000; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode(2, OUTPUT);
pinMode(3, INPUT);
pinMode(13, OUTPUT); // Use LED indicator (if required)
pinMode(7, OUTPUT);
pinMode(9, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(2, LOW);
delayMicroseconds(2);
digitalWrite(2, HIGH);
delayMicroseconds(10);
digitalWrite(2, LOW);
duration = pulseIn(3, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration / 58.2;
Serial.println(distance); //แสดงค่าระยะทาง
if(distance <= 250)
// Laser rangefinder caculate effect range
{
digitalWrite(13,HIGH);
Serial.println("Enemy tank is in range, fire when ready!");
delay(5000);
Serial.println("On the way!!");
delay(1000);
digitalWrite(7,HIGH);
delay(500);
digitalWrite(7,LOW);
delay(500);
digitalWrite(7,HIGH);
delay(250);
digitalWrite(7,LOW);
digitalWrite(13, LOW);
delay(2500);
Serial.println("Enemy tank destroyed, nice shot!");
}
else{
digitalWrite(9,HIGH);
Serial.println("Enemy tank is not in range, do not expose yourself!");
delay(5000);
digitalWrite(9,LOW);
}
delay(5000);
}