Robot
Dependencies: HIDScope MODSERIAL Motordriver QEI Servo mbed
Fork of The_Claw_with_EMG_Control_PID by
Diff: main.cpp
- Revision:
- 2:ad4b181a6422
- Parent:
- 1:1d2208dce484
- Child:
- 3:3a671d01bcb8
--- a/main.cpp Tue Sep 20 08:43:33 2016 +0000 +++ b/main.cpp Wed Oct 19 11:03:34 2016 +0000 @@ -1,17 +1,3 @@ -/** - * Minor BioRobotics 2016 - * - * Programming Homework Set 2 - * Exercise 2, example solution. - * - * Note that this problem can be solved in *much* cleaner ways, using slightly - * more advanced C++ / programming techniques. However, this example shows that - * even with very basic programming skills quite a lot can be achieved. - * - * M.E. Grootens [at] utwente.nl, - * v0.1, 16.09.2016 - */ - #include "mbed.h" // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = > @@ -27,16 +13,15 @@ // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = > // constants -const int kNumLeds = 3; // three LEDs const int kLedOn = 0; // LED on if 0 // LEDs -DigitalOut led_0(LED_RED); -DigitalOut led_1(LED_GREEN); -DigitalOut led_2(LED_BLUE); +DigitalOut led_r(LED_RED); +DigitalOut led_g(LED_GREEN); +DigitalOut led_b(LED_BLUE); // ID of led that should blink; *volatile* because changed by interrupt -volatile int led_blink_id = 0; +volatile int part_id = 0; // counters int num_turned_on_0 = 0; // count number of times red LED turned on @@ -54,43 +39,43 @@ */ void ToggleLed() { - switch (led_blink_id) { + switch (part_id) { case 0: { // LED 0 to toggle and count: - led_0 = not led_0; - if (led_0 == kLedOn) { + led_r = kLedOn; + if (led_r == kLedOn) { num_turned_on_0++; } // LEDs to turn off: - led_1 = not kLedOn; - led_2 = not kLedOn; + led_g = not kLedOn; + led_b = not kLedOn; break; } case 1: { // LED 1 to toggle and count: - led_1 = not led_1; - if (led_1 == kLedOn) { + led_g = kLedOn; + if (led_g == kLedOn) { num_turned_on_1++; } // LEDs to turn off: - led_0 = not kLedOn; - led_2 = not kLedOn; + led_r = not kLedOn; + led_b = not kLedOn; break; } case 2: { // LED 2 to toggle and count: - led_2 = not led_2; - if (led_2 == kLedOn) { + led_b = kLedOn; + if (led_b == kLedOn) { num_turned_on_2++; } // LEDs to turn off: - led_0 = not kLedOn; - led_1 = not kLedOn; + led_r = not kLedOn; + led_g = not kLedOn; break; } @@ -111,14 +96,20 @@ * led_blink_id goes from 0 -> 1 -> 2 -> 0 -> ... * @ensure led_blink_id = ++led_blink_id % kNumStates */ -void SwitchLedColor() { - led_blink_id++; - if (led_blink_id>=kNumLeds) { - // could also use modulo ('%') operator - led_blink_id = 0; - } +void SetValue2() { + part_id = 0; + } + +void SetValue3() { + part_id = 1; + } + + +void SetValue4() { + part_id = 2; } + /** * Main loop. */ @@ -129,9 +120,9 @@ pc.printf("\r\n**RESET**\r\n"); // Turn off all LEDs initially - led_0 = not kLedOn; - led_1 = not kLedOn; - led_2 = not kLedOn; + led_r = not kLedOn; + led_g = not kLedOn; + led_b = not kLedOn; // Create ticker and attach LED toggle function Ticker tick_toggle_led; @@ -142,8 +133,17 @@ tick_print_serial.attach(&PrintSerial,kTimePrintSerial); // Create interrupt and attach switch function - InterruptIn sw2(SW2); - sw2.fall(&SwitchLedColor); + InterruptIn btn_base(D2); + btn_base.fall(&SetValue2); + InterruptIn btn_cart(D3); + btn_cart.fall(&SetValue3); + + InterruptIn btn_arm(D4); + btn_arm.fall(&SetValue4); + while (true); } + + + \ No newline at end of file