mag niet van hendrik D:

Dependencies:   mbed MatrixMath QEI HIDScope Matrix biquadFilter MODSERIAL FastPWM

Revision:
15:80b3ac2b8448
Parent:
14:20f11bb58244
Child:
16:40183eeadb6d
diff -r 20f11bb58244 -r 80b3ac2b8448 main.cpp
--- a/main.cpp	Mon Sep 23 08:17:52 2019 +0000
+++ b/main.cpp	Mon Sep 23 11:08:17 2019 +0000
@@ -1,10 +1,19 @@
-#include "mbed.h"
+#include "mbed.h"
+#include "MODSERIAL.h"
 #include "FastPWM.h"
 
 // Motor 1 aan zetten en uit zetten dmv button 1 en 2 werkt.
 // Snelheid aansturen nog niet.
 
-Serial pc(USBTX,USBRX);
+MODSERIAL pc(USBTX, USBRX);
+DigitalOut ledr(LED_RED);
+DigitalOut ledg(LED_GREEN);
+DigitalOut ledb(LED_BLUE);
+Ticker RE;
+Timer R;
+Timer G;
+Timer B;
+
 InterruptIn BUT1(D1);
 InterruptIn BUT2(D0);
 FastPWM lichtje(D3);
@@ -12,40 +21,143 @@
 DigitalOut direction(D6);
 DigitalOut speed(D7);
 
-float getal;
+// variables
+
+volatile char command = 'r';
+enum states {red, green, blue};
+
+states CurrentState = red;
+bool StateChanged = true;
+
+double numerator;
+double denominator;
+double brightness;
+
 int n=5;
 
+float speedy;
+
+// functions
+
+void ES()
+{
+    command = 'r';
+}
+
+void CheckCommandFromTerminal(void)
+{
+    command = pc.getcNb();
+}
+
+void StateMachine(void)
+{
+    switch(CurrentState) {
+        case red:
+            if  (StateChanged) {
+                pc.printf("Initiating turning LED red\n\r");
+                StateChanged= false;
+                ledr = 0;
+                ledg = 1;
+                ledb = 1;
+                R.start();
+                direction = 0;
+                pc.printf("LED is now red!\n\r");
+            }
+            if (command == 'g') {
+                R.stop();
+                pc.printf("The LED has been red for %f seconds!\n\r", R.read());
+                CurrentState= green;
+                StateChanged= true;
+            }
+            if (command == 'b') {
+                R.stop();
+                pc.printf("The LED has been red for %f seconds!\n\r", R.read());
+                CurrentState= blue;
+                StateChanged= true;
+            }
+            break;
+        case green:
+            if  (StateChanged) {
+                pc.printf("Initiating turning LED green\n\r");
+                StateChanged= false;
+                ledr = 1;
+                ledg = 0;
+                ledb = 1;
+                G.start();
+                direction = 1;
+                pc.printf("LED is now green!\n\r");
+            }
+            if (command == 'r') {
+                G.stop();
+                pc.printf("The LED has been green for %f seconds!\n\r", G.read());
+                CurrentState= red;
+                StateChanged= true;
+            }
+            if (command == 'b') {
+                G.stop();
+                pc.printf("The LED has been green for %f seconds!\n\r", G.read());
+                CurrentState= blue;
+                StateChanged= true;
+            }
+            break;
+        case blue:
+            if  (StateChanged) {
+                pc.printf("Initiating turning LED blue\n\r");
+                StateChanged= false;
+                ledr = 1;
+                ledg = 1;
+                ledb = 0;
+                B.start();
+                //direction = 1;
+                speed = 255;
+                pc.printf("LED is now blue!\n\r");
+            }
+            if (command == 'r') {
+                B.stop();
+                pc.printf("The LED has been blue for %f seconds!\n\r", B.read());
+                CurrentState= red;
+                StateChanged= true;
+            }
+            if (command == 'g') {
+                B.stop();
+                pc.printf("The LED has been blue for %f seconds!\n\r", B.read());
+                CurrentState= green;
+                StateChanged= true;
+            }
+            break;
+        default:
+            pc.printf("Unknown or unimplemented state reached!\n\r");
+    }
+}
+
 void plus()
 {
     n++; // n=n+1
-    direction = 1;
-    if (n>10){
+    if (n>10) {
         n=10;
-        }
     }
-    
+}
+
 void min()
 {
     n--;
-    direction = 0;
-    if (n<0){
+    if (n<0) {
         n=0;
-        }
     }
+}
 
-int main() {
+// main
+
+int main()
+{
     pc.baud(115200);
-    lichtje=1;
+    RE.attach(ES,3);
     BUT1.fall(plus);
     BUT2.fall(min);
-        while(true)
-            {
-                    getal = 0.1*n*ain.read();
-                    speed = 0.1*n*ain.read();
-                    lichtje.period_ms(20);  // 4 second period
-                    lichtje.write(getal);  // duty cycle
-                    speed.write(getal);
-                    pc.printf("%3.3f%\n\r", getal);
-        
+    //lichtje.period_ms(20);    // Wat is hier het praktisch nu van?
+    while(true) {
+        CheckCommandFromTerminal();
+        StateMachine();
+        lichtje.write(ain.read());  // duty cycle
     }
 }
\ No newline at end of file