Actually, I want to make Visal Phumint's This project
Because I can't write code, I can only find data to piece together. I can only find these two examples. The following code is RC PWM signal input and voltage signal output, thus controlling the throttle size.
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 pwm_pin = 6; //declare pin which reads pwm value
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 together, 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 don't know if I should continue. I really hope to get your help.
int pwm_pin = 6;
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);
}
}