6 years, 10 months ago.

If statement with pwmin

I am using the l432kc nucleo board and am wondering how do I make an in if statement with pwmin to turn on a led when using a rc controller. Here's what i came up with so far however the led dosent react when i press the controller.

DigitalOut myled(LED3);

PwmIn a(D9); pin receiving the pwm signal

int main() {

while(1) {

if (a> 0.001509) 0.001509 is the pulsewidth reading when rc controller not pressed {

myled = 1;

}

}

}

2 Answers

6 years, 9 months ago.

Hello, it might be that the LED for this board idles high, such that 1 is actually the OFF state. Try to set the LED to 0.

Accepted Answer

the led for the board will be off all the way no matter what i press (sorry for the late reply)

posted by amos seah 10 Jul 2017

It may help to turn the LED off at some point:

if (a> 0.001509)
  myled = 1;
else
  myled = 0;

Or even

myled = (a> 0.001509);
posted by Andy A 10 Jul 2017

thanks! it worked

posted by amos seah 12 Jul 2017
6 years, 9 months ago.

I assume that you are using Simon Ford’s PwmIn library. It would help if you verified that and perhaps included more of your code.

It would also help if you would provide some information on your r/c receiver and how you connected the r/c receiver to your Nucleo board. For example, most r/c receivers use 5V logic levels and probably require some sort of logic level converter for compatibility with the 3.3V logic levels of MCU on the Nucleo board. Fortunately, PA_8 (pin D9) on the L432KC board is 5v tolerant so you probably did not damage the L432KC MCU if you did connect a 5V r/c receiver output directly to the Nucleo board.

I can verify that PA_8 (pin D9) on the L432KC board works fine with the mbed InteruptIn code, so it should be compatible with Simon’s PwmIn library. I can also verify that LD3 is ON when PB_3 (pin D13) is HIGH (logic level 1) and that LED3 is one of the generic signal names for PB_3, which drives LD3.

I suggest you first try setting up a loop-back (mbed pwm out driving your PwmIn code) similar to Simon’s "PmwIn_HelloWorld" example. Examining the .pulsewidth and .period properties of the PwmIn object will probably tell you more than just looking at the LED. First get the example code working and then you may want to modify the "PmwIn_HelloWorld" example to more accurately simulate the expected PWM signals from your r/c receiver - see the "Control a R/C model servo" example in the PwmOut documentation in mbed OS 2 Handbook. Be careful not to use one of the inverted pwm outputs on the L432KC board (PB_6 = D5, for example), or else you will not get the correct duty cycle. This loop-back testing will give you some confidence that the mbed side of things is working as expected.

yep solved it i accidentally added a ; after my if statement

posted by amos seah 12 Jul 2017