added emg to milestone 1 but not completely working yet

Dependencies:   HIDScope MODSERIAL mbed

Fork of Milestone_1 by Casper Kroon

Revision:
3:f00ddd66fe39
Parent:
2:735ca8577f31
Child:
4:fe0b7e7b1de9
--- a/main.cpp	Fri Sep 28 10:53:57 2018 +0000
+++ b/main.cpp	Fri Sep 28 11:33:57 2018 +0000
@@ -6,7 +6,8 @@
 PwmOut led(D10);
 AnalogIn potmeter(A5);
 DigitalIn button(D2);
-DigitalOut directionpin(D5);
+DigitalOut directionpin(D7);
+MODSERIAL pc(USBTX, USBRX);
 
 HIDScope scope(2);
 Ticker ticker;
@@ -15,7 +16,8 @@
 
 volatile float x;
 volatile float y;
-volatile float p = potmeter;
+volatile float scaled_potmeter;
+volatile float c;
 
 void sendData() {
     scope.set(0,potmeter); //set the potmeter data to the first scope
@@ -23,40 +25,51 @@
     scope.send(); //send the datapoints of the 2 motors
 }
 
+void Proces_states(void){
+    switch (CurrentState){       
+        case forward:
+            directionpin = 1;
+            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 = 0;
+            c = scaled_potmeter*-1;
+            pwmpin.write(c); //pwm of motor is potmeter value
+            led.write(c); //led is potmeter value 
+            break; 
+    }            
+}
+
 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) {        
-        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);
+    while (true) {  
+        scaled_potmeter = (potmeter*2)-1; //scale potmeter from 0-1 to (-1 to 1)
+
+        if (scaled_potmeter > 0) {
+            CurrentState = forward;
+            pc.printf("state = forward\r\n");
+            Proces_states();
+        }
+        if (scaled_potmeter == 0) {
+            CurrentState = stop;
+            pc.printf("state = stop\r\n");
+            Proces_states(); 
+        }
+        if (scaled_potmeter < 0) {
+            CurrentState = backwards;
+            pc.printf("state = backwards\r\n");
+            Proces_states(); 
+        }   
+        wait(0.2f);
     }
 }