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.

Files at this revision

API Documentation at this revision

Comitter:
edware
Date:
Wed Jul 12 04:38:54 2017 +0000
Parent:
4:66e69779ecc3
Commit message:
Sample Servo / LCD code for KL46Z.; Display slider value on LCD and set servo position according to slider.; Sample code for RS

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 66e69779ecc3 -r b237e6e2ed77 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jul 12 04:38:54 2017 +0000
@@ -0,0 +1,47 @@
+#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    
+}