Aukie Hooglugt / Mbed 2 deprecated EMG_Project

Dependencies:   HIDScope MODSERIAL- mbed-dsp mbed

Revision:
23:8d9a623dd713
Child:
24:c6073b9efd5b
diff -r 9dce7ec48f5d -r 8d9a623dd713 Project_main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Project_main.cpp	Fri Oct 03 09:20:24 2014 +0000
@@ -0,0 +1,161 @@
+#include "mbed.h"
+#include "MODSERIAL.h"
+#include "HIDScope.h"
+
+//Define objects
+AnalogIn    emg0(PTB1); //Analog input
+PwmOut      emgfloat(PTD4);//Float voor EMG-waarde
+PwmOut      red(LED_RED); //PWM output
+PwmOut      green(LED_GREEN);
+PwmOut      blue(LED_BLUE);
+int         direction = 0;
+int         force = 0;
+
+Ticker log_timer;
+MODSERIAL pc(USBTX,USBRX);
+HIDScope scope(2);
+
+/** Looper function
+* functions used for Ticker and Timeout should be of type void <name>(void)
+* i.e. no input arguments, no output arguments.
+* if you want to change a variable that you use in other places (for example in main)
+* you will have to make that variable global in order to be able to reach it both from
+* the function called at interrupt time, and in the main function.
+* To make a variable global, define it under the includes.
+* variables that are changed in the interrupt routine (written to) should be made
+* 'volatile' to let the compiler know that those values may change outside the current context.
+* i.e.: "volatile uint16_t emg_value;" instead of "uint16_t emg_value"
+* in the example below, the variable is not re-used in the main function, and is thus declared
+* local in the looper function only.
+**/
+void looper()
+{
+    /*variable to store value in*/
+    uint16_t emg_value;
+    /*put raw emg value both in emgfloat and in emg_value*/
+    emgfloat.write(emg0.read());      // read float value (0..1 = 0..3.3V)
+    emg_value = emg0.read_u16(); // read direct ADC result (0..4096 = 0..3.3V)
+    /*send value to PC. Line below is used to prevent buffer overrun */
+    if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30)
+        pc.printf("%u\n",emg_value);
+    scope.set(0,emg_value);
+    scope.set(1,emgfloat.read());
+    scope.send();
+    /**When not using the LED, the above could also have been done this way:
+    * pc.printf("%u\n", emg0.read_u16());
+    */
+}
+
+
+
+int main()
+{
+    /*setup baudrate. Choose the same in your program on PC side*/
+    pc.baud(115200);
+    /*set the period for the PWM to the emgfloat PTD4*/
+    emgfloat.period_ms(2);
+    /**Here you attach the 'void looper(void)' function to the Ticker object
+    * The looper() function will be called every 0.001 seconds.
+    * Please mind that the parentheses after looper are omitted when using attach.
+    */
+    log_timer.attach(looper, 0.001);
+    goto directionchoice;
+    while(1) { //Loop keuze DIRECTION
+        directionchoice:
+        for(int i=1; i<4; i++) {
+            if(i==1) {           //red
+                red=0;
+                green=1;
+                blue=1;
+                for (int lag=0; lag<50; lag++) {
+                    if(emgfloat.read()>0.8) {                    // 0.8 klopt niet als grenswaarde. #nofilter
+                        direction = 1;
+                        pc.printf("A");
+                        goto forcechoice;
+                    } else {
+                        wait(0.1);
+                    }
+                }
+            }
+            if(i==2) {           //green
+                red =1;
+                green=0;
+                blue=1;
+                for (int lag=0; lag<50; lag++) {
+                    if(emgfloat.read()>0.8) {                    //0.8 klopt niet als grenswaarde. #nofilter
+                        direction = 2;
+                        pc.printf("B");
+                        goto forcechoice;
+                    } else {
+                        wait(0.1);
+                    }
+                }
+            }
+            if(i==3) {           //blue
+                red=1;
+                green=1;
+                blue=0;
+                for (int lag=0; lag<50; lag++) {
+                    if(emgfloat.read()>0.8) {                    //0.8 klopt niet als grenswaarde. #nofilter
+                        direction = 3;
+                        pc.printf("C");
+                        goto forcechoice;
+                    } else {
+                        wait(0.1);
+                    }
+                }
+            }
+        }      
+    }
+    while(1) { //Loop keuze FORCE
+    forcechoice:
+        for(int i=1; i<4; i++) {
+            if(i==1) {           //red
+                red=0;
+                green=1;
+                blue=1;
+                for (int lag=0; lag<50; lag++) {
+                    if(emgfloat.read()>0.8) {                    // 0.8 klopt niet als grenswaarde. #nofilter
+                        force = 1;
+                        pc.printf("A");
+                        goto choicesmade;
+                    } else {
+                        wait(0.1);
+                    }
+                }
+            }
+            if(i==2) {           //green
+                red =1;
+                green=0;
+                blue=1;
+                for (int lag=0; lag<50; lag++) {
+                    if(emgfloat.read()>0.8) {                    //0.8 klopt niet als grenswaarde. #nofilter
+                        force = 2;
+                        pc.printf("B");
+                        goto choicesmade;
+                    } else {
+                        wait(0.1);
+                    }
+                }
+            }
+            if(i==3) {           //blue
+                red=1;
+                green=1;
+                blue=0;
+                for (int lag=0; lag<50; lag++) {
+                    if(emgfloat.read()>0.8) {                    //0.8 klopt niet als grenswaarde. #nofilter
+                        force = 3;
+                        pc.printf("C");
+                        goto choicesmade;
+                    } else {
+                        wait(0.1);
+                    }
+                }
+            }
+        }
+    }
+    choicesmade:
+    red = 0;
+    green = 0;
+    blue = 0;
+    }
\ No newline at end of file