Hi,
This is my first diy program, up to this time I only copied and slightly modified. Now I still looked at other and previous programs, but I started with a clean 'piece of paper'.
The idea is the system reads PWM signals from an RC car receiver, transforms them, and sends them out again as altered PWM signals.
Here we go:
byte ST_PIN = A0; // Steering PWM coming in from Rx
byte THR_PIN = A1; // Throttle PWM coming in from Rx
int st_val; // Value read from steering PWM
int thr_val; // Value read from throttle PWM
int st_sig = 5; // New PWM steering signal going out
int thr_sig = 6; // New PWM throttle signal going out
void setup() {
Serial.begin(9600);
pinMode(ST_PIN, INPUT);
pinMode(THR_PIN, INPUT);
pinMode(st_sig, OUTPUT);
pinMode(thr_sig, OUTPUT);
}
void loop() {
st_val = pulseIn(ST_PIN, HIGH); // Read steering PWM signal
// Transform steering signals: signal too high / left turn / neutral / right turn / signal too low / no signal, go to neutral
if(st_val > 1885);
{st_sig = 169;}
else if (1885 > st_val > 1495)
{st_sig = map(st_val, 1885, 1488, 169, 85);}
else if (1495 > st_val > 1488);
{st_sig = 85;}
else if (1488 > st_val > 1096);
{st_sig = map(st_val, 1488, 1096, 85, 0);}
else if (1096 > st_val > 750)
{st_sig = 0;}
else
{st_sig = 85;}
analogWrite(5, st_sig); // Write new steering PWM to pin 5
thr_val = pulseIn(THR_PIN, HIGH);
// Transform throttle signals: signal too high / throttle / neutral / brake / signal too low / no signal, go to neutral
if(thr_val > 1843);
{thr_sig = 169;}
else if (1843 > thr_val > 1350);
{thr_sig = map(st_val, 1843, 1350, 169, 85);}
else if (1450 > thr_val > 1443);
{thr_sig = 85;}
else if (1443 > thr_val > 1048);
{thr_sig = map(thr_val, 1343, 1048, 85, 0);}
else if (1048 > thr_val > 750);
{thr_sig = 0;}
else
{thr_sig = 85;}
analogWrite(6, thr_sig) //Write new throttle PWM to pin 6
int st_voltage = analogRead(A3);
Serial.println(st_voltage);
delay(250);
int thr_voltage = analogRead(A4);
Serial.println(st_voltage);
delay(250);
}
When compiling the system tells me 'else'withour a previous 'if' about the line I put some extra open space around (cannot highlight in code, obviously). I've been staring myself blind at it, please enighten me.
Besides that, I wonder if the last bit about writing the different voltages; I run the outcoming PWM signals through DACs and I want to check the outcome, so I feed the outcome of the DACs back to ports A3 and A4 to perform a basic analog read. Will this what I wrote give me both signals?
All help is welcome,
Cheers,
Hugo