Thank you very much for your help. I can only find these two examples. The following code is RC PWM signal input and voltage signal output to control the throttle.
const int inputPinA = 2;
const int outputPinA = 3;
void setup() {
pinMode(inputPinA, INPUT);
}
void loop() {
unsigned long pulseLength;
pulseLength = constrain(pulseIn(inputPinA, HIGH), 1000, 3000);
analogWrite(outputPinA, map(pulseLength, 1000, 3000, 0, 255));
}
The following code is to control the gear, medium speed and high speed (low speed by default).
int out1 = 7; //declare pin for output 1
int out2 = 8; //declare pin for output 2
int pwm_value = 0;
void setup() {
pinMode(pwm_pin, INPUT);
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
}
void loop() {
pwm_value = pulseIn(pwm_pin, HIGH);
while (pwm_value < 1200){
digitalWrite(out1,LOW);
digitalWrite(out2,LOW);
pwm_value = pulseIn(pwm_pin, HIGH);
}
while ((pwm_value >1200) && (pwm_value <1800)){
digitalWrite(out1,HIGH);
digitalWrite(out2,LOW);
pwm_value = pulseIn(pwm_pin, HIGH);
}
while ((pwm_value >1800) && (pwm_value <2100)){
digitalWrite(out1,LOW);
digitalWrite(out2,HIGH);
pwm_value = pulseIn(pwm_pin, HIGH);
}
}
The following code combines the two codes, and something bad happens. After the combination, the voltage signal output function fails, and the control gear part code is very stuck and the delay is very high. I can't work normally after trying many modifications. I don't know if I should continue.
int out1 = 7;
int out2 = 8;
int pwm_value = 0;
const int inputPinA = 2;
const int outputPinA = 3;
const int inputPinB = 4;
const int outputPinB = 5;
void setup() {
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
pinMode(pwm_pin, INPUT);
pinMode(inputPinA, INPUT);
pinMode(inputPinB, INPUT);
}
void loop() {
unsigned long pulseLength;
pulseLength = constrain(pulseIn(inputPinA, HIGH), 1000, 2100);
analogWrite(outputPinA, map(pulseLength, 1000, 2100, 0, 255));
pulseLength = constrain(pulseIn(inputPinB, HIGH), 1000, 2100);
analogWrite(outputPinB, map(pulseLength, 1000, 2100, 0, 255));
pwm_value = pulseIn(pwm_pin, HIGH);
while (pwm_value < 1200){
digitalWrite(out1,LOW);
digitalWrite(out2,LOW);
pwm_value = pulseIn(pwm_pin, HIGH);
}
while ((pwm_value >1200) && (pwm_value <1800)){
digitalWrite(out1,HIGH);
digitalWrite(out2,LOW);
pwm_value = pulseIn(pwm_pin, HIGH);
}
while ((pwm_value >1800) && (pwm_value <2100)){
digitalWrite(out1,LOW);
digitalWrite(out2,HIGH);
pwm_value = pulseIn(pwm_pin, HIGH);
}
}