Arduino UNO uses a PWM channel of RC model remote controller to control two LEDs

I want to realize this function:
Arduino UNO uses a PWM channel of RC model remote controller to control two LEDs. When the signal is 1000, LEDA and LEDB are off, when the signal is about 1500, LEDA is on, and when the signal is 1800-2000, LEDA is off at the same time.
However, the example used cannot be realized, and LEDA can only be turned on when the signal is around 1500, and LEDB can be turned on when the signal is between 1800 and 2000 (LEDA does not go out).
Hope to get your help, thank you!

const int inputPinA = 2; 
const int outputPinA = 3; 
const int outputPinB = 4; 
void setup() {
    pinMode(inputPinA, INPUT);

}


void loop() {
    
    unsigned long pulseLength;

    pulseLength = constrain(pulseIn(inputPinA, HIGH), 1400, 1500);
    analogWrite(outputPinA, map(pulseLength, 1400, 1500, 0, 255));

    pulseLength = constrain(pulseIn(inputPinA, HIGH), 1600, 2000);
    analogWrite(outputPinB, map(pulseLength, 1600, 2000, 0, 255));

}
  • Print the value of pulseLength to the serial monitor, is it what you think it should be ?
1 Like

Thank you very much for your help. I don't know anything about embedded code. I can only quote some simple ones.

Actually, I want to make Visal Phumint's

Because you can't code, you can only find data to piece together.

With what?

You may code whatever you want.

Do you understand, the silver box is a BLDC controller. Some of them only require 0vdc to 5vdc level to change motor speed.

So get the pulse length, print it as suggested to see its raw value is plausible, then just use if statements directly, or better yet, a switch/case statement with ranges, viz:

  switch (pukseLength) {
  case 1000 ... 1300 :
     // whatever turns both LEDs off

    break;

  case 1301 ... 1799 :
    // whatever turns LEDA on and LEDB off

    break;

  case 1800 ... 2000 :
    // whatever turns both LEDs on

    break;

  default :
    Serial.print("what? ");
    Serial.print(pukseLength);

    break;
  }

which isn't exact what you wrote, which frankly I can't make proper sense of. But key is that each case has to control both LEDs whether it be turning them on or off, as the pulse width can go betwee them in any order.

HTH

a7

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);
  }
  
}

I'm out of time for now.

Try following the advice in #2, #5 and #6 above. Use your best common sense. I'll check back, that will definitely be in the future. :expressionless:

a7

Thank you very much for your help. I plan to use Arduino UNO to control it. Some functions have been realized by using patchwork data, but at present, it is impossible to combine all functions together. As for the control voltage, BLDC controller needs 0.8VDC-4.2VDC control (the data obtained in the test may be different in different versions).

Thank you very much for your help. Code is not my strong point. I really won't. I can only try it by piecing together data.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.