My first big writted program ;)

Dependencies:   mbed

Revision:
0:f86463132427
Child:
1:d3366745d5ec
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Dec 26 05:34:03 2016 +0000
@@ -0,0 +1,249 @@
+#include "mbed.h"
+
+Serial rs232(USBTX, USBRX);
+
+DigitalOut led[8] {PC_3, PC_2, PA_1, PA_0, PA_15, PC_12, PC_13, PB_7};
+Timer knight, tree;
+char user_input = 0;
+void ledintilize()
+{
+    for(int x=0; x<8; x++) {
+        led[x] = 1;
+    }
+}
+void pin_clear_state()
+{
+    led[0] = led[1] = led[2] = led[3] = led[4] = led[5] = led[6] = led[7] = 1;
+}
+void led_knight()
+{
+    switch(knight.read_ms()) {
+        case 500:
+            pin_clear_state();
+            led[0] = 0;
+            break;
+        case 1000:
+            led[0] = 1;
+            led[1] = 0;
+            break;
+        case 1500:
+            led[1] = 1;
+            led[2] = 0;
+            break;
+        case 2000:
+            led[2] = 1;
+            led[3] = 0;
+            break;
+        case 2500:
+            led[3] = 1;
+            led[4] = 0;
+            break;
+        case 3000:
+            led[4] = 1;
+            led[5] = 0;
+            break;
+        case 3500:
+            led[5] = 1;
+            led[6] = 0;
+            break;
+        case 4000:
+            led[6] = 1;
+            led[7] = 0;
+            break;
+        case 4500:
+            led[7] = 1;
+            knight.reset();
+            break;
+    }
+}
+
+void led_knight2()
+{
+    switch(knight.read_ms()) {
+        case 500:
+            pin_clear_state();
+            led[0] = 0;
+            break;
+        case 1000:
+            led[0] = 1;
+            led[1] = 0;
+            break;
+        case 1500:
+            led[1] = 1;
+            led[2] = 0;
+            break;
+        case 2000:
+            led[2] = 1;
+            led[3] = 0;
+            break;
+        case 2500:
+            led[3] = 1;
+            led[4] = 0;
+            break;
+        case 3000:
+            led[4] = 1;
+            led[5] = 0;
+            break;
+        case 3500:
+            led[5] = 1;
+            led[6] = 0;
+            break;
+        case 4000:
+            led[6] = 1;
+            led[7] = 0;
+            break;
+        case 4500:
+            led[7] = 1;
+            break;
+
+        case 5000:
+            led[7] = 1;
+            led[6] = 0;
+            break;
+        case 5500:
+            led[6] = 1;
+            led[5] = 0;
+            break;
+        case 6000:
+            led[5] = 1;
+            led[4] = 0;
+            break;
+        case 6500:
+            led[4] = 1;
+            led[3] = 0;
+            break;
+        case 7000:
+            led[3] = 1;
+            led[2] = 0;
+            break;
+        case 7500:
+            led[2] = 1;
+            led[1] = 0;
+            break;
+        case 8000:
+            led[1] = 1;
+            led[0] = 0;
+            break;
+        case 8500:
+            led[0] = 1;
+            knight.reset();
+            break;
+    }
+
+}
+void  knight3()
+{
+    switch(knight.read_ms()) {
+        case 500:
+            pin_clear_state();
+            // led[7] = 1;
+            led[6] = 0;
+            break;
+        case 1000:
+            led[6] = 1;
+            led[5] = 0;
+            break;
+        case 1500:
+            led[5] = 1;
+            led[4] = 0;
+            break;
+        case 2000:
+            led[4] = 1;
+            led[3] = 0;
+            break;
+        case 25000:
+            led[3] = 1;
+            led[2] = 0;
+            break;
+        case 3000:
+            led[2] = 1;
+            led[1] = 0;
+            break;
+        case 35000:
+            led[1] = 1;
+            led[0] = 0;
+            break;
+        case 4000:
+            led[0] = 1;
+            knight.reset();
+            break;
+    }
+}
+
+void police_light()
+{
+    if(knight.read_ms() >= 200) {
+        pin_clear_state();
+        //led[4] = led[5] = led[6] = led[7] = 1;
+        led[0] = led[1] = led[2] = led[3] = 0;
+        if(knight.read_ms() >= 400) {
+            led[0] = led[1] = led[2] = led[3] = 1;
+            led[4] = led[5] = led[6] = led[7] = 0;
+            knight.reset();
+        }
+    }
+}
+
+void police_light2()
+{
+    if(knight.read_ms() >= 200) {
+        pin_clear_state();
+        //led[1] = led[3] = led[5] = led[7] = 1;
+        led[0] = led[2] = led[4] = led[6] = 0;
+        if(knight.read_ms() >= 400) {
+            led[0] = led[2] = led[4] = led[6] = 1;
+            led[1] = led[3] = led[5] = led[7] = 0;
+            knight.reset();
+        }
+    }
+}
+
+
+
+void rs232in()
+{
+    if(rs232.readable()) {
+        user_input = rs232.getc();
+    }
+}
+int main()
+{
+    ledintilize();
+    rs232.attach(&rs232in);
+
+    while(1) {
+
+
+
+        switch(user_input) {
+
+
+            case '1':
+
+                knight.start();
+                led_knight();
+                break;
+            case '2':
+
+                knight.start();
+                led_knight2();
+                break;
+
+            case '3':
+                knight.start();
+                knight3();
+                break;
+
+            case '4':
+                knight.start();
+                police_light();
+                break;
+
+            case '5':
+                knight.start();
+                police_light2();
+                break;
+
+        }
+    }
+}
\ No newline at end of file