mark reilly
/
Counting_Machine_V6
final form
main.cpp@3:c9057d642843, 2018-10-23 (annotated)
- Committer:
- mark1998
- Date:
- Tue Oct 23 12:00:45 2018 +0000
- Revision:
- 3:c9057d642843
- Parent:
- 2:c95f08525bea
- Child:
- 4:67766287a6a0
do while
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 | 0:733afbfa4d3f | 6 | DigitalIn LDR3(p12); //5p |
chenniges | 0:733afbfa4d3f | 7 | DigitalIn LDR4(p13); //10p |
chenniges | 0:733afbfa4d3f | 8 | DigitalIn LDR5(p14); //20p |
chenniges | 0:733afbfa4d3f | 9 | DigitalIn LDR6(p15); //50p |
chenniges | 0:733afbfa4d3f | 10 | DigitalIn LDR7(p16); //1GBP |
chenniges | 0:733afbfa4d3f | 11 | DigitalIn LDR8(p17); //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); |
chenniges | 0:733afbfa4d3f | 16 | |
chenniges | 0:733afbfa4d3f | 17 | /*int main() |
chenniges | 0:733afbfa4d3f | 18 | { |
chenniges | 0:733afbfa4d3f | 19 | int j=0; |
chenniges | 0:733afbfa4d3f | 20 | lcd.cls(); |
chenniges | 0:733afbfa4d3f | 21 | lcd.locate(0,3); |
chenniges | 0:733afbfa4d3f | 22 | lcd.printf("mbed application board!"); |
chenniges | 0:733afbfa4d3f | 23 | |
chenniges | 0:733afbfa4d3f | 24 | while(true) |
chenniges | 0:733afbfa4d3f | 25 | { // this is the third thread |
chenniges | 0:733afbfa4d3f | 26 | lcd.locate(0,15); |
chenniges | 0:733afbfa4d3f | 27 | lcd.printf("Counting : %d",j); |
chenniges | 0:733afbfa4d3f | 28 | j++; |
chenniges | 0:733afbfa4d3f | 29 | wait(1.0); |
chenniges | 0:733afbfa4d3f | 30 | } |
chenniges | 0:733afbfa4d3f | 31 | }*/ |
chenniges | 0:733afbfa4d3f | 32 | |
chenniges | 0:733afbfa4d3f | 33 | |
chenniges | 0:733afbfa4d3f | 34 | int main() |
chenniges | 0:733afbfa4d3f | 35 | { |
chenniges | 0:733afbfa4d3f | 36 | float a[8]={0,0,0,0,0,0,0,0}; //number of 1p coins |
chenniges | 0:733afbfa4d3f | 37 | int n[8]={0,0,0,0,0,0,0,0}; //amount of 1p coins |
chenniges | 0:733afbfa4d3f | 38 | float v[8]={0.01,0.02,0.05,0.1,0.2,0.5,1,2};// value of coins |
chenniges | 0:733afbfa4d3f | 39 | //... |
chenniges | 0:733afbfa4d3f | 40 | float ta=0; //total amount of all coins |
chenniges | 0:733afbfa4d3f | 41 | //int c=0; //counting variable |
chenniges | 0:733afbfa4d3f | 42 | //float tm; // token money |
chenniges | 0:733afbfa4d3f | 43 | //int buttons; //one for selection of coins, one for confirming and programm ending |
chenniges | 0:733afbfa4d3f | 44 | int an = 0;//number in which of the arrays we are |
mark1998 | 2:c95f08525bea | 45 | Timer t; |
mark1998 | 2:c95f08525bea | 46 | /*time_t start, end; //time counting |
chenniges | 0:733afbfa4d3f | 47 | double elapsed; // seconds |
mark1998 | 2:c95f08525bea | 48 | start = time(NULL);*/ |
chenniges | 0:733afbfa4d3f | 49 | |
chenniges | 0:733afbfa4d3f | 50 | for(;;) |
chenniges | 0:733afbfa4d3f | 51 | { |
mark1998 | 3:c9057d642843 | 52 | if(t > 1) |
mark1998 | 2:c95f08525bea | 53 | { |
mark1998 | 2:c95f08525bea | 54 | break; |
mark1998 | 2:c95f08525bea | 55 | } |
mark1998 | 2:c95f08525bea | 56 | |
chenniges | 0:733afbfa4d3f | 57 | if(LDR1 == 1)//we could also use switch-case here for every diode |
chenniges | 0:733afbfa4d3f | 58 | { |
chenniges | 0:733afbfa4d3f | 59 | n[0]++; |
chenniges | 0:733afbfa4d3f | 60 | a[0] = n[0]*v[0]; |
mark1998 | 2:c95f08525bea | 61 | t.reset(); |
mark1998 | 2:c95f08525bea | 62 | t.start(); |
chenniges | 0:733afbfa4d3f | 63 | //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 | 64 | //why is LED1 = 0; a failure? |
chenniges | 0:733afbfa4d3f | 65 | //c = 0; |
mark1998 | 2:c95f08525bea | 66 | //time(0); |
chenniges | 0:733afbfa4d3f | 67 | } |
chenniges | 0:733afbfa4d3f | 68 | |
mark1998 | 2:c95f08525bea | 69 | |
mark1998 | 2:c95f08525bea | 70 | /* |
chenniges | 0:733afbfa4d3f | 71 | end = time(NULL); |
chenniges | 0:733afbfa4d3f | 72 | elapsed = difftime(end, start); |
mark1998 | 2:c95f08525bea | 73 | if (elapsed >= 8.0)//seconds |
chenniges | 0:733afbfa4d3f | 74 | { |
chenniges | 0:733afbfa4d3f | 75 | break; |
chenniges | 0:733afbfa4d3f | 76 | } |
mark1998 | 2:c95f08525bea | 77 | */ |
chenniges | 0:733afbfa4d3f | 78 | |
mark1998 | 2:c95f08525bea | 79 | if(LDR2 == 1) |
chenniges | 0:733afbfa4d3f | 80 | { |
chenniges | 0:733afbfa4d3f | 81 | n[1]++; |
chenniges | 0:733afbfa4d3f | 82 | a[1] = n[1]*v[1]; |
mark1998 | 2:c95f08525bea | 83 | t.reset(); |
mark1998 | 2:c95f08525bea | 84 | t.start(); |
chenniges | 0:733afbfa4d3f | 85 | } |
chenniges | 0:733afbfa4d3f | 86 | if(LDR3 == 1) |
chenniges | 0:733afbfa4d3f | 87 | { |
chenniges | 0:733afbfa4d3f | 88 | n[2]++; |
chenniges | 0:733afbfa4d3f | 89 | a[2] = n[2]*v[2]; |
mark1998 | 2:c95f08525bea | 90 | t.reset(); |
mark1998 | 2:c95f08525bea | 91 | t.start(); |
chenniges | 0:733afbfa4d3f | 92 | } |
chenniges | 0:733afbfa4d3f | 93 | if(LDR4 == 1) |
chenniges | 0:733afbfa4d3f | 94 | { |
chenniges | 0:733afbfa4d3f | 95 | n[3]++; |
chenniges | 0:733afbfa4d3f | 96 | a[3] = n[3]*v[3]; |
mark1998 | 2:c95f08525bea | 97 | t.reset(); |
mark1998 | 2:c95f08525bea | 98 | t.start(); |
chenniges | 0:733afbfa4d3f | 99 | } |
chenniges | 0:733afbfa4d3f | 100 | if(LDR5 == 1) |
chenniges | 0:733afbfa4d3f | 101 | { |
chenniges | 0:733afbfa4d3f | 102 | n[4]++; |
chenniges | 0:733afbfa4d3f | 103 | a[4] = n[4]*v[4]; |
mark1998 | 2:c95f08525bea | 104 | t.reset(); |
mark1998 | 2:c95f08525bea | 105 | t.start(); |
chenniges | 0:733afbfa4d3f | 106 | } |
chenniges | 0:733afbfa4d3f | 107 | if(LDR6 == 1) |
chenniges | 0:733afbfa4d3f | 108 | { |
chenniges | 0:733afbfa4d3f | 109 | n[5]++; |
chenniges | 0:733afbfa4d3f | 110 | a[5] = n[5]*v[5]; |
mark1998 | 2:c95f08525bea | 111 | t.reset(); |
mark1998 | 2:c95f08525bea | 112 | t.start(); |
chenniges | 0:733afbfa4d3f | 113 | } |
chenniges | 0:733afbfa4d3f | 114 | if(LDR7 == 1) |
chenniges | 0:733afbfa4d3f | 115 | { |
chenniges | 0:733afbfa4d3f | 116 | n[6]++; |
chenniges | 0:733afbfa4d3f | 117 | a[6] = n[6]*v[6]; |
mark1998 | 2:c95f08525bea | 118 | t.reset(); |
mark1998 | 2:c95f08525bea | 119 | t.start(); |
chenniges | 0:733afbfa4d3f | 120 | } |
chenniges | 0:733afbfa4d3f | 121 | if(LDR8 == 1) |
chenniges | 0:733afbfa4d3f | 122 | { |
chenniges | 0:733afbfa4d3f | 123 | n[7]++; |
chenniges | 0:733afbfa4d3f | 124 | a[7] = n[7]*v[7]; |
mark1998 | 2:c95f08525bea | 125 | t.reset(); |
mark1998 | 2:c95f08525bea | 126 | t.start(); |
mark1998 | 2:c95f08525bea | 127 | } |
chenniges | 0:733afbfa4d3f | 128 | //... other types of coins |
chenniges | 0:733afbfa4d3f | 129 | /*c++; |
chenniges | 0:733afbfa4d3f | 130 | if (c >= 100)//depands how long the programm per cycle needs |
chenniges | 0:733afbfa4d3f | 131 | { |
chenniges | 0:733afbfa4d3f | 132 | break; |
chenniges | 0:733afbfa4d3f | 133 | }*/ |
chenniges | 0:733afbfa4d3f | 134 | } |
mark1998 | 2:c95f08525bea | 135 | /*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 | 136 | wait(5); |
chenniges | 0:733afbfa4d3f | 137 | lcd.cls(); |
mark1998 | 2:c95f08525bea | 138 | //doing these things for the other coins as well*/ |
chenniges | 0:733afbfa4d3f | 139 | |
chenniges | 0:733afbfa4d3f | 140 | ta = a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]; |
chenniges | 0:733afbfa4d3f | 141 | lcd.locate(0,0); |
mark1998 | 3:c9057d642843 | 142 | lcd.printf("Your total value is %d %c.",ta,char(163)); |
chenniges | 0:733afbfa4d3f | 143 | wait(5); |
chenniges | 0:733afbfa4d3f | 144 | lcd.cls(); |
chenniges | 0:733afbfa4d3f | 145 | |
mark1998 | 2:c95f08525bea | 146 | lcd.printf("Please select witch number \nof coins you would like to see."); |
chenniges | 0:733afbfa4d3f | 147 | wait(5); |
chenniges | 0:733afbfa4d3f | 148 | lcd.cls(); |
mark1998 | 2:c95f08525bea | 149 | lcd.printf("Confirm your choice by using the switch."); |
mark1998 | 2:c95f08525bea | 150 | wait(3); |
mark1998 | 3:c9057d642843 | 151 | lcd.cls(); |
chenniges | 0:733afbfa4d3f | 152 | /*switch (buttons) |
chenniges | 0:733afbfa4d3f | 153 | { |
chenniges | 0:733afbfa4d3f | 154 | case button1: an = an+1; |
chenniges | 0:733afbfa4d3f | 155 | if (an > 8) |
chenniges | 0:733afbfa4d3f | 156 | { |
chenniges | 0:733afbfa4d3f | 157 | an = 1 |
chenniges | 0:733afbfa4d3f | 158 | } |
chenniges | 0:733afbfa4d3f | 159 | lcd.cls(); |
chenniges | 0:733afbfa4d3f | 160 | lcd.printf("Do you want to see the number of %d coins?",v[an]); |
mark1998 | 3:c9057d642843 | 161 | break; |
chenniges | 0:733afbfa4d3f | 162 | 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 | 163 | break; |
chenniges | 0:733afbfa4d3f | 164 | }*/ |
mark1998 | 3:c9057d642843 | 165 | do |
chenniges | 0:733afbfa4d3f | 166 | { |
mark1998 | 3:c9057d642843 | 167 | if (switch1 == 1) |
chenniges | 0:733afbfa4d3f | 168 | { |
chenniges | 0:733afbfa4d3f | 169 | an++; |
chenniges | 0:733afbfa4d3f | 170 | if (an > 8) |
chenniges | 0:733afbfa4d3f | 171 | { |
chenniges | 0:733afbfa4d3f | 172 | an = 1; |
chenniges | 0:733afbfa4d3f | 173 | } |
chenniges | 0:733afbfa4d3f | 174 | lcd.cls(); |
chenniges | 1:fa25df63b18f | 175 | lcd.printf("Do you want to see the number of %d %c coins?",v[an],char(163)); |
mark1998 | 2:c95f08525bea | 176 | wait(3); |
chenniges | 0:733afbfa4d3f | 177 | //button1 = 0; |
chenniges | 0:733afbfa4d3f | 178 | } |
mark1998 | 3:c9057d642843 | 179 | else |
chenniges | 0:733afbfa4d3f | 180 | { |
chenniges | 0:733afbfa4d3f | 181 | 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 |
mark1998 | 3:c9057d642843 | 182 | wait(5); |
mark1998 | 3:c9057d642843 | 183 | lcd.cls(); |
mark1998 | 3:c9057d642843 | 184 | lcd.printf("If you would like to see other \ntypes of coins as well, please \nuse switch again."); |
mark1998 | 3:c9057d642843 | 185 | wait(5); |
mark1998 | 3:c9057d642843 | 186 | lcd.cls(); |
chenniges | 0:733afbfa4d3f | 187 | } |
chenniges | 0:733afbfa4d3f | 188 | } |
mark1998 | 3:c9057d642843 | 189 | while(switch1 == 1); |
mark1998 | 3:c9057d642843 | 190 | //wait(5); |
mark1998 | 3:c9057d642843 | 191 | //lcd.cls(); |
mark1998 | 3:c9057d642843 | 192 | |
mark1998 | 3:c9057d642843 | 193 | |
mark1998 | 3:c9057d642843 | 194 | lcd.printf("Please use the switch for finishing process."); |
mark1998 | 3:c9057d642843 | 195 | wait(4); |
mark1998 | 3:c9057d642843 | 196 | do |
mark1998 | 3:c9057d642843 | 197 | { |
mark1998 | 3:c9057d642843 | 198 | if(switch1 == 1) |
mark1998 | 3:c9057d642843 | 199 | { |
mark1998 | 3:c9057d642843 | 200 | lcd.cls(); |
mark1998 | 3:c9057d642843 | 201 | lcd.printf("Thank you for using 'Coounting Machine'. \nGood Bye. :-)"); |
mark1998 | 3:c9057d642843 | 202 | wait(5); |
mark1998 | 3:c9057d642843 | 203 | lcd.cls(); |
mark1998 | 3:c9057d642843 | 204 | } |
mark1998 | 3:c9057d642843 | 205 | }while (switch1 == 0); |
chenniges | 0:733afbfa4d3f | 206 | /*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 | 207 | { |
chenniges | 0:733afbfa4d3f | 208 | lcd.printf("Please enter the mount of money you took:"); |
chenniges | 0:733afbfa4d3f | 209 | scanf("%f",&tm); |
chenniges | 0:733afbfa4d3f | 210 | lcd.printf("%f",tm); |
chenniges | 0:733afbfa4d3f | 211 | wait(2); |
chenniges | 0:733afbfa4d3f | 212 | ta = ta-tm; |
chenniges | 0:733afbfa4d3f | 213 | 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 | 214 | }*/ |
chenniges | 0:733afbfa4d3f | 215 | |
chenniges | 0:733afbfa4d3f | 216 | return(0); |
chenniges | 0:733afbfa4d3f | 217 | } |
mark1998 | 3:c9057d642843 | 218 | //change all 1 to 0 at LDR1,2,... |