Sample Servo / LCD code for KL46Z. Display slider value on LCD and set servo position according to slider. Sample code for RS

Fork of Servo by Simon Ford

Demonstrate SLIDER, SERVO and LCD

Servo output is on port pin PTC1 Servo must be powered independently from KL46Z Delay of 1-2s out of reset caused by unknown issue.

main.cpp

Committer:
edware
Date:
2017-07-12
Revision:
5:b237e6e2ed77

File content as of revision 5:b237e6e2ed77:

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

/* This defines will be replaced by PinNames soon */
#if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
  #define ELEC0 9
  #define ELEC1 10
#elif defined (TARGET_KL05Z)
  #define ELEC0 9
  #define ELEC1 8
#else
  #error TARGET NOT DEFINED
#endif

Servo s1(PTC1);
//AnalogIn p1(PTB0);
SLCD slcd;
float f,g;

int main() {
    TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
    s1.calibrate(0.0009,220.0);
    f = 0;
#if 0
    while (true) {
        f = 0;
        f=tsi.readPercentage();
        while (f>1) f-=1;
        slcd.printf("%1.3f",f);
        wait(0.05f);
    }
#else
    while(1) {
        g =0;
        g=(float)tsi.readPercentage();
        while (g>1) g-=1;   // Workaround for bug. Ensures 0<=g<1
        g *= 1.2;           // Scale up by 20% for greater servo output range
        s1.write(g);        // Set servo position
        //printf(" %f \r",p1.read());
        slcd.printf("%1.3f",(float)g);  // Display value on LCD
        //wait(0.1f);
        f+=0.001;
    }
#endif    
}