Motor + potmerer + EMG + led

Dependencies:   HIDScope MODSERIAL mbed

Fork of EMG_Motor_LED by Michel Vos

Revision:
2:735ca8577f31
Parent:
1:aa505856416d
Child:
3:f00ddd66fe39
--- a/main.cpp	Tue Sep 25 14:50:26 2018 +0000
+++ b/main.cpp	Fri Sep 28 10:53:57 2018 +0000
@@ -10,9 +10,12 @@
 
 HIDScope scope(2);
 Ticker ticker;
+enum states{forward, stop, backwards};
+states CurrentState;
 
 volatile float x;
 volatile float y;
+volatile float p = potmeter;
 
 void sendData() {
     scope.set(0,potmeter); //set the potmeter data to the first scope
@@ -20,18 +23,41 @@
     scope.send(); //send the datapoints of the 2 motors
 }
 
-int main() {  
-    float u = -0.3f; //determineusefulvalue, -0.3f is justanexample
-    directionpin = u > 0.0f; //if bigger than 0, gives true, otherwise gives false   
+int main() {
+      
+//    float u = -0.3f; //determineusefulvalue, -0.3f is justanexample
+//    directionpin = u > 0; //if bigger than 0, gives true, otherwise gives false   
     x = 1; //placeholder value for potmeter of second motor
     
+    float scaled_potmeter = (p*2)-1; //scale potmeter from 0-1 to (-1 to 1)
+    float c; //value to be used in negative scaled potmeter value
+    bool forward = scaled_potmeter > 0;
+    bool stop = scaled_potmeter == 0;
+    bool backwards = scaled_potmeter < 0;
+    
     pwmpin.period_us(60); //60 microseconds PWM period, 16.7 kHz
     led.period_us(60); //60 microseconds
     ticker.attach(&sendData, 0.005f); //send data to hidscope at 200Hz
 
-    while (true) {
-        pwmpin.write(potmeter); //pwm of motor is potmeter value
-        led.write(potmeter); //led is potmeter value
-        wait(0.2f);
+    while (true) {        
+        switch (CurrentState){       
+            case forward:
+                directionpin = true;
+                pwmpin.write(scaled_potmeter); //pwm of motor is potmeter value
+                led.write(scaled_potmeter); //led is potmeter value  
+                break;
+            case stop:
+                // do nothing
+                break;
+            case backwards:
+                directionpin = false;
+                c = scaled_potmeter*-1;
+                pwmpin.write(c); //pwm of motor is potmeter value
+                led.write(c); //led is potmeter value 
+                break; 
+        }            
+        wait(1.0f);
     }
-}
\ No newline at end of file
+}
+
+