Skip to end of metadata
Go to start of metadata

You are viewing an old version of this content. View the current version.

Compare with Current View Version History

Version 1 Next »

Purpose

Sets a PWM Pin to a desired value.

When to use

Every time you want the value of a PWM pin to be modified.

Where to use

Most probably in the void loop() part of your .ino file. Can also be used at the beginning of your code execution, in the void setup() part of your .ino file, so that the pin is in a known state on startup.

Click here to learn more on the PWM outputs of the 9880 board. It explains in particular why PWM port #1 to #4 can only be used with power motor controller.

This function requires the use of the following functions at some point of your .ino file in order to work properly:

Returns

This function does not return a value once it has completed its tasks.

Main prototype and parameters

static void Crc::CrcLib::SetPwmOutput(unsigned char pin, char value)

The following parameters must be passed to the function for it to work properly:

  • pin: The name of the PWM pin you want to set. Must be of the type unsigned char. Must have been initialized using InitializePwmOutput() beforehand.

  • value: The value you want to set the pin to. Must be of the type signed char, so in the range  [-127, 127].

    • Setting a pin to -127 will have the pin generating a pulse equal to the minimum pulse width previously set by InitializePwmOutput().

    • Setting a pin to 127 will have the pin generating a pulse equal to the maximum pulse width previously set by InitializePwmOutput().

    • Setting a pin to other values will have the pin generating a pulse mapped between the minimum and maximum pulse width previously set by InitializePwmOutput().

Overloads

This function does not have any overloads. It can only be used as described by the main prototype.

Examples

 #1: We want a 3-wire VEX continuous servo, wired to PWM port #5, to continuously run at full speed forward during the entire code execution.
#include <CrcLib.h>

using namespace Crc;

void setup() {
    CrcLib::Initialize();

    CrcLib::InitializePwmOutput(CRC_PWM_5);
    CrcLib::SetPwmOutput(CRC_PWM_5,127);
    /* 
    The rest of your setup code
    ...
     */
}
void loop() {
    CrcLib::Update();

    /* 
    The rest of your looping code
    ...
    */
}
 #2: We want the angle of a hobby type standard servo (Manufacturer: FEETECH, Part number: FS5115M) hooked up to PWM Port #12 to be directly controlled by the X axis of the left remote controller joystick. An internet search reveals the required pulse of that Servo is 0.5ms-2.5ms. This is called "binding a joystick to an output".
#include <CrcLib.h>

using namespace Crc;

signed char joystickValue;

void setup() {
    CrcLib::Initialize();
    
    CrcLib::InitializePwmOutput(CRC_PWM_12, 500, 2500);
    
    /* 
    The rest of your setup code
    ...
     */
}
void loop() {
    CrcLib::Update();

    joystickValue = CrcLib::ReadAnalogChannel(ANALOG::JOYSTICK1_X);
    CrcLib::SetPwmOutput(CRC_PWM_12, joystickValue );
    /* 
    The rest of your looping code
    ...
    */
}

 

More on this function

Can I use the native Arduino function analogWrite() to control the PWM outputs ?

You could, but we strongly recommend against it if you don’t know what you are doing and don’t have a precise reason to do so. The analogWrite() native Arduino function controls PWM pins using the “normal” definition of PWM. Although PWM pins on the CrcDuino are PWM capable pins, they are used as Servo PWM pins by CrcLib, since the CRC Robotics Competition robots mostly use hobby type standard servos, continuous servo and PWM compatible power motor controllers. For more info on this, read this.

  • No labels