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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Servo.h"
00003 #include "tsi_sensor.h"
00004 #include "SLCD.h"
00005 
00006 /* This defines will be replaced by PinNames soon */
00007 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
00008   #define ELEC0 9
00009   #define ELEC1 10
00010 #elif defined (TARGET_KL05Z)
00011   #define ELEC0 9
00012   #define ELEC1 8
00013 #else
00014   #error TARGET NOT DEFINED
00015 #endif
00016 
00017 Servo s1(PTC1);
00018 //AnalogIn p1(PTB0);
00019 SLCD slcd;
00020 float f,g;
00021 
00022 int main() {
00023     TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
00024     s1.calibrate(0.0009,220.0);
00025     f = 0;
00026 #if 0
00027     while (true) {
00028         f = 0;
00029         f=tsi.readPercentage();
00030         while (f>1) f-=1;
00031         slcd.printf("%1.3f",f);
00032         wait(0.05f);
00033     }
00034 #else
00035     while(1) {
00036         g =0;
00037         g=(float)tsi.readPercentage();
00038         while (g>1) g-=1;   // Workaround for bug. Ensures 0<=g<1
00039         g *= 1.2;           // Scale up by 20% for greater servo output range
00040         s1.write(g);        // Set servo position
00041         //printf(" %f \r",p1.read());
00042         slcd.printf("%1.3f",(float)g);  // Display value on LCD
00043         //wait(0.1f);
00044         f+=0.001;
00045     }
00046 #endif    
00047 }