I'm trying to make an override code

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.
No description
21 Replies
jermany
jermanyOP4d ago
// C++ code //

#include <Servo.h>

Servo turretServo;

int pos = 0;
int analogPin = A0;

bool autoSearch = true;
bool overrideFunction = false;
bool manualControl = false;
bool displayLight = false;

unsigned long LEDTime = 0;
unsigned long ServoTime = 0;

const int LED_INTERVAL = 500;
const int SERVO_INTERVAL = 10;

void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(analogPin, INPUT);
turretServo.attach(3);
}

void loop() {
unsigned long timeElapsed = millis();
int sensorValue = analogRead(analogPin);

if (sensorValue >= 1000) {
overrideFunction = true;
} else if (sensorValue <= 10) {
overrideFunction = false;
}


if (timeElapsed - LEDTime >= LED_INTERVAL) {
LEDTime = timeElapsed;
displayLight = !displayLight;
digitalWrite(LED_BUILTIN, displayLight);
digitalWrite(11, displayLight);
}

if (overrideFunction) {
turretServo.write(180);

if (timeElapsed - ServoTime >= SERVO_INTERVAL) {
ServoTime = timeElapsed;

if (!manualControl) {
if (autoSearch) {
pos++;
if (pos >= 270) autoSearch = false;
} else {
pos--;
if (pos <= 0) autoSearch = true;
}

turretServo.write(pos);
}

return;
}
}
}
// C++ code //

#include <Servo.h>

Servo turretServo;

int pos = 0;
int analogPin = A0;

bool autoSearch = true;
bool overrideFunction = false;
bool manualControl = false;
bool displayLight = false;

unsigned long LEDTime = 0;
unsigned long ServoTime = 0;

const int LED_INTERVAL = 500;
const int SERVO_INTERVAL = 10;

void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(analogPin, INPUT);
turretServo.attach(3);
}

void loop() {
unsigned long timeElapsed = millis();
int sensorValue = analogRead(analogPin);

if (sensorValue >= 1000) {
overrideFunction = true;
} else if (sensorValue <= 10) {
overrideFunction = false;
}


if (timeElapsed - LEDTime >= LED_INTERVAL) {
LEDTime = timeElapsed;
displayLight = !displayLight;
digitalWrite(LED_BUILTIN, displayLight);
digitalWrite(11, displayLight);
}

if (overrideFunction) {
turretServo.write(180);

if (timeElapsed - ServoTime >= SERVO_INTERVAL) {
ServoTime = timeElapsed;

if (!manualControl) {
if (autoSearch) {
pos++;
if (pos >= 270) autoSearch = false;
} else {
pos--;
if (pos <= 0) autoSearch = true;
}

turretServo.write(pos);
}

return;
}
}
}
here's the entire code, and... I gotta admit, it sucks for a newbie like me
MaderDash
MaderDash4d ago
Allready tried helping you you never replied now your crossposting
DarwinWasW
DarwinWasW4d ago
Smells like GPT Chat.
jermany
jermanyOP4d ago
Oh uh... dang... I slept because of, yeah. Timezones. Apologies yep, I'm guilty for that one~ this is version two tho
// C++ code //

#include <Servo.h>

Servo turretServo;

int pos = 0;
int sensorValue = 0;
int overrideFunction = 0;

bool autoSearch = true;
bool manualControl = false;
bool displayLight = false;

unsigned long LEDTime = 0;
unsigned long ServoTime = 0;

const int LED_INTERVAL = 500;
const int SERVO_INTERVAL = 10;
const int microPin = A0;

void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(microPin, INPUT);
turretServo.attach(3);
}

void loop() {
unsigned long timeElapsed = millis();
overrideFunction = digitalRead(microPin);

if (overrideFunction == LOW) {
if (timeElapsed - LEDTime >= LED_INTERVAL) {
LEDTime = timeElapsed;
displayLight = !displayLight;
digitalWrite(LED_BUILTIN, displayLight);
digitalWrite(11, displayLight);
}

if (timeElapsed - ServoTime >= SERVO_INTERVAL) {
ServoTime = timeElapsed;

if (autoSearch) {
pos++;
if (pos >= 270) autoSearch = false;
} else {
pos--;
if (pos <= 0) autoSearch = true;
}

turretServo.write(pos);
}

return;

} else {

turretServo.write(90);

}

}
// C++ code //

