/*
Arduino | coding-help
Help me pls
WWII | Majd idriss — 9/17/26 12:54 PM
I’m building a radar/distance detection system with an Arduino Uno R3,
HC-SR04 ultrasonic sensor mounted on a servo, 1602 I2C LCD, joystick,
red LED, passive buzzer, and two buttons.
The servo scans 0°→180°→0° continuously in Auto mode.
The joystick button switches between Auto and Manual mode, and in
Manual mode the joystick controls the servo.
The separate button cycles the detection threshold: 10→20→30→40→50→10 cm.
When an object is within the selected threshold, the red LED flashes,
the buzzer sounds, and the LCD shows/flashes “Alert!”.
Normally, the LCD displays the current mode and detection distance.
I need help with code
*/
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
const int SWEEP_SPD = 500;
const int MAX_VECTOR = 175;
const int MIN_VECTOR = 5;
const int NUM_BTNS = 2;
// pin constants
const int BUZZ_PIN = 6;
const int LED_PIN = 7;
const int SERVO_PIN = 10;
const int ECHO_PIN = 11;
const int TRIG_PIN = 12;
const int JOY_V_PIN = A1;
const int JOY_H_PIN = A0;
const int BTN_PINS[] = {8, 9};
const int THRESHOLDS[] = {10, 20, 30, 40, 50};
const unsigned long DEBOUNCE_TIME = 20;
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
// function returns which button was pressed, or 0 if none
int checkButtons() {
static bool currState[NUM_BTNS];
static bool lastState[NUM_BTNS];
static unsigned long lastTime[NUM_BTNS];
int btnPressed = 0;
for (int i = 0; i < NUM_BTNS; i++) {
currState[i] = digitalRead(BTN_PINS[i]);
unsigned long now = millis();
if (currState[i] != lastState[i] && now - lastTime[i] >= DEBOUNCE_TIME) {
if (!currState[i]) {
//Serial.println("Button pressed!");
btnPressed = i + 1;
} else {
//Serial.println("Button released!");
}
lastTime[i] = now;
lastState[i] = currState[i];
}
}
return btnPressed;
}
int getDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(5);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
long cm = (duration / 2) / 29.1; // Divide by 29.1 or multiply by 0.0343
//long inches = (duration / 2) / 74; // Divide by 74 or multiply by 0.0135
return cm;
}
void showAlert(bool isOn) {
static bool ledState = false;
static unsigned long prevAlert = 0;
if (isOn) {
if (millis() - prevAlert >= 250) {
//Serial.println("Alert");
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
prevAlert = millis();
}
} else {
digitalWrite(LED_PIN, LOW);
}
}
void showSplash() {
lcd.setCursor(2, 0);
lcd.print("Range Ranger");
lcd.setCursor(6, 1);
lcd.print("V1.0");
delay(2000);
lcd.clear();
}
void updateDisplay(int bearing, int range, bool isAuto, char* threshold) {
char buffer[20];
snprintf(buffer, 20, "Dir: %03d%c %s", bearing, char(223), isAuto ? "Auto" : "Man ");
lcd.setCursor(0, 0);
lcd.print(buffer);
snprintf(buffer, 20, "Dis: %3dcm %s", range, threshold);
lcd.setCursor(0, 1);
lcd.print(buffer);
///*
Serial.print("Mode: ");
Serial.print(isAuto ? "Auto" : "Manual");
Serial.print("\tThreshold: ");
Serial.print(threshold);
Serial.print("\tBearing: ");
Serial.print(bearing);
Serial.print("\tRange: ");
Serial.println(range);
//*/
}
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
servo.attach(SERVO_PIN);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZ_PIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BTN_PINS[0], INPUT_PULLUP);
pinMode(BTN_PINS[1], INPUT_PULLUP);
showSplash();
}
void loop() {
static bool isAutoMode = true;
static int count = 0;
static int increment = 5;
static int alertRange = 10;
static int bearing = MIN_VECTOR;
static unsigned long prevMillis = 0;
char threshold[6];
int numBtn = checkButtons();
if (numBtn) {
if (numBtn == 1) {
isAutoMode = !isAutoMode;
}
if (numBtn == 2) {
count++;
if (count > 4) count = 0;
alertRange = THRESHOLDS[count];
}
}
long range = getDistance();
if (range <= alertRange) {
snprintf(threshold, 6, "%s", "Alert");
showAlert(true);
} else {
snprintf(threshold, 6, " %2d ", alertRange);
showAlert(false);
}
if (millis() - prevMillis >= SWEEP_SPD) {
if (isAutoMode) {
bearing = bearing + increment;
if (bearing >= MAX_VECTOR || bearing <= MIN_VECTOR) {
increment = -increment;
}
servo.write(bearing);
} else {
bearing = map(analogRead(JOY_H_PIN), 0, 1023, 180, 0);
servo.write(bearing);
}
updateDisplay(bearing, range, isAutoMode, threshold);
prevMillis = millis();
}
}
MODE