mark reilly
/
Counting_Machine_V6
final form
main.cpp@6:21c45f712206, 2018-11-21 (annotated)
- Committer:
- mark1998
- Date:
- Wed Nov 21 16:26:03 2018 +0000
- Revision:
- 6:21c45f712206
- Parent:
- 5:cb60f96b36c9
- Child:
- 7:b522dd5a5d39
djc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chenniges | 0:733afbfa4d3f | 1 | #include "mbed.h" |
chenniges | 0:733afbfa4d3f | 2 | #include "C12832.h" |
chenniges | 0:733afbfa4d3f | 3 | |
chenniges | 0:733afbfa4d3f | 4 | DigitalIn LDR1(p9); //1p |
chenniges | 0:733afbfa4d3f | 5 | DigitalIn LDR2(p10); //2p |
chenniges | 4:67766287a6a0 | 6 | DigitalIn LDR3(p21); //5p |
chenniges | 4:67766287a6a0 | 7 | DigitalIn LDR4(p22); //10p |
chenniges | 4:67766287a6a0 | 8 | DigitalIn LDR5(p23); //20p |
chenniges | 4:67766287a6a0 | 9 | DigitalIn LDR6(p24); //50p |
chenniges | 4:67766287a6a0 | 10 | DigitalIn LDR7(p29); //1GBP |
chenniges | 4:67766287a6a0 | 11 | DigitalIn LDR8(p30); //2GBP //I don't know the mistake |
mark1998 | 3:c9057d642843 | 12 | DigitalIn switch1(p18); |
mark1998 | 3:c9057d642843 | 13 | //AnalogIn button2(p19); |
chenniges | 0:733afbfa4d3f | 14 | |
chenniges | 0:733afbfa4d3f | 15 | C12832 lcd(p5, p7, p6, p8, p11); |
mark1998 | 5:cb60f96b36c9 | 16 | Serial pc(USBTX, USBRX); // USB serial interface |
chenniges | 0:733afbfa4d3f | 17 | |
chenniges | 0:733afbfa4d3f | 18 | /*int main() |
chenniges | 0:733afbfa4d3f | 19 | { |
chenniges | 0:733afbfa4d3f | 20 | int j=0; |
chenniges | 0:733afbfa4d3f | 21 | lcd.cls(); |
chenniges | 0:733afbfa4d3f | 22 | lcd.locate(0,3); |
chenniges | 0:733afbfa4d3f | 23 | lcd.printf("mbed application board!"); |
chenniges | 0:733afbfa4d3f | 24 | |
chenniges | 0:733afbfa4d3f | 25 | while(true) |
chenniges | 0:733afbfa4d3f | 26 | { // this is the third thread |
chenniges | 0:733afbfa4d3f | 27 | lcd.locate(0,15); |
chenniges | 0:733afbfa4d3f | 28 | lcd.printf("Counting : %d",j); |
chenniges | 0:733afbfa4d3f | 29 | j++; |
chenniges | 0:733afbfa4d3f | 30 | wait(1.0); |
chenniges | 0:733afbfa4d3f | 31 | } |
chenniges | 0:733afbfa4d3f | 32 | }*/ |
chenniges | 0:733afbfa4d3f | 33 | |
chenniges | 0:733afbfa4d3f | 34 | |
chenniges | 0:733afbfa4d3f | 35 | int main() |
chenniges | 0:733afbfa4d3f | 36 | { |
mark1998 | 6:21c45f712206 | 37 | //pc.baud(921600); |
chenniges | 0:733afbfa4d3f | 38 | float a[8]={0,0,0,0,0,0,0,0}; //number of 1p coins |
chenniges | 0:733afbfa4d3f | 39 | int n[8]={0,0,0,0,0,0,0,0}; //amount of 1p coins |
chenniges | 0:733afbfa4d3f | 40 | float v[8]={0.01,0.02,0.05,0.1,0.2,0.5,1,2};// value of coins |
chenniges | 0:733afbfa4d3f | 41 | //... |
chenniges | 0:733afbfa4d3f | 42 | float ta=0; //total amount of all coins |
chenniges | 0:733afbfa4d3f | 43 | //int c=0; //counting variable |
chenniges | 0:733afbfa4d3f | 44 | //float tm; // token money |
chenniges | 0:733afbfa4d3f | 45 | //int buttons; //one for selection of coins, one for confirming and programm ending |
chenniges | 0:733afbfa4d3f | 46 | int an = 0;//number in which of the arrays we are |
mark1998 | 2:c95f08525bea | 47 | Timer t; |
mark1998 | 2:c95f08525bea | 48 | /*time_t start, end; //time counting |
chenniges | 0:733afbfa4d3f | 49 | double elapsed; // seconds |
mark1998 | 2:c95f08525bea | 50 | start = time(NULL);*/ |
chenniges | 0:733afbfa4d3f | 51 | |
chenniges | 4:67766287a6a0 | 52 | while(t < 8) |
chenniges | 0:733afbfa4d3f | 53 | { |
chenniges | 4:67766287a6a0 | 54 | //if(t > 1) |
chenniges | 4:67766287a6a0 | 55 | //{ |
chenniges | 4:67766287a6a0 | 56 | // break; |
chenniges | 4:67766287a6a0 | 57 | //} |
mark1998 | 2:c95f08525bea | 58 | |
chenniges | 4:67766287a6a0 | 59 | if(LDR1 == 0)//we could also use switch-case here for every diode |
chenniges | 0:733afbfa4d3f | 60 | { |
chenniges | 0:733afbfa4d3f | 61 | n[0]++; |
chenniges | 0:733afbfa4d3f | 62 | a[0] = n[0]*v[0]; |
mark1998 | 2:c95f08525bea | 63 | t.reset(); |
mark1998 | 2:c95f08525bea | 64 | t.start(); |
chenniges | 0:733afbfa4d3f | 65 | //wait(0.2); //I don't know if the programm would count a coin twice when we reset the LED immediately because the programm runs faster than the coins fall |
chenniges | 0:733afbfa4d3f | 66 | //why is LED1 = 0; a failure? |
chenniges | 0:733afbfa4d3f | 67 | //c = 0; |
mark1998 | 2:c95f08525bea | 68 | //time(0); |
chenniges | 0:733afbfa4d3f | 69 | } |
chenniges | 0:733afbfa4d3f | 70 | |
mark1998 | 2:c95f08525bea | 71 | |
mark1998 | 2:c95f08525bea | 72 | /* |
chenniges | 0:733afbfa4d3f | 73 | end = time(NULL); |
chenniges | 0:733afbfa4d3f | 74 | elapsed = difftime(end, start); |
mark1998 | 2:c95f08525bea | 75 | if (elapsed >= 8.0)//seconds |
chenniges | 0:733afbfa4d3f | 76 | { |
chenniges | 0:733afbfa4d3f | 77 | break; |
chenniges | 0:733afbfa4d3f | 78 | } |
mark1998 | 2:c95f08525bea | 79 | */ |
chenniges | 0:733afbfa4d3f | 80 | |
chenniges | 4:67766287a6a0 | 81 | if(LDR2.read() == 0) |
chenniges | 0:733afbfa4d3f | 82 | { |
chenniges | 0:733afbfa4d3f | 83 | n[1]++; |
chenniges | 0:733afbfa4d3f | 84 | a[1] = n[1]*v[1]; |
mark1998 | 2:c95f08525bea | 85 | t.reset(); |
mark1998 | 2:c95f08525bea | 86 | t.start(); |
chenniges | 0:733afbfa4d3f | 87 | } |
chenniges | 4:67766287a6a0 | 88 | if(LDR3.read() == 0) |
chenniges | 0:733afbfa4d3f | 89 | { |
chenniges | 0:733afbfa4d3f | 90 | n[2]++; |
chenniges | 0:733afbfa4d3f | 91 | a[2] = n[2]*v[2]; |
mark1998 | 2:c95f08525bea | 92 | t.reset(); |
mark1998 | 2:c95f08525bea | 93 | t.start(); |
chenniges | 0:733afbfa4d3f | 94 | } |
chenniges | 4:67766287a6a0 | 95 | if(LDR4.read() == 0) |
chenniges | 0:733afbfa4d3f | 96 | { |
chenniges | 0:733afbfa4d3f | 97 | n[3]++; |
chenniges | 0:733afbfa4d3f | 98 | a[3] = n[3]*v[3]; |
mark1998 | 2:c95f08525bea | 99 | t.reset(); |
mark1998 | 2:c95f08525bea | 100 | t.start(); |
chenniges | 0:733afbfa4d3f | 101 | } |
chenniges | 4:67766287a6a0 | 102 | if(LDR5.read() == 0) |
chenniges | 0:733afbfa4d3f | 103 | { |
chenniges | 0:733afbfa4d3f | 104 | n[4]++; |
chenniges | 0:733afbfa4d3f | 105 | a[4] = n[4]*v[4]; |
mark1998 | 2:c95f08525bea | 106 | t.reset(); |
mark1998 | 2:c95f08525bea | 107 | t.start(); |
chenniges | 0:733afbfa4d3f | 108 | } |
chenniges | 4:67766287a6a0 | 109 | if(LDR6.read() == 0) |
chenniges | 0:733afbfa4d3f | 110 | { |
chenniges | 0:733afbfa4d3f | 111 | n[5]++; |
chenniges | 0:733afbfa4d3f | 112 | a[5] = n[5]*v[5]; |
mark1998 | 2:c95f08525bea | 113 | t.reset(); |
mark1998 | 2:c95f08525bea | 114 | t.start(); |
chenniges | 0:733afbfa4d3f | 115 | } |
chenniges | 4:67766287a6a0 | 116 | if(LDR7.read() == 0) |
chenniges | 0:733afbfa4d3f | 117 | { |
chenniges | 0:733afbfa4d3f | 118 | n[6]++; |
chenniges | 0:733afbfa4d3f | 119 | a[6] = n[6]*v[6]; |
mark1998 | 2:c95f08525bea | 120 | t.reset(); |
mark1998 | 2:c95f08525bea | 121 | t.start(); |
chenniges | 0:733afbfa4d3f | 122 | } |
chenniges | 4:67766287a6a0 | 123 | if(LDR8.read() == 0) |
chenniges | 0:733afbfa4d3f | 124 | { |
chenniges | 0:733afbfa4d3f | 125 | n[7]++; |
chenniges | 0:733afbfa4d3f | 126 | a[7] = n[7]*v[7]; |
mark1998 | 2:c95f08525bea | 127 | t.reset(); |
mark1998 | 2:c95f08525bea | 128 | t.start(); |
mark1998 | 2:c95f08525bea | 129 | } |
chenniges | 0:733afbfa4d3f | 130 | //... other types of coins |
chenniges | 0:733afbfa4d3f | 131 | /*c++; |
chenniges | 0:733afbfa4d3f | 132 | if (c >= 100)//depands how long the programm per cycle needs |
chenniges | 0:733afbfa4d3f | 133 | { |
chenniges | 0:733afbfa4d3f | 134 | break; |
chenniges | 0:733afbfa4d3f | 135 | }*/ |
chenniges | 0:733afbfa4d3f | 136 | } |
mark1998 | 2:c95f08525bea | 137 | /*lcd.printf("You have %d coins of 1p \nwhich means you have %f$*ps",n[0],a[0]); // it looks like we have to use only printf and not lcd.printf |
chenniges | 0:733afbfa4d3f | 138 | wait(5); |
chenniges | 0:733afbfa4d3f | 139 | lcd.cls(); |
mark1998 | 2:c95f08525bea | 140 | //doing these things for the other coins as well*/ |
chenniges | 0:733afbfa4d3f | 141 | |
chenniges | 0:733afbfa4d3f | 142 | ta = a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]; |
mark1998 | 5:cb60f96b36c9 | 143 | //lcd.locate(0,0); |
mark1998 | 6:21c45f712206 | 144 | //pc.printf("Your total value is %d %c.",ta,char(163)); |
mark1998 | 6:21c45f712206 | 145 | pc.printf( "your total value is %f %c.",ta,char(163)); |
chenniges | 0:733afbfa4d3f | 146 | wait(5); |
mark1998 | 5:cb60f96b36c9 | 147 | system("CLS"); |
chenniges | 0:733afbfa4d3f | 148 | |
mark1998 | 5:cb60f96b36c9 | 149 | pc.printf("Please select witch number \nof coins you would like to see."); |
chenniges | 0:733afbfa4d3f | 150 | wait(5); |
mark1998 | 5:cb60f96b36c9 | 151 | // pc.cls(); |
mark1998 | 5:cb60f96b36c9 | 152 | system("CLS"); |
mark1998 | 5:cb60f96b36c9 | 153 | pc.printf("Confirm your choice by using the switch."); |
mark1998 | 2:c95f08525bea | 154 | wait(3); |
mark1998 | 5:cb60f96b36c9 | 155 | system("CLS"); |
mark1998 | 5:cb60f96b36c9 | 156 | // pc.cls(); |
chenniges | 0:733afbfa4d3f | 157 | /*switch (buttons) |
chenniges | 0:733afbfa4d3f | 158 | { |
chenniges | 0:733afbfa4d3f | 159 | case button1: an = an+1; |
chenniges | 0:733afbfa4d3f | 160 | if (an > 8) |
chenniges | 0:733afbfa4d3f | 161 | { |
chenniges | 0:733afbfa4d3f | 162 | an = 1 |
chenniges | 0:733afbfa4d3f | 163 | } |
chenniges | 0:733afbfa4d3f | 164 | lcd.cls(); |
chenniges | 0:733afbfa4d3f | 165 | lcd.printf("Do you want to see the number of %d coins?",v[an]); |
mark1998 | 3:c9057d642843 | 166 | break; |
chenniges | 0:733afbfa4d3f | 167 | case button2: lcd.printf("You have %d of this type of coins. \nThat means you have %f£ of this type of coin.",n[an],a[an]); //it doesn't know button1 and button2 yet |
chenniges | 0:733afbfa4d3f | 168 | break; |
chenniges | 0:733afbfa4d3f | 169 | }*/ |
chenniges | 4:67766287a6a0 | 170 | //while(switch1 == 1) |
chenniges | 4:67766287a6a0 | 171 | //{ |
chenniges | 4:67766287a6a0 | 172 | do |
chenniges | 0:733afbfa4d3f | 173 | { |
chenniges | 4:67766287a6a0 | 174 | if (switch1 == 1) |
chenniges | 0:733afbfa4d3f | 175 | { |
mark1998 | 6:21c45f712206 | 176 | |
mark1998 | 6:21c45f712206 | 177 | // an++; |
mark1998 | 6:21c45f712206 | 178 | // if (an > 8) |
mark1998 | 6:21c45f712206 | 179 | // { |
mark1998 | 6:21c45f712206 | 180 | // an = 1; |
mark1998 | 6:21c45f712206 | 181 | // } |
mark1998 | 5:cb60f96b36c9 | 182 | //pc.cls(); |
mark1998 | 6:21c45f712206 | 183 | |
mark1998 | 6:21c45f712206 | 184 | |
mark1998 | 6:21c45f712206 | 185 | |
mark1998 | 5:cb60f96b36c9 | 186 | system("CLS"); |
mark1998 | 5:cb60f96b36c9 | 187 | pc.printf("Do you want to see the number of %d %c coins?",v[an],char(163)); |
mark1998 | 6:21c45f712206 | 188 | na++; |
mark1998 | 6:21c45f712206 | 189 | if (an > 7); |
mark1998 | 6:21c45f712206 | 190 | { |
mark1998 | 6:21c45f712206 | 191 | an= 0 ; |
mark1998 | 6:21c45f712206 | 192 | } |
chenniges | 4:67766287a6a0 | 193 | wait(3); |
chenniges | 4:67766287a6a0 | 194 | //button1 = 0; |
chenniges | 0:733afbfa4d3f | 195 | } |
chenniges | 4:67766287a6a0 | 196 | if(switch1 == 0) |
chenniges | 0:733afbfa4d3f | 197 | { |
mark1998 | 5:cb60f96b36c9 | 198 | pc.printf("You have %d of this type of coins. \nThat means you have %f %c of this type of coin.",n[an],a[an],char(163)); //it doesn't know button1 and button2 yet |
mark1998 | 3:c9057d642843 | 199 | wait(5); |
mark1998 | 5:cb60f96b36c9 | 200 | //lcd.cls(); |
mark1998 | 5:cb60f96b36c9 | 201 | system("CLS"); |
mark1998 | 5:cb60f96b36c9 | 202 | pc.printf("If you would like to see other \ntypes of coins as well, please \nuse switch again."); |
mark1998 | 3:c9057d642843 | 203 | wait(5); |
mark1998 | 5:cb60f96b36c9 | 204 | system("CLS"); |
mark1998 | 5:cb60f96b36c9 | 205 | //lcd.cls(); |
chenniges | 0:733afbfa4d3f | 206 | } |
chenniges | 4:67766287a6a0 | 207 | }while(switch1 == 1); |
chenniges | 4:67766287a6a0 | 208 | |
chenniges | 4:67766287a6a0 | 209 | /*if (switch1 == 0) |
chenniges | 4:67766287a6a0 | 210 | { |
chenniges | 4:67766287a6a0 | 211 | lcd.printf("You have %d of this type of coins. \nThat means you have %f %c of this type of coin.",n[an],a[an],char(163)); //it doesn't know button1 and button2 yet |
chenniges | 4:67766287a6a0 | 212 | wait(5); |
chenniges | 4:67766287a6a0 | 213 | lcd.cls(); |
chenniges | 4:67766287a6a0 | 214 | lcd.printf("If you would like to see other \ntypes of coins as well, please \nuse switch again."); |
chenniges | 4:67766287a6a0 | 215 | wait(5); |
chenniges | 4:67766287a6a0 | 216 | lcd.cls(); |
chenniges | 4:67766287a6a0 | 217 | } |
chenniges | 4:67766287a6a0 | 218 | }*/ |
mark1998 | 3:c9057d642843 | 219 | //wait(5); |
mark1998 | 3:c9057d642843 | 220 | //lcd.cls(); |
mark1998 | 3:c9057d642843 | 221 | |
mark1998 | 3:c9057d642843 | 222 | |
mark1998 | 5:cb60f96b36c9 | 223 | pc.printf("Please use the switch for finishing process."); |
chenniges | 4:67766287a6a0 | 224 | //wait(4); |
chenniges | 4:67766287a6a0 | 225 | while(switch1 == 0) |
mark1998 | 3:c9057d642843 | 226 | { |
chenniges | 4:67766287a6a0 | 227 | } |
chenniges | 4:67766287a6a0 | 228 | //do |
chenniges | 4:67766287a6a0 | 229 | //{ |
chenniges | 4:67766287a6a0 | 230 | // if(switch1 == 1) |
chenniges | 4:67766287a6a0 | 231 | //{ |
mark1998 | 5:cb60f96b36c9 | 232 | //pc.cls(); |
mark1998 | 5:cb60f96b36c9 | 233 | system("CLS"); |
mark1998 | 5:cb60f96b36c9 | 234 | pc.printf("Thank you for using 'Coounting Machine'. \nGood Bye. :-)"); |
mark1998 | 3:c9057d642843 | 235 | wait(5); |
mark1998 | 5:cb60f96b36c9 | 236 | system("CLS"); |
mark1998 | 5:cb60f96b36c9 | 237 | //pc.cls(); |
chenniges | 4:67766287a6a0 | 238 | //} |
chenniges | 4:67766287a6a0 | 239 | //}while (switch1 == 0); |
chenniges | 0:733afbfa4d3f | 240 | /*if() //press button or something like that //total amount which is left when the user took money and entered the amount of it in the machine. |
chenniges | 0:733afbfa4d3f | 241 | { |
chenniges | 0:733afbfa4d3f | 242 | lcd.printf("Please enter the mount of money you took:"); |
chenniges | 0:733afbfa4d3f | 243 | scanf("%f",&tm); |
chenniges | 0:733afbfa4d3f | 244 | lcd.printf("%f",tm); |
chenniges | 0:733afbfa4d3f | 245 | wait(2); |
chenniges | 0:733afbfa4d3f | 246 | ta = ta-tm; |
chenniges | 0:733afbfa4d3f | 247 | lcd.printf("Now you have only %f \bx156 left", ta); //\b for one step backword and I guess that x156 for £ //I'm not sure |
chenniges | 0:733afbfa4d3f | 248 | }*/ |
chenniges | 0:733afbfa4d3f | 249 | |
chenniges | 0:733afbfa4d3f | 250 | return(0); |
chenniges | 0:733afbfa4d3f | 251 | } |
mark1998 | 3:c9057d642843 | 252 | //change all 1 to 0 at LDR1,2,... |