fake coffee machine
Dependencies: Servo TextLCD mbed
main.cpp@1:dd54f5e7db5c, 2014-12-16 (annotated)
- Committer:
- tuffnatty
- Date:
- Tue Dec 16 16:56:58 2014 +0000
- Revision:
- 1:dd54f5e7db5c
- Parent:
- 0:2886eabcadbc
new variant
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tuffnatty | 0:2886eabcadbc | 1 | #include "mbed.h" |
tuffnatty | 0:2886eabcadbc | 2 | #include "TextLCD.h" |
tuffnatty | 0:2886eabcadbc | 3 | #include "Servo.h" |
tuffnatty | 0:2886eabcadbc | 4 | |
tuffnatty | 0:2886eabcadbc | 5 | #define PIN_LCD_4 PB_8 |
tuffnatty | 0:2886eabcadbc | 6 | #define PIN_LCD_6 PB_9 |
tuffnatty | 0:2886eabcadbc | 7 | #define PIN_LCD_11 PA_10 |
tuffnatty | 0:2886eabcadbc | 8 | #define PIN_LCD_12 PB_3 |
tuffnatty | 0:2886eabcadbc | 9 | #define PIN_LCD_13 PB_5 |
tuffnatty | 0:2886eabcadbc | 10 | #define PIN_LCD_14 PB_4 |
tuffnatty | 0:2886eabcadbc | 11 | |
tuffnatty | 0:2886eabcadbc | 12 | #define PIN_SERVO_ORANGE PA_15 |
tuffnatty | 0:2886eabcadbc | 13 | |
tuffnatty | 0:2886eabcadbc | 14 | #define PIN_LED_COFFEE /* first */ PC_4, PB_13, PB_14, PB_15, PB_1, PB_2, PB_12, PA_11, PA_12, PC_5, \ |
tuffnatty | 0:2886eabcadbc | 15 | /* second */ PC_6, PC_8, PC_9, PB_10, PA_8, PA_9,PC_7, PB_6, PA_7, PA_6 |
tuffnatty | 0:2886eabcadbc | 16 | #define PIN_LED_WATER PC_0, PC_1, PB_0, PA_4, PA_1, PA_0 |
tuffnatty | 0:2886eabcadbc | 17 | #define PIN_LED_COCOA PC_3, PC_2, PH_1, PH_0, PC_15 |
tuffnatty | 0:2886eabcadbc | 18 | #define PIN_LED_MILK PC_14, PB_7, PA_14 |
tuffnatty | 0:2886eabcadbc | 19 | |
tuffnatty | 0:2886eabcadbc | 20 | #define PIN_BUTTON_AMERICANO PC_10 |
tuffnatty | 0:2886eabcadbc | 21 | #define PIN_BUTTON_CHOCO PC_11 |
tuffnatty | 0:2886eabcadbc | 22 | #define PIN_BUTTON_ESPRESSO PC_12 |
tuffnatty | 0:2886eabcadbc | 23 | #define PIN_BUTTON_LATTE PD_2 |
tuffnatty | 0:2886eabcadbc | 24 | #define PIN_BUTTON_MOCCO PA_13 |
tuffnatty | 0:2886eabcadbc | 25 | |
tuffnatty | 0:2886eabcadbc | 26 | #define PIN_SWITCH PC_13 /* same as USER_BUTTON */ |
tuffnatty | 0:2886eabcadbc | 27 | |
tuffnatty | 0:2886eabcadbc | 28 | #define SERVO_POS_A 544 /* 544-2400 */ |
tuffnatty | 0:2886eabcadbc | 29 | #define SERVO_POS_B 2400 /* 544-2400 */ |
tuffnatty | 0:2886eabcadbc | 30 | |
tuffnatty | 0:2886eabcadbc | 31 | TextLCD lcd(PIN_LCD_4, PIN_LCD_6, PIN_LCD_11, PIN_LCD_12, PIN_LCD_13, PIN_LCD_14); // rs, e, d4-d7 |
tuffnatty | 0:2886eabcadbc | 32 | DigitalIn latte_button(PIN_BUTTON_LATTE, PullUp); |
tuffnatty | 0:2886eabcadbc | 33 | DigitalIn mocco_button(PIN_BUTTON_MOCCO, PullUp); |
tuffnatty | 0:2886eabcadbc | 34 | DigitalIn americano_button(PIN_BUTTON_AMERICANO, PullUp); |
tuffnatty | 0:2886eabcadbc | 35 | DigitalIn choco_button(PIN_BUTTON_CHOCO, PullUp); |
tuffnatty | 0:2886eabcadbc | 36 | DigitalIn espresso_button(PIN_BUTTON_ESPRESSO, PullUp); |
tuffnatty | 0:2886eabcadbc | 37 | DigitalIn type_switch(PIN_SWITCH, PullDown); |
tuffnatty | 0:2886eabcadbc | 38 | |
tuffnatty | 0:2886eabcadbc | 39 | DigitalOut led[34] = {PIN_LED_COFFEE, PIN_LED_COCOA, PIN_LED_MILK, PIN_LED_WATER}; |
tuffnatty | 0:2886eabcadbc | 40 | Servo servo(PIN_SERVO_ORANGE); |
tuffnatty | 0:2886eabcadbc | 41 | |
tuffnatty | 0:2886eabcadbc | 42 | #define COFFEE 0 |
tuffnatty | 0:2886eabcadbc | 43 | #define COCOA 1 |
tuffnatty | 0:2886eabcadbc | 44 | #define MILK 2 |
tuffnatty | 0:2886eabcadbc | 45 | #define WATER 3 |
tuffnatty | 0:2886eabcadbc | 46 | |
tuffnatty | 0:2886eabcadbc | 47 | #define MAX_COFFEE 20 |
tuffnatty | 0:2886eabcadbc | 48 | #define MAX_COCOA 5 |
tuffnatty | 0:2886eabcadbc | 49 | #define MAX_MILK 3 |
tuffnatty | 0:2886eabcadbc | 50 | #define MAX_WATER 6 |
tuffnatty | 0:2886eabcadbc | 51 | |
tuffnatty | 0:2886eabcadbc | 52 | int left[4]; |
tuffnatty | 0:2886eabcadbc | 53 | |
tuffnatty | 0:2886eabcadbc | 54 | void show_leds() { |
tuffnatty | 0:2886eabcadbc | 55 | int i; |
tuffnatty | 0:2886eabcadbc | 56 | for (i = 0; i < MAX_COFFEE; i++) { |
tuffnatty | 0:2886eabcadbc | 57 | led[0 + i] = (i + 1 <= left[COFFEE]); |
tuffnatty | 0:2886eabcadbc | 58 | } |
tuffnatty | 0:2886eabcadbc | 59 | for (i = 0; i < MAX_COCOA; i++) { |
tuffnatty | 0:2886eabcadbc | 60 | led[MAX_COFFEE + i] = (i + 1 <= left[COCOA]); |
tuffnatty | 0:2886eabcadbc | 61 | } |
tuffnatty | 0:2886eabcadbc | 62 | for (i = 0; i < MAX_MILK; i++) { |
tuffnatty | 0:2886eabcadbc | 63 | led[MAX_COFFEE + MAX_COCOA + i] = (i + 1 <= left[MILK]); |
tuffnatty | 0:2886eabcadbc | 64 | } |
tuffnatty | 0:2886eabcadbc | 65 | for (i = 0; i < MAX_WATER; i++) { |
tuffnatty | 0:2886eabcadbc | 66 | led[MAX_COFFEE + MAX_COCOA + MAX_MILK + i] = (i + 1 <= left[WATER]); |
tuffnatty | 0:2886eabcadbc | 67 | } |
tuffnatty | 0:2886eabcadbc | 68 | } |
tuffnatty | 0:2886eabcadbc | 69 | |
tuffnatty | 0:2886eabcadbc | 70 | void reset() { |
tuffnatty | 0:2886eabcadbc | 71 | int n = type_switch; |
tuffnatty | 0:2886eabcadbc | 72 | //lcd.printf("type %d\n", n); |
tuffnatty | 1:dd54f5e7db5c | 73 | if (0/*type_switch == 0*/) { |
tuffnatty | 0:2886eabcadbc | 74 | left[COFFEE] = 15; |
tuffnatty | 0:2886eabcadbc | 75 | left[COCOA] = 5; |
tuffnatty | 0:2886eabcadbc | 76 | left[MILK] = 3; |
tuffnatty | 0:2886eabcadbc | 77 | left[WATER] = 5; |
tuffnatty | 0:2886eabcadbc | 78 | } else { |
tuffnatty | 0:2886eabcadbc | 79 | left[COFFEE] = 20; |
tuffnatty | 0:2886eabcadbc | 80 | left[COCOA] = 4; |
tuffnatty | 0:2886eabcadbc | 81 | left[MILK] = 2; |
tuffnatty | 0:2886eabcadbc | 82 | left[WATER] = 6; |
tuffnatty | 0:2886eabcadbc | 83 | } |
tuffnatty | 0:2886eabcadbc | 84 | show_leds(); |
tuffnatty | 0:2886eabcadbc | 85 | servo.Enable(SERVO_POS_A, 20000); |
tuffnatty | 0:2886eabcadbc | 86 | wait(1); |
tuffnatty | 0:2886eabcadbc | 87 | lcd.cls(); |
tuffnatty | 0:2886eabcadbc | 88 | lcd.printf("B\xC3\xB2""ep\xB8\xBF""e \xBD""a\xBE\xB8\xBFo\xBA"); |
tuffnatty | 0:2886eabcadbc | 89 | //lcd.printf("B"/*\xC3\xB2""ep\xB8\xBF""e \xBD""a\xBE\xB8\xBFo\xBA"*/); |
tuffnatty | 0:2886eabcadbc | 90 | } |
tuffnatty | 0:2886eabcadbc | 91 | |
tuffnatty | 0:2886eabcadbc | 92 | void drop_key() { |
tuffnatty | 0:2886eabcadbc | 93 | servo.SetPosition(SERVO_POS_B); |
tuffnatty | 0:2886eabcadbc | 94 | wait(1); |
tuffnatty | 0:2886eabcadbc | 95 | } |
tuffnatty | 0:2886eabcadbc | 96 | |
tuffnatty | 0:2886eabcadbc | 97 | void try_drink(int ing0, int ing1, int ing2, int ing3) { |
tuffnatty | 0:2886eabcadbc | 98 | wait(0.5); |
tuffnatty | 0:2886eabcadbc | 99 | if (left[COFFEE] >= ing0 && left[COCOA] >= ing1 && left[MILK] >= ing2 && left[WATER] >= ing3) { |
tuffnatty | 0:2886eabcadbc | 100 | left[COFFEE] -= ing0; |
tuffnatty | 0:2886eabcadbc | 101 | left[COCOA] -= ing1; |
tuffnatty | 0:2886eabcadbc | 102 | left[MILK] -= ing2; |
tuffnatty | 0:2886eabcadbc | 103 | left[WATER] -= ing3; |
tuffnatty | 0:2886eabcadbc | 104 | show_leds(); |
tuffnatty | 0:2886eabcadbc | 105 | lcd.cls(); |
tuffnatty | 0:2886eabcadbc | 106 | if (left[COFFEE] || left[COCOA] || left[MILK] || left[WATER]) { |
tuffnatty | 0:2886eabcadbc | 107 | lcd.printf("\xA4""a\xBA""a\xB7 c\xE3""e\xBB""a\xBD \xBD""e \xBEo\xBB\xBDoc\xBF\xC4\xC6\n"); |
tuffnatty | 0:2886eabcadbc | 108 | } else { |
tuffnatty | 0:2886eabcadbc | 109 | lcd.printf("\n\xA4""a\xBA""a\xB7 c\xE3""e\xBB""a\xBD\n"); |
tuffnatty | 0:2886eabcadbc | 110 | drop_key(); |
tuffnatty | 0:2886eabcadbc | 111 | reset(); |
tuffnatty | 0:2886eabcadbc | 112 | } |
tuffnatty | 0:2886eabcadbc | 113 | } |
tuffnatty | 0:2886eabcadbc | 114 | } |
tuffnatty | 0:2886eabcadbc | 115 | |
tuffnatty | 0:2886eabcadbc | 116 | int main() { |
tuffnatty | 0:2886eabcadbc | 117 | int was_americano = 1, was_latte = 1, was_mocco = 1, was_choco = 1, was_espresso = 1; |
tuffnatty | 0:2886eabcadbc | 118 | int now_americano, now_latte, now_mocco, now_choco, now_espresso; |
tuffnatty | 0:2886eabcadbc | 119 | reset(); |
tuffnatty | 0:2886eabcadbc | 120 | while (1) { |
tuffnatty | 0:2886eabcadbc | 121 | now_americano = americano_button; |
tuffnatty | 0:2886eabcadbc | 122 | now_latte = latte_button; |
tuffnatty | 0:2886eabcadbc | 123 | now_mocco = mocco_button; |
tuffnatty | 0:2886eabcadbc | 124 | now_choco = choco_button; |
tuffnatty | 0:2886eabcadbc | 125 | now_espresso = espresso_button; |
tuffnatty | 0:2886eabcadbc | 126 | if (!was_americano && now_americano) { |
tuffnatty | 1:dd54f5e7db5c | 127 | //try_drink(4, 0, 0, 2); |
tuffnatty | 1:dd54f5e7db5c | 128 | try_drink(1, 0, 0, 1); |
tuffnatty | 0:2886eabcadbc | 129 | } else if (!was_latte && now_latte) { |
tuffnatty | 1:dd54f5e7db5c | 130 | //try_drink(2, 0, 1, 0); |
tuffnatty | 1:dd54f5e7db5c | 131 | try_drink(1, 0, 1, 0); |
tuffnatty | 0:2886eabcadbc | 132 | } else if (!was_mocco && now_mocco) { |
tuffnatty | 1:dd54f5e7db5c | 133 | //try_drink(3, 1, 0, 0); |
tuffnatty | 1:dd54f5e7db5c | 134 | try_drink(2, 2, 1, 1); |
tuffnatty | 0:2886eabcadbc | 135 | } else if (!was_choco && now_choco) { |
tuffnatty | 1:dd54f5e7db5c | 136 | //try_drink(0, 2, 1, 0); |
tuffnatty | 1:dd54f5e7db5c | 137 | try_drink(0, 3, 1, 0); |
tuffnatty | 0:2886eabcadbc | 138 | } else if (!was_espresso && now_espresso) { |
tuffnatty | 1:dd54f5e7db5c | 139 | //try_drink(2, 0, 0, 1); |
tuffnatty | 1:dd54f5e7db5c | 140 | try_drink(5, 0, 0, 1); |
tuffnatty | 0:2886eabcadbc | 141 | } |
tuffnatty | 0:2886eabcadbc | 142 | was_americano = now_americano; |
tuffnatty | 0:2886eabcadbc | 143 | was_latte = now_latte; |
tuffnatty | 0:2886eabcadbc | 144 | was_espresso = now_espresso; |
tuffnatty | 0:2886eabcadbc | 145 | was_mocco = now_mocco; |
tuffnatty | 0:2886eabcadbc | 146 | was_choco = now_choco; |
tuffnatty | 0:2886eabcadbc | 147 | wait(0.05); |
tuffnatty | 0:2886eabcadbc | 148 | } |
tuffnatty | 0:2886eabcadbc | 149 | } |