/*
Arduino | coding-help
I'm trying to make an override code
jermany June 2, 2025— 10:35 PM
So, for context, I am making a system where the servo motor
is constantly on an automatic sweeping motion. The micro:bit
sends an analog signal from pin 0. If its high, it will
activate an override code
Problem is... the override code doesn't work.
And the servo either freezes or the override doesn't activate.
*/
#include <Servo.h>
const int MAX_SWEEP = 180;
const int MIN_SWEEP = 0;
const int ANALOG_PIN = A0;
const int BACKLITE_PIN = 11;
const int INDICATOR_PIN = A1;
const int SERVO_PIN = 12;
const unsigned long LED_INTERVAL = 500;
const unsigned long SERVO_INTERVAL = 10;
//bool isAuto = true;
//bool isManual = false;
bool isOverride = false;
bool isBacklight = false;
int servoPos = 0;
int increment = 1;
unsigned long prevLedTime = 0;
unsigned long prevServoTime = 0;
Servo turretServo;
void blinkLed() {
if (millis() - prevLedTime >= LED_INTERVAL) {
prevLedTime = millis();
isBacklight = !isBacklight;
digitalWrite(LED_BUILTIN, isBacklight);
digitalWrite(BACKLITE_PIN, isBacklight);
}
}
void sweepServo() {
if (millis() - prevServoTime >= SERVO_INTERVAL) {
prevServoTime = millis();
servoPos += increment;
turretServo.write(servoPos);
if (servoPos >= MAX_SWEEP) increment = -1;
if (servoPos <= MIN_SWEEP) increment = 1;
}
}
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(BACKLITE_PIN, OUTPUT);
pinMode(INDICATOR_PIN, OUTPUT);
turretServo.attach(SERVO_PIN);
turretServo.write(0);
}
void loop() {
blinkLed();
int analogVal = analogRead(ANALOG_PIN);
isOverride = (analogVal >= 512 ? true : false);
digitalWrite(INDICATOR_PIN, isOverride);
if (!isOverride) {
sweepServo();
}
}
Override