This FRDM-KL25Z demo will blink the RGB LED with all colors. The touch slider input controls the blinking frequency. The UART output shows the touch slider input.

Dependencies:   TSI mbed

main.cpp

Committer:
jewirth
Date:
2014-05-31
Revision:
1:609d9df3ffdc
Parent:
0:6f33cc0cd2ac
Child:
2:61fcde3ffe6e

File content as of revision 1:609d9df3ffdc:

#include "mbed.h"
#include "TSISensor.h"

#define LED_ON                  0
#define LED_OFF                 1
#define RGB_COMPONENTS          3
#define RGB_COLORS              ((int) pow(2 *1.0, RGB_COMPONENTS *1.0))
#define BAUDRATE                115200
#define MAXDELAY                0.3

int main()
{
    // RGB LED
    DigitalOut led_rgb_red(LED1);   // red
    DigitalOut led_rgb_grn(LED2);   // green
    DigitalOut led_rgb_blu(LED3);   // blue
    DigitalOut led_rgb[RGB_COMPONENTS] = {led_rgb_red, led_rgb_grn, led_rgb_blu};
    float delay  = MAXDELAY; // blink delay

    // Touch slider
    TSISensor tsi;
    float sliderNewValue;

    // UART
    RawSerial pc(USBTX, USBRX);
    char tmpString[64]; // text buffer
    pc.baud(BAUDRATE);

    while (true)
    {
        // show all colors
        for (int i=1; i<RGB_COLORS; i++, wait(delay))   // for each possible color
        {
            for (int j=0; j<RGB_COMPONENTS; j++)            // for each LED component
                led_rgb[j] = ( i & 1<<j ) ? LED_ON : LED_OFF;   // set LED component according to active color
                
            // check for slider input
            if ( (sliderNewValue = tsi.readPercentage()) > 0)
            {
                // set delay to slider input
                delay = MAXDELAY * sliderNewValue;
                
                // UART output (NOTE: practically, this will increase the blink delay)
                sprintf(tmpString, "slider input = %3.0f%%  -->  ", 100.0 * sliderNewValue);
                pc.puts(tmpString);
                sprintf(tmpString, "blink delay = %3.0f ms\n", 1000 * delay);
                pc.puts(tmpString);
            }
        }
    }
}