Chenkai Shao
/
4180Final
Automatic cocktail maker
main.cpp@2:2351d0558c70, 2015-12-03 (annotated)
- Committer:
- cshao06
- Date:
- Thu Dec 03 04:58:36 2015 +0000
- Revision:
- 2:2351d0558c70
- Parent:
- 1:aa2e8e59127c
Fixed distance sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cshao06 | 0:0095fd32715b | 1 | #include "mbed.h" |
cshao06 | 0:0095fd32715b | 2 | #include "rtos.h" |
cshao06 | 0:0095fd32715b | 3 | #include <algorithm> |
cshao06 | 0:0095fd32715b | 4 | |
cshao06 | 0:0095fd32715b | 5 | using namespace std; |
cshao06 | 0:0095fd32715b | 6 | |
cshao06 | 0:0095fd32715b | 7 | DigitalOut latch(p9); |
cshao06 | 0:0095fd32715b | 8 | DigitalOut enable(p10); |
cshao06 | 0:0095fd32715b | 9 | |
cshao06 | 0:0095fd32715b | 10 | SPI spi(p11, p12, p13); |
cshao06 | 0:0095fd32715b | 11 | |
cshao06 | 0:0095fd32715b | 12 | DigitalOut valve1(p15); |
cshao06 | 0:0095fd32715b | 13 | DigitalOut valve2(p16); |
cshao06 | 0:0095fd32715b | 14 | DigitalOut valve3(p17); |
cshao06 | 0:0095fd32715b | 15 | |
cshao06 | 0:0095fd32715b | 16 | AnalogIn distSensor(p20); |
cshao06 | 0:0095fd32715b | 17 | |
cshao06 | 0:0095fd32715b | 18 | DigitalOut led1(LED1); |
cshao06 | 0:0095fd32715b | 19 | DigitalOut led2(LED2); |
cshao06 | 0:0095fd32715b | 20 | DigitalOut led3(LED3); |
cshao06 | 0:0095fd32715b | 21 | |
cshao06 | 0:0095fd32715b | 22 | Serial pc(USBTX, USBRX); |
cshao06 | 0:0095fd32715b | 23 | //uLCD_4DGL lcd(p28, p27, p30); |
cshao06 | 0:0095fd32715b | 24 | |
cshao06 | 0:0095fd32715b | 25 | int vTime[3]; |
cshao06 | 0:0095fd32715b | 26 | bool t_enabled; |
cshao06 | 0:0095fd32715b | 27 | osThreadId mainThreadID; |
cshao06 | 0:0095fd32715b | 28 | |
cshao06 | 0:0095fd32715b | 29 | void RGB_LED(int red, int green, int blue) { |
cshao06 | 0:0095fd32715b | 30 | unsigned int low_color=0; |
cshao06 | 0:0095fd32715b | 31 | unsigned int high_color=0; |
cshao06 | 0:0095fd32715b | 32 | high_color=(blue<<4)|((red&0x3C0)>>6); |
cshao06 | 0:0095fd32715b | 33 | low_color=(((red&0x3F)<<10)|(green)); |
cshao06 | 0:0095fd32715b | 34 | spi.write(high_color); |
cshao06 | 0:0095fd32715b | 35 | spi.write(low_color); |
cshao06 | 0:0095fd32715b | 36 | latch=1; |
cshao06 | 0:0095fd32715b | 37 | latch=0; |
cshao06 | 0:0095fd32715b | 38 | } |
cshao06 | 0:0095fd32715b | 39 | |
cshao06 | 0:0095fd32715b | 40 | void thread1(void const *args) |
cshao06 | 0:0095fd32715b | 41 | { |
cshao06 | 0:0095fd32715b | 42 | while (1) |
cshao06 | 0:0095fd32715b | 43 | { |
cshao06 | 0:0095fd32715b | 44 | if (t_enabled) |
cshao06 | 0:0095fd32715b | 45 | { |
cshao06 | 0:0095fd32715b | 46 | valve1 = 1; led1 = 1; |
cshao06 | 0:0095fd32715b | 47 | int waitTime = (int)(vTime[0] * 100); |
cshao06 | 0:0095fd32715b | 48 | Thread::wait(waitTime); |
cshao06 | 0:0095fd32715b | 49 | valve1 = 0; led1 = 0; |
cshao06 | 0:0095fd32715b | 50 | t_enabled = false; |
cshao06 | 0:0095fd32715b | 51 | osSignalSet(mainThreadID, 0x1); |
cshao06 | 0:0095fd32715b | 52 | } |
cshao06 | 0:0095fd32715b | 53 | } |
cshao06 | 0:0095fd32715b | 54 | } |
cshao06 | 0:0095fd32715b | 55 | |
cshao06 | 0:0095fd32715b | 56 | void thread2(void const *args) |
cshao06 | 0:0095fd32715b | 57 | { |
cshao06 | 0:0095fd32715b | 58 | while (1) |
cshao06 | 0:0095fd32715b | 59 | { |
cshao06 | 0:0095fd32715b | 60 | if (t_enabled) |
cshao06 | 0:0095fd32715b | 61 | { |
cshao06 | 0:0095fd32715b | 62 | valve2 = 1; led2 = 1; |
cshao06 | 0:0095fd32715b | 63 | int waitTime = (int)(vTime[1] * 100); |
cshao06 | 0:0095fd32715b | 64 | Thread::wait(waitTime); |
cshao06 | 0:0095fd32715b | 65 | valve2 = 0; led2 = 0; |
cshao06 | 0:0095fd32715b | 66 | t_enabled = false; |
cshao06 | 0:0095fd32715b | 67 | osSignalSet(mainThreadID, 0x2); |
cshao06 | 0:0095fd32715b | 68 | } |
cshao06 | 0:0095fd32715b | 69 | } |
cshao06 | 0:0095fd32715b | 70 | } |
cshao06 | 0:0095fd32715b | 71 | |
cshao06 | 0:0095fd32715b | 72 | void thread3(void const *args) |
cshao06 | 0:0095fd32715b | 73 | { |
cshao06 | 0:0095fd32715b | 74 | while (1) |
cshao06 | 0:0095fd32715b | 75 | { |
cshao06 | 0:0095fd32715b | 76 | if (t_enabled) |
cshao06 | 0:0095fd32715b | 77 | { |
cshao06 | 0:0095fd32715b | 78 | valve3 = 1; led3 = 1; |
cshao06 | 0:0095fd32715b | 79 | int waitTime = (int)(vTime[2] * 100); |
cshao06 | 0:0095fd32715b | 80 | Thread::wait(waitTime); |
cshao06 | 0:0095fd32715b | 81 | valve3 = 0; led3 = 0; |
cshao06 | 0:0095fd32715b | 82 | t_enabled = false; |
cshao06 | 0:0095fd32715b | 83 | osSignalSet(mainThreadID, 0x4); |
cshao06 | 0:0095fd32715b | 84 | } |
cshao06 | 0:0095fd32715b | 85 | } |
cshao06 | 0:0095fd32715b | 86 | } |
cshao06 | 0:0095fd32715b | 87 | |
cshao06 | 0:0095fd32715b | 88 | void thread4(void const *args) |
cshao06 | 0:0095fd32715b | 89 | { |
cshao06 | 0:0095fd32715b | 90 | int red=0; |
cshao06 | 0:0095fd32715b | 91 | int green=0; |
cshao06 | 0:0095fd32715b | 92 | int blue=0; |
cshao06 | 0:0095fd32715b | 93 | while(1) { |
cshao06 | 0:0095fd32715b | 94 | for (red = 0; red<1000; red = red+200) { |
cshao06 | 0:0095fd32715b | 95 | for (blue = 0; blue<1000; blue = blue+200) { |
cshao06 | 0:0095fd32715b | 96 | for (green = 0; green<1000; green = green+200) |
cshao06 | 0:0095fd32715b | 97 | |
cshao06 | 0:0095fd32715b | 98 | { |
cshao06 | 0:0095fd32715b | 99 | RGB_LED( red, green, blue); |
cshao06 | 0:0095fd32715b | 100 | Thread::wait(250); |
cshao06 | 0:0095fd32715b | 101 | } |
cshao06 | 0:0095fd32715b | 102 | } |
cshao06 | 0:0095fd32715b | 103 | } |
cshao06 | 0:0095fd32715b | 104 | } |
cshao06 | 0:0095fd32715b | 105 | } |
cshao06 | 0:0095fd32715b | 106 | |
cshao06 | 0:0095fd32715b | 107 | void thread5(void const * args) |
cshao06 | 0:0095fd32715b | 108 | { |
cshao06 | 0:0095fd32715b | 109 | Thread::wait(15000); |
cshao06 | 0:0095fd32715b | 110 | while (1) |
cshao06 | 0:0095fd32715b | 111 | { |
cshao06 | 0:0095fd32715b | 112 | //bool flag = false; |
cshao06 | 0:0095fd32715b | 113 | //pc.printf("%f\n", distSensor); |
cshao06 | 0:0095fd32715b | 114 | if (distSensor > 0.05) |
cshao06 | 0:0095fd32715b | 115 | { |
cshao06 | 0:0095fd32715b | 116 | pc.putc('l'); |
cshao06 | 0:0095fd32715b | 117 | //pc.putc(' '); |
cshao06 | 0:0095fd32715b | 118 | //wait(0.5); |
cshao06 | 0:0095fd32715b | 119 | //Thread::wait(12000000); |
cshao06 | 0:0095fd32715b | 120 | break; |
cshao06 | 0:0095fd32715b | 121 | } |
cshao06 | 0:0095fd32715b | 122 | //Thread::wait(500); |
cshao06 | 0:0095fd32715b | 123 | } |
cshao06 | 0:0095fd32715b | 124 | } |
cshao06 | 0:0095fd32715b | 125 | |
cshao06 | 0:0095fd32715b | 126 | int main() |
cshao06 | 0:0095fd32715b | 127 | { |
cshao06 | 0:0095fd32715b | 128 | mainThreadID = osThreadGetId(); |
cshao06 | 0:0095fd32715b | 129 | Thread t1(thread1); |
cshao06 | 0:0095fd32715b | 130 | Thread t2(thread2); |
cshao06 | 0:0095fd32715b | 131 | Thread t3(thread3); |
cshao06 | 0:0095fd32715b | 132 | Thread t4(thread4); |
cshao06 | 2:2351d0558c70 | 133 | //Thread t5(thread5); |
cshao06 | 0:0095fd32715b | 134 | t_enabled = false; |
cshao06 | 0:0095fd32715b | 135 | char message; |
cshao06 | 0:0095fd32715b | 136 | char msg[10]; |
cshao06 | 0:0095fd32715b | 137 | char m[4]; |
cshao06 | 0:0095fd32715b | 138 | |
cshao06 | 0:0095fd32715b | 139 | |
cshao06 | 0:0095fd32715b | 140 | spi.format(16,0); |
cshao06 | 0:0095fd32715b | 141 | spi.frequency(500000); |
cshao06 | 0:0095fd32715b | 142 | enable=0; |
cshao06 | 0:0095fd32715b | 143 | latch=0; |
cshao06 | 2:2351d0558c70 | 144 | wait(10); |
cshao06 | 2:2351d0558c70 | 145 | while(distSensor < 0.05) {} |
cshao06 | 2:2351d0558c70 | 146 | pc.putc('l'); |
cshao06 | 0:0095fd32715b | 147 | |
cshao06 | 0:0095fd32715b | 148 | int i; |
cshao06 | 0:0095fd32715b | 149 | while (1) |
cshao06 | 0:0095fd32715b | 150 | { |
cshao06 | 0:0095fd32715b | 151 | if (pc.readable()) |
cshao06 | 0:0095fd32715b | 152 | { |
cshao06 | 0:0095fd32715b | 153 | message = pc.getc(); |
cshao06 | 0:0095fd32715b | 154 | if (message == 'g') |
cshao06 | 0:0095fd32715b | 155 | { |
cshao06 | 0:0095fd32715b | 156 | i = 0; |
cshao06 | 0:0095fd32715b | 157 | while (i < 10) |
cshao06 | 0:0095fd32715b | 158 | { |
cshao06 | 0:0095fd32715b | 159 | if (pc.readable()) |
cshao06 | 0:0095fd32715b | 160 | { |
cshao06 | 0:0095fd32715b | 161 | msg[i] = pc.getc(); |
cshao06 | 0:0095fd32715b | 162 | i++; |
cshao06 | 0:0095fd32715b | 163 | } |
cshao06 | 0:0095fd32715b | 164 | } |
cshao06 | 0:0095fd32715b | 165 | sprintf(m, "%.4s", msg); |
cshao06 | 0:0095fd32715b | 166 | vTime[0] = atoi(m); |
cshao06 | 0:0095fd32715b | 167 | //lcd.printf("%d\n", vTime[0]); |
cshao06 | 0:0095fd32715b | 168 | //lcd.printf("%s\n", m); |
cshao06 | 0:0095fd32715b | 169 | |
cshao06 | 0:0095fd32715b | 170 | sprintf(m, "%.3s", msg + 4); |
cshao06 | 0:0095fd32715b | 171 | vTime[1] = atoi(m); |
cshao06 | 0:0095fd32715b | 172 | |
cshao06 | 0:0095fd32715b | 173 | //lcd.printf("%d\n", vTime[1]); |
cshao06 | 0:0095fd32715b | 174 | //lcd.printf("%s\n", m); |
cshao06 | 0:0095fd32715b | 175 | sprintf(m, "%.3s", msg + 7); |
cshao06 | 0:0095fd32715b | 176 | vTime[2] = atoi(m); |
cshao06 | 0:0095fd32715b | 177 | |
cshao06 | 0:0095fd32715b | 178 | //lcd.printf("%d\n", vTime[2]); |
cshao06 | 0:0095fd32715b | 179 | //lcd.printf("%s\n", m); |
cshao06 | 0:0095fd32715b | 180 | t_enabled = true; |
cshao06 | 0:0095fd32715b | 181 | osSignalWait(0x7, osWaitForever); |
cshao06 | 0:0095fd32715b | 182 | pc.putc('j'); |
cshao06 | 0:0095fd32715b | 183 | } |
cshao06 | 0:0095fd32715b | 184 | if (message == 'a') |
cshao06 | 0:0095fd32715b | 185 | { |
cshao06 | 0:0095fd32715b | 186 | valve1 = 1; led1 = 1; |
cshao06 | 0:0095fd32715b | 187 | } |
cshao06 | 0:0095fd32715b | 188 | if (message == 'b') |
cshao06 | 0:0095fd32715b | 189 | { |
cshao06 | 0:0095fd32715b | 190 | valve1 = 0; led1 = 0; |
cshao06 | 0:0095fd32715b | 191 | } |
cshao06 | 0:0095fd32715b | 192 | if (message == 'c') |
cshao06 | 0:0095fd32715b | 193 | { |
cshao06 | 0:0095fd32715b | 194 | valve2 = 1; led2 = 1; |
cshao06 | 0:0095fd32715b | 195 | } |
cshao06 | 0:0095fd32715b | 196 | if (message == 'd') |
cshao06 | 0:0095fd32715b | 197 | { |
cshao06 | 0:0095fd32715b | 198 | valve2 = 0; led2 = 0; |
cshao06 | 0:0095fd32715b | 199 | } |
cshao06 | 0:0095fd32715b | 200 | if (message == 'e') |
cshao06 | 0:0095fd32715b | 201 | { |
cshao06 | 0:0095fd32715b | 202 | valve3 = 1; led3 = 1; |
cshao06 | 0:0095fd32715b | 203 | } |
cshao06 | 0:0095fd32715b | 204 | if (message == 'f') |
cshao06 | 0:0095fd32715b | 205 | { |
cshao06 | 0:0095fd32715b | 206 | valve3 = 0; led3 = 0; |
cshao06 | 0:0095fd32715b | 207 | } |
cshao06 | 0:0095fd32715b | 208 | } |
cshao06 | 0:0095fd32715b | 209 | } |
cshao06 | 0:0095fd32715b | 210 | } |