// ROBOT KARNAFAL MASIH PROSES
// Api (3 potentiometers)
const int Api_L = 12;
const int Api_C = 34;
const int Api_R = 35;
// IR boundary sensors
const int IR_L = 25;
const int IR_R = 26;
// Servo
const int ServoL = 25;
const int ServoR = 26;
// Led Mata
const int LedR = 25;
const int LedL = 26;
// Ultrasonic
const int TRIG = 5;
const int ECHO = 2;
// Api ADC threshold (tune as needed)
const int Api_THRESHOLD = 2000;
// ULTRASONIC FUNCTION
long getDistanceCM(){
digitalWrite(TRIG, LOW); delayMicroseconds(5);
digitalWrite(TRIG, HIGH); delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long duration = pulseIn(ECHO, HIGH, 40000);
if(duration == 0) return -1; // no echo
return duration * 0.0345 / 2;} // convert to cm
void setup(){
Serial.begin(115200);
// IR sensors
pinMode(IR_L, INPUT);
pinMode(IR_R, INPUT);
// Servo
pinMode(ServoL, OUTPUT);
pinMode(ServoR, OUTPUT);
// Ultrasonic pins
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
Serial.println("Defense");}
void loop(){
// Read Api Sensors (3 pots)
int ApiL = analogRead(Api_L);
int ApiC = analogRead(Api_C);
int ApiR = analogRead(Api_R);
bool detectL = ApiL > Api_THRESHOLD;
bool detectC = ApiC > Api_THRESHOLD;
bool detectR = ApiR > Api_THRESHOLD;
// Read IR Boundary Sensors
bool L_Hit = (digitalRead(IR_L) == LOW);
bool R_Hit = (digitalRead(IR_R) == LOW);
// Read Ultrasonic
long distance = getDistanceCM();
// Print Clean Sensor Output
Serial.println("\--------");
Serial.println(" SENSOR READINGS");
Serial.println("--------");
Serial.print ("Api L/C/R ADC: ");
Serial.print (ApiL); Serial.print(" | ");
Serial.print (ApiC); Serial.print(" | ");
Serial.println(ApiR);
Serial.print ("Api Logic: ");
if(detectL) Serial.println("LEFT");
else if(detectR) Serial.println("RIGHT");
else if(detectC) Serial.println("CENTER");
else Serial.println("NO Api");
Serial.print("Boundary Sensors F/B: ");
Serial.print(L_Hit); Serial.print(" / ");
Serial.println(R_Hit);
Serial.print("Distance (cm): ");
if(distance < 0) Serial.println("NO ECHO");
else Serial.println(distance);
delay(300);}MASIH PROSES