Introduction: Getting Started With FRDM Kl46z Part 3 - LCD, Slider & PWM

In third part I want to show You how to use Touch Slider with PWM and LCD.

We will see how change value on LCD and PWM on leds during sliding finger.

Step 1: LCD

To use LCD we have to click right mouse button on our project's name and 'Import library' -> 'From Import Wizard' then search SLCD library and add it to the project. The second and LAST thing you have to do to use LCD is to include lcd library by #include. That's all ! You even don't have to initialize lcd because defaul t constructor do it for us. We can also open downloaded library and see other functions to use. For example we can change the contrast or dot place.

Step 2: TSI Sensor and PWM

To use touch slider we have to add tsi_sensor library in the same way as we added lcd library. Then include tsi_sensor.h and create object of TSIAnalogSlider with arguments which are our TSI pins and range. As we can see on picture above these pins are PTB16 and PTB17. Function which indicates TSI_sensor value is readPercentage(), or readDistance().

For PWM we don't have to add library, because in mbed.h there is PWMOut. We create an object with LED_COLOR as an argument.

Step 3: Writing a Programme

It's very short and quite easy code which can be used i bigger projects.

#include "mbed.h"
#include "SLCD.h" #include "tsi_sensor.h"

SLCD slcd; TSIAnalogSlider slider(PTB16, PTB17, 100); // touch sensor

PwmOut gLed(LED_GREEN); // pwm out PwmOut rLed(LED_RED);

int main() { slcd.printf("lcd "); wait(2); // delay 2 sec while (1) { slcd.CharPosition = 0; // if we don't use it the value on the screen will be sliding slcd.printf("%1.2f",slider.readPercentage()); // print TSI_sensor value on LCD rLed = slider.readPercentage(); // set TSI_value to the PWM linked with LED gLed = 1.0 - slider.readPercentage(); wait_ms(10); } }