Automatic cocktail maker

Dependencies:   mbed-rtos mbed

Committer:
cshao06
Date:
Thu Dec 03 04:26:13 2015 +0000
Revision:
0:0095fd32715b
Child:
1:aa2e8e59127c
yeah

Who changed what in which revision?

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