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 Digital Output (DIO) Pin to a desired value.

When to use

Every time you want the state of a DO 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 DIOs of the 9880 board.

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::SetDigitalOutput(unsigned char pin, unsigned char value)

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

  • pin: The name of the digital pin you want to set. Must be of the type unsigned char. Must have been initialized as an output using CrcLib::SetDigitalPinMode() beforehand.

  • value: The value you want to set the pin to. Must be of the type unsigned char. Needs to be one of the following:

    • LOW, to output 0V on the given DO pin.

    • HIGH, to output 5V on the given DO pin.

Overloads

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

Examples

 #1: We want to output 0V on Digital Port #1 when the green triangle button on the ps3 remote controller is pressed, and 5V when it's not. LONG VERSION
#include <CrcLib.h>

using namespace Crc;

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

    //Set up DIO Port 1 as an Output
    CrcLib::SetDigitalPinMode(CRC_DIG_1, OUTPUT);
    
    /* 
    The rest of your setup code
    ...
     */
}
void loop() {
    CrcLib::Update();

    //Check Green Triangle button state (on a ps3 controller)
    if(CrcLib::ReadDigitalChannel(BUTTON::COLORS_UP)==HIGH) //Button is pressed
    {
      CrcLib::SetDigitalOutput(CRC_DIG_1, LOW);   //Output 0V on pin
    }
    if(CrcLib::ReadDigitalChannel(BUTTON::COLORS_UP)==LOW) //Button is not pressed
    {
      CrcLib::SetDigitalOutput(CRC_DIG_1, HIGH);   //Output 5V on pin
    }
    
    /* 
    The rest of your looping code
    ...
    */
}
 #2: We want to output 0V on Digital Port #1 when the green triangle button on the ps3 remote controller is pressed, and 5V when it's not. SHORT VERSION
#include <CrcLib.h>

using namespace Crc;

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

    //Set up DIO Port 1 as an Output
    CrcLib::SetDigitalPinMode(CRC_DIG_1, OUTPUT);
    
    /* 
    The rest of your setup code
    ...
     */
}
void loop() {
    CrcLib::Update();

    //Mirrors the reversed state of the Green Triangle button (on a ps3 controller) to DIO Port 1
    CrcLib::SetDigitalOutput(CRC_DIG_1, !(CrcLib::ReadDigitalChannel(BUTTON::COLORS_UP)));

    /* 
    The rest of your looping code
    ...
    */
}

More on this function

Why not use the native Arduino function digitalWrite() instead of CrcLib::SetDigitalOutput() ?

We recommend using CrcLib::SetDigitalOutput() because it adds a few layers of validation to make sure you don't assign a value to an unsafe pin.

How does Example #2 work ?

In the Arduino world, LOW means 0 and HIGH means 1. In C/C++, 0 means False, and every other value means TRUE. For more information on the meaning of LOW and HIGH, click here.

CrcLib::ReadDigitalChannel(), when executed, returns the value of the remote controller button referred to in argument. The returned value is a bool, meaning in the Arduino world that it’s either LOW or HIGH. If the button is pressed on the remote controller, HIGH is returned, and LOW is returned if it’s not. The means that, when the button is pressed, the line of code will be executed as the following: CrcLib::SetDigitalOutput(CRC_DIG_1, !(HIGH));

The ! operator, called Logical NOT, reverses the logical state of what’s following. TRUE becomes False, and vice-versa. The means that, when the button is pressed, the line of code will in fact be executed as the following: CrcLib::SetDigitalOutput(CRC_DIG_1, LOW);

Therefore, the line CrcLib::SetDigitalOutput(CRC_DIG_1, !(CrcLib::ReadDigitalChannel(BUTTON::COLORS_UP))); will have, on every void loop() execution, the CRC_DIG_1 port mirroring the reversed value of the Green Triangle Button.

  • No labels