0
$\begingroup$

How might I be able to control one function (like brightness control of an LED) with two different triggers (like a tactile switch and an IR remote)?

I am trying to be able to control the brightness with switches as well as IR remote when desired.

$\endgroup$
  • $\begingroup$ A "trigger" implies an event, or at best a binary on/off state. Brightness is a continuous value between 2 endpoints (e.g. 0-255). Are you simply asking how to hand off control in an electrical circuit between 2 inputs? $\endgroup$ – Ian Jun 17 '14 at 17:53
2
$\begingroup$

I'm assuming you are doing this with a microcontroller.

The solution is very simple. Let's say you know how to do them individually. In the most general case, this would be:

  • For the tactile sensor:

    /* get the value from tactile sensor */
    unsigned int value_from_tactile = read_tactile();
    
    /* convert to range of LED brightness (formula is just random example) */
    unsigned int brightness = 100 - value_from_tactile * 100 / 256;
    
    write_brightness(brightness);
    
  • For IR remote:

    /* get the value from IR sensor */
    unsigned int value_from_ir = read_ir();
    
    /* convert to range of LED brightness (formula is just random example) */
    unsigned int brightness = value_from_ir * 100 / 65536;
    
    write_brightness(brightness);
    

which are generally the same code, even though each is implemented with a different peripheral, e.g. one may use ADC, another I2C etc.

The problem is now how to merge them. Before getting to code, we should first define the exact behavior of the system. This is really really dependent on the goal of the system.

Let's put this in mathematical terms. The examples above translate to:

  • For the tactile sensor:

    $b_1(x) = 100 - x * 100 / 256$

  • For the IR remote:

    $b_2(y) = y * 100 / 65536$

where $b_N$ is the brightness of the LED, $x$ is the value of the tactile sensor and $y$ is the value of the IR sensor. Obviously, these two functions conflict. They drive the same output with different formulas.

The question is, what would the function $b(x, y)$ look like, where it takes into account both $x$ and $y$? The answer is that you should decide it based on your systems. Some examples would be:

  1. $b(x, y) = max(b_1(x), b_2(y))$: This means whoever wants the LED to have a higher value wins.
  2. $b(x, y) = (b_1(x) + b_2(y)) / 2$: The brightness of the LED is determined equally by both sensors.
  3. $b(x, y) = (N * b_1(x) + M * b_2(y)) / (N + M)$: One of the sensors has more control over the brightness of the LED.
  4. $b(x, y) = min(100, b_1(x) + b_2(y))$: Both sensors can drive the brightness of the LED to its maximum individually, but they can also work together.
  5. $b(x, y) = \left\{ \begin{array}{l l} b_1(x) & \quad \text{if $b_2(y) == 0$}\\ b_2(y) & \quad \text{o.w.} \end{array} \right.$: Give priority to IR sensor and only control through touch if not being controlled by IR.

There are many other ways you can think of combining the two. Whichever you choose, the code would be straightforward. We just put the two pieces of code from before together, and then add the chosen merge function:

/* get the value from tactile and IR sensors */
unsigned int value_from_tactile = read_tactile();
unsigned int value_from_ir = read_ir();

/* convert to range of LED brightness (formula is just random example) */
unsigned int brightness_1 = 100 - value_from_tactile * 100 / 256;
unsigned int brightness_2 = value_from_ir * 100 / 65536;

/* merge */
unsigned int brightness;
switch (strategy)
{
    case 1:
    default:
        brightness = brightness_1 < brightness_2?brightness_2:brightness_1;
        break;
    case 2:
        brightness = (brightness_1 + brightness_2) / 2;
        break;
    case 3:
        brightness = (N * brightness_1 + M * brightness_2) / (N + M);
        break;
    case 4:
        brightness = brightness_1 + brightness_2;
        if (brightness > 100) brightness = 100;
        break;
    case 5:
        brightness = brightness_2 == 0?brightness_1:brightness_2;
        break;
}

write_brightness(brightness);
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.