Hi all. I'm very inexperienced with Arduino. I've made a small submersible. It has a magnetic switch that turns on a motor for a set length of time. To do this I've used a very simple if statement with a delay to time it.
sketch here
#include <Servo.h>
Servo esc;
Servo servo;
void setup()
{
esc.attach(3);
servo.attach(2);
Serial.begin(9600);
// Since the other end of the reed switch is connected to ground, we need
// to pull-up the reed switch pin internally.
pinMode(Reed, INPUT_PULLUP);
esc.writeMicroseconds(1000); // send "stop" signal to ESC. Also necessary to arm the ESC.
delay(5000);
servo.write(90);
}
void loop()
{
esc.writeMicroseconds(1000); // send "stop" signal to ESC. Also necessary to arm the ESC.
int proximity = digitalRead(Reed); // Read the state of the switch
if (proximity == HIGH) // If the pin reads low, the switch is closed.
{
esc.writeMicroseconds(1000); // send "stop" signal to ESC.;
delay(15);
}
else
{
delay(1000);
esc.writeMicroseconds(2000); // send "stop" signal to ESC. Also necessary to arm the ESC.; // sets the servo position according to the scaled value
delay(60000);
}
}
I've since added a servo connected to a rudder that uses an HMC5883L magnetometer sensor to keep the sub in a straight line.
So my question is, how do I time the esc to turn off after a set amount of time while allowing the programme to routinely check the magnetometer and adjusting the servo accordingly