#include <Servo.h>

Servo turretServo;

int pos = 0;
int sensorValue = 0;
int overrideFunction = 0;

bool autoSearch = true;
bool manualControl = false;
bool displayLight = false;

unsigned long LEDTime = 0;
unsigned long ServoTime = 0;

const int LED_INTERVAL = 500;
const int SERVO_INTERVAL = 10;
const int microPin = A0;

void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(microPin, INPUT);
turretServo.attach(3);
}

void loop() {
unsigned long timeElapsed = millis();
overrideFunction = digitalRead(microPin);

if (overrideFunction == LOW) {
if (timeElapsed - LEDTime >= LED_INTERVAL) {
LEDTime = timeElapsed;
displayLight = !displayLight;
digitalWrite(LED_BUILTIN, displayLight);
digitalWrite(11, displayLight);
}

if (timeElapsed - ServoTime >= SERVO_INTERVAL) {
ServoTime = timeElapsed;

if (autoSearch) {
pos++;
if (pos >= 270) autoSearch = false;
} else {
pos--;
if (pos <= 0) autoSearch = true;
}

turretServo.write(pos);
}

return;

} else {

turretServo.write(90);

}

}
im still open to suggestions, I'm kind of new also, please don't expect me to reply immediately, I'm from the Philippines and we might have different active times
DarwinWasW
DarwinWasW4d ago
1. what device are you using - as in arduino
jermany
jermanyOP4d ago
I'm using an Arduino as the main microcontroller micro:bit for the inputs I plan to use micro:bits for radio I may use a radio receiver or smth but... I'm broke. And as you can see in the pic I use Tinkercad
DarwinWasW
DarwinWasW4d ago
What is it you actually want to do ?
jermany
jermanyOP4d ago
So here's my whole plan: - I basically want to make hardware for a TF2 Sentry + wrangler. I thought this is the best idea for my first project You know these stuff right?
DarwinWasW
DarwinWasW4d ago
So you want the unit to receive codes - via the Micro from the wireless wrangler
jermany
jermanyOP4d ago
Yep So more details:
DarwinWasW
DarwinWasW4d ago
You have created the wrngler ?
jermany
jermanyOP4d ago
Almost, the thing above is a prototype I'm just testing micro:bit as an input The radio function and the gyroscope function will actually be useful You get what I'm saying so far, right?
DarwinWasW
DarwinWasW4d ago
Yes - relatively simple
jermany
jermanyOP4d ago
Oh good so can I explain my plans now?
DarwinWasW
DarwinWasW4d ago
Yeah sure.
jermany
jermanyOP4d ago
So... what I wanna do is: ✅ Build hardware for the sentry itself ✅ Prototype the receiver and sender with micro:bit code - I kind of did that by experimenting with buttons in the micro:bit Things I haven't done yet: - Radio inputs - Harnessing the built-in gyroscope for aiming - Or, using the buttons for aiming left and right, and tilting up and down for aiming up or down - Experimenting with joysticks - Building the 3D model for the sentry itself (which is already hard if I didn't have a 3D PRINTER YET) - Buying the materials for the project I'm still learning this stuff... well, I got into HTML and heard about Arduino some time ago, this is relatively new to me It is just now that I had enough free time to pursue this hobby lol
AnonEngineering
breaking tasks out into functions can help organize the code https://wokwi.com/projects/432710592056814593
DarwinWasW
DarwinWasW4d ago
@jermany why use a uno - why no use an esp32 with inbuilt wireless and bluetooth ?
jermany
jermanyOP4d ago
Baby steps oh neat, thanks!
DarwinWasW
DarwinWasW4d ago
It is more difficult to work an uno receiving information from a micro than just using a stand alone esp32
jermany
jermanyOP4d ago
wait a minute it also uses void setup/loop hold up THANK YOU ALL SO MUCH I picked the right community for this...

Did you find this page helpful?