Hi all,
I am trying to build a controller to operate an outboard motor from an RC stick. Middle position (using different modes) the gear is ion Neutral and power can be applied in the range of idle to fast idle. Stick up or down engages the appropriate gear and allow power application from idle to full (delay protections not implemented yet).
Using the Servo library I got terrible results with unwanted servo movement al over the place. Reading on timer issues I have opted for the no-library approach which worked well, however when I using the PulseIn command I get a small amount of jitter. Is there a way around this?
Thanks
/*
- ECU
- No Servo Library
*/
int tlg_pin = 2;
int cmd;
// int last;
int cond_cmd;
int gear_val;
int thr_val;
void setup()
{
pinMode(tlg_pin, INPUT);
}
void loop()
{
// last = cmd;
cmd = pulseIn(tlg_pin, HIGH);
// if (abs(last - cmd) < 2) {
// cmd = last;
// }
if (cmd > 500 and cmd < 735) {
gear_val = 1500;
gear_srv();
//delay
thr_val = map(cmd, 500, 735, 1000, 1200);
thr_srv();
}
if (cmd >= 740 and cmd <= 1120) {
gear_val = 2000;
gear_srv();
//delay
thr_val = (map(cmd, 740, 1120, 1000, 2000));
thr_srv();
}
if (cmd >= 1200 and cmd <= 1570) {
gear_val = 1000;
gear_srv();
//delay
thr_val = (map(cmd, 1200, 1570, 1000, 2000));
thr_srv();
}
}
void thr_srv() {
long startthr = micros();
digitalWrite(9, HIGH);
while (micros() - startthr < thr_val)
;
digitalWrite(9, LOW);
//delay(10);
}
void gear_srv() {
long startgear = micros();
digitalWrite(8, HIGH);
while (micros() - startgear < gear_val)
;
digitalWrite(8, LOW);
delay(10);
}