code to drive the motors to the right position

Dependencies:   HIDScope QEI mbed

Fork of BMT-K9_potmeter_fade by First Last

Revision:
3:6a97e1d68511
Parent:
2:6a4a2e355cd9
Child:
4:e481566a5b54
--- a/main.cpp	Thu Sep 17 08:38:02 2015 +0000
+++ b/main.cpp	Wed Sep 23 09:44:06 2015 +0000
@@ -1,13 +1,13 @@
 #include "mbed.h"
 #include "HIDScope.h"
-
+#include "QEI.h"
 
 // myled is an object of class PwmOut. It uses the LED_RED pin
 // in human speech: myled is an output that can be controlled with PWM. LED_RED is the pin which is connected to the output
 PwmOut myled2(D5);
 PwmOut myled1(D6);
 
-//DigitalOut motor1(D4);
+DigitalOut motor1direction(D7);
 // pot is an object of class AnalogIn. It uses the PTB0 pin
 // in human speech: pot is an analog input. You can read the voltage on pin PTB0
 AnalogIn pot1(A0);
@@ -15,7 +15,11 @@
 
 //HIDScope scope(1);
 Serial pc(USBTX, USBRX);
-
+QEI wheel (D12, D13, NC, 624);
+float lastpotread = 0;
+int countsPerRound = 32*131;
+float gototick;
+int currentpulses;
 //start 'main' function. Should be done once in every C(++) program
 int main()
 {
@@ -23,13 +27,30 @@
     //period of PWM signal is 10kHz. Every 100 microsecond a new PWM period is started
     myled1.period_ms(0.1);
     myled2.period_ms(0.1);
+    myled1=0.5;
     //motor1=1;
     //while 1 is unequal to zero. For humans: loop forever
     while(1) {
-        pc.printf("potvalue: %f \n",pot1.read());
+        currentpulses=wheel.getPulses();
+        gototick = pot1.read()*countsPerRound;
+        if (lastpotread != pot1.read()){
+            lastpotread=pot1.read();
+            pc.printf("potvalue: %f, position: %d \n\r",gototick,currentpulses);
+        }
         
-        myled1.write(pot1.read());
-        myled2.write(pot2.read());
+        if (gototick > wheel.getPulses())
+        {
+            motor1direction=0;
+            
+        }
+        else
+        {
+            motor1direction=1;
+        }
+        
+        
+        //myled1.write(pot1.read());
+        //myled2.write(pot2.read());
         //wait some time to give the LED output a few PWM cycles. Otherwise a new value is written before the previously set PWM period (of 100microseconds) is finished
         //This loop executes at roughly 100Hz (1/0.01s)
         wait(0.01);