final code (doesnt work)
Dependencies: mbed Servo C12832_lcd
main.cpp@1:52cda602892c, 2018-11-23 (annotated)
- Committer:
- mazmonem
- Date:
- Fri Nov 23 16:37:21 2018 +0000
- Revision:
- 1:52cda602892c
- Parent:
- 0:65b5886093c5
final code;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mazmonem | 0:65b5886093c5 | 1 | #include "mbed.h" |
mazmonem | 0:65b5886093c5 | 2 | #include "Servo.h" |
mazmonem | 0:65b5886093c5 | 3 | #include "C12832_lcd.h" |
mazmonem | 0:65b5886093c5 | 4 | #include "IR_Dist.h" |
mazmonem | 0:65b5886093c5 | 5 | #include "FSR.h" |
mazmonem | 0:65b5886093c5 | 6 | |
mazmonem | 0:65b5886093c5 | 7 | Timeout increment; |
mazmonem | 0:65b5886093c5 | 8 | Timeout interrupt; |
mazmonem | 0:65b5886093c5 | 9 | InterruptIn down(p12); |
mazmonem | 0:65b5886093c5 | 10 | InterruptIn left (p13); |
mazmonem | 0:65b5886093c5 | 11 | InterruptIn centre(p14); |
mazmonem | 0:65b5886093c5 | 12 | InterruptIn up(p15); |
mazmonem | 0:65b5886093c5 | 13 | InterruptIn right(p16); |
mazmonem | 1:52cda602892c | 14 | Servo myservo(p21); |
mazmonem | 1:52cda602892c | 15 | C12832_LCD lcd; |
mazmonem | 1:52cda602892c | 16 | AnalogIn fsr(p17); |
mazmonem | 1:52cda602892c | 17 | AnalogIn dist(p18); |
mazmonem | 0:65b5886093c5 | 18 | |
mazmonem | 0:65b5886093c5 | 19 | float iraverage; //averages ir sensor input |
mazmonem | 0:65b5886093c5 | 20 | float fsraverage; //averages fsr input |
mazmonem | 0:65b5886093c5 | 21 | |
mazmonem | 0:65b5886093c5 | 22 | /** |
mazmonem | 0:65b5886093c5 | 23 | code for servo motor to run |
mazmonem | 0:65b5886093c5 | 24 | */ |
mazmonem | 0:65b5886093c5 | 25 | void servo() |
mazmonem | 0:65b5886093c5 | 26 | { |
mazmonem | 0:65b5886093c5 | 27 | if (fsraverage = 0) { //will only run if the fsr reading is below a certain value |
mazmonem | 0:65b5886093c5 | 28 | myservo = 0; //position of the servo is initially at its starting point |
mazmonem | 0:65b5886093c5 | 29 | wait(1); //waits 1 second |
mazmonem | 0:65b5886093c5 | 30 | myservo = 1; //moves the servo 90 degrees |
mazmonem | 0:65b5886093c5 | 31 | wait(1); //waits another second |
mazmonem | 0:65b5886093c5 | 32 | } |
mazmonem | 0:65b5886093c5 | 33 | } |
mazmonem | 0:65b5886093c5 | 34 | int hours = 0; //defines an integer value called hours and sets it to zero initially |
mazmonem | 0:65b5886093c5 | 35 | int minutes = 0; //defines an integer value called minutes and sets it to zero initially |
mazmonem | 0:65b5886093c5 | 36 | |
mazmonem | 0:65b5886093c5 | 37 | /** |
mazmonem | 0:65b5886093c5 | 38 | Adjust minutes |
mazmonem | 0:65b5886093c5 | 39 | */ |
mazmonem | 0:65b5886093c5 | 40 | |
mazmonem | 0:65b5886093c5 | 41 | void adj_min() |
mazmonem | 0:65b5886093c5 | 42 | { |
mazmonem | 0:65b5886093c5 | 43 | if (minutes > 59) { // if the value of minutes is greater than 59 |
mazmonem | 0:65b5886093c5 | 44 | minutes = 0; //minutes will reset to zero |
mazmonem | 0:65b5886093c5 | 45 | } |
mazmonem | 0:65b5886093c5 | 46 | } |
mazmonem | 0:65b5886093c5 | 47 | |
mazmonem | 0:65b5886093c5 | 48 | /** |
mazmonem | 0:65b5886093c5 | 49 | Adjust hours |
mazmonem | 0:65b5886093c5 | 50 | */ |
mazmonem | 0:65b5886093c5 | 51 | |
mazmonem | 0:65b5886093c5 | 52 | void adj_hrs() |
mazmonem | 0:65b5886093c5 | 53 | { |
mazmonem | 0:65b5886093c5 | 54 | if (hours > 23) { //if the value of hours is greater than 23 |
mazmonem | 0:65b5886093c5 | 55 | hours = 0; //set hours to zero |
mazmonem | 0:65b5886093c5 | 56 | } |
mazmonem | 0:65b5886093c5 | 57 | } |
mazmonem | 0:65b5886093c5 | 58 | |
mazmonem | 0:65b5886093c5 | 59 | /** |
mazmonem | 0:65b5886093c5 | 60 | Increment minutes |
mazmonem | 0:65b5886093c5 | 61 | */ |
mazmonem | 0:65b5886093c5 | 62 | |
mazmonem | 0:65b5886093c5 | 63 | void inc_min() |
mazmonem | 0:65b5886093c5 | 64 | { |
mazmonem | 0:65b5886093c5 | 65 | minutes++; //increase the value of minutes by one |
mazmonem | 0:65b5886093c5 | 66 | adj_min(); //updates the value of adj_min |
mazmonem | 0:65b5886093c5 | 67 | } |
mazmonem | 0:65b5886093c5 | 68 | |
mazmonem | 0:65b5886093c5 | 69 | /** |
mazmonem | 0:65b5886093c5 | 70 | Increment hours |
mazmonem | 0:65b5886093c5 | 71 | */ |
mazmonem | 0:65b5886093c5 | 72 | |
mazmonem | 0:65b5886093c5 | 73 | void inc_hrs() |
mazmonem | 0:65b5886093c5 | 74 | { |
mazmonem | 0:65b5886093c5 | 75 | |
mazmonem | 0:65b5886093c5 | 76 | hours++; //increases the value of hours by one |
mazmonem | 0:65b5886093c5 | 77 | adj_hrs(); //updates the value of adj_hrs |
mazmonem | 0:65b5886093c5 | 78 | } |
mazmonem | 0:65b5886093c5 | 79 | /** |
mazmonem | 0:65b5886093c5 | 80 | Decrement minutes |
mazmonem | 0:65b5886093c5 | 81 | */ |
mazmonem | 0:65b5886093c5 | 82 | |
mazmonem | 0:65b5886093c5 | 83 | void dec_min() |
mazmonem | 0:65b5886093c5 | 84 | { |
mazmonem | 0:65b5886093c5 | 85 | minutes--; //decrease the value of minutes by 1 |
mazmonem | 0:65b5886093c5 | 86 | adj_min(); //updates the value of adj_min |
mazmonem | 0:65b5886093c5 | 87 | } |
mazmonem | 0:65b5886093c5 | 88 | |
mazmonem | 0:65b5886093c5 | 89 | /** |
mazmonem | 0:65b5886093c5 | 90 | Decrement hours |
mazmonem | 0:65b5886093c5 | 91 | */ |
mazmonem | 0:65b5886093c5 | 92 | |
mazmonem | 0:65b5886093c5 | 93 | void dec_hrs() |
mazmonem | 0:65b5886093c5 | 94 | { |
mazmonem | 0:65b5886093c5 | 95 | hours--; //decreases the value of hours by 1 |
mazmonem | 0:65b5886093c5 | 96 | adj_hrs(); //updates the value of adj_hrs |
mazmonem | 0:65b5886093c5 | 97 | } |
mazmonem | 0:65b5886093c5 | 98 | |
mazmonem | 0:65b5886093c5 | 99 | |
mazmonem | 0:65b5886093c5 | 100 | void down_rise() |
mazmonem | 0:65b5886093c5 | 101 | { |
mazmonem | 0:65b5886093c5 | 102 | if (left) { |
mazmonem | 0:65b5886093c5 | 103 | dec_hrs(); |
mazmonem | 0:65b5886093c5 | 104 | } else if (right) { |
mazmonem | 0:65b5886093c5 | 105 | dec_min(); |
mazmonem | 0:65b5886093c5 | 106 | } |
mazmonem | 0:65b5886093c5 | 107 | } |
mazmonem | 0:65b5886093c5 | 108 | void up_rise() |
mazmonem | 0:65b5886093c5 | 109 | { |
mazmonem | 0:65b5886093c5 | 110 | if (left) { |
mazmonem | 0:65b5886093c5 | 111 | inc_hrs(); |
mazmonem | 0:65b5886093c5 | 112 | } else if (right) { |
mazmonem | 0:65b5886093c5 | 113 | inc_min(); |
mazmonem | 0:65b5886093c5 | 114 | } |
mazmonem | 0:65b5886093c5 | 115 | } |
mazmonem | 0:65b5886093c5 | 116 | |
mazmonem | 0:65b5886093c5 | 117 | int main() |
mazmonem | 0:65b5886093c5 | 118 | { |
mazmonem | 0:65b5886093c5 | 119 | down.rise(&down_rise); |
mazmonem | 0:65b5886093c5 | 120 | up.rise(&up_rise); |
mazmonem | 0:65b5886093c5 | 121 | int set = 1; // sets the value of the integer set to 1 initially |
mazmonem | 0:65b5886093c5 | 122 | myservo.calibrate(0.0005,45); //calibrating the float degrees and float position |
mazmonem | 0:65b5886093c5 | 123 | while(1) { |
mazmonem | 0:65b5886093c5 | 124 | increment.attach(&inc_min, 60.0); |
mazmonem | 0:65b5886093c5 | 125 | lcd.cls(); |
mazmonem | 0:65b5886093c5 | 126 | lcd.locate(0,3); //location of text on the lcd |
mazmonem | 0:65b5886093c5 | 127 | /** |
mazmonem | 0:65b5886093c5 | 128 | defines ir sensor values |
mazmonem | 0:65b5886093c5 | 129 | */ |
mazmonem | 0:65b5886093c5 | 130 | iraverage = 0.0; //ir sensor average starts at 0 |
mazmonem | 0:65b5886093c5 | 131 | for (int j=0; j<1000; j++) { |
mazmonem | 0:65b5886093c5 | 132 | iraverage = iraverage + dist.read()/1000.0; //displays the average per 1000 readings |
mazmonem | 0:65b5886093c5 | 133 | wait(0.2); //waits 0.2 seconds to repeat the cycle |
mazmonem | 0:65b5886093c5 | 134 | } |
mazmonem | 0:65b5886093c5 | 135 | /** |
mazmonem | 0:65b5886093c5 | 136 | defines fsr values |
mazmonem | 0:65b5886093c5 | 137 | */ |
mazmonem | 0:65b5886093c5 | 138 | fsraverage = 0.0; //average of the ir sensor values is 0 |
mazmonem | 0:65b5886093c5 | 139 | for (int k=0; k<1000; k++) { |
mazmonem | 0:65b5886093c5 | 140 | fsraverage = fsraverage + fsr.read()/1000.0; //displays the average per 1000 readings |
mazmonem | 0:65b5886093c5 | 141 | wait(0.2); //waits 0.2 seconds to repeat the cycle |
mazmonem | 0:65b5886093c5 | 142 | } |
mazmonem | 0:65b5886093c5 | 143 | /** |
mazmonem | 0:65b5886093c5 | 144 | condition at which the system will not work |
mazmonem | 0:65b5886093c5 | 145 | */ |
mazmonem | 0:65b5886093c5 | 146 | if ((iraverage = 0) || (fsraverage = 0)) { |
mazmonem | 0:65b5886093c5 | 147 | myservo = 0; |
mazmonem | 0:65b5886093c5 | 148 | |
mazmonem | 0:65b5886093c5 | 149 | if (centre) { |
mazmonem | 0:65b5886093c5 | 150 | if (up) { |
mazmonem | 0:65b5886093c5 | 151 | if (up) { //switching between increasing minutes and hours |
mazmonem | 0:65b5886093c5 | 152 | up_rise(); |
mazmonem | 0:65b5886093c5 | 153 | } |
mazmonem | 0:65b5886093c5 | 154 | |
mazmonem | 0:65b5886093c5 | 155 | if (down) { //switching between decreasing minutes and hours |
mazmonem | 0:65b5886093c5 | 156 | down_rise(); |
mazmonem | 0:65b5886093c5 | 157 | } |
mazmonem | 0:65b5886093c5 | 158 | |
mazmonem | 0:65b5886093c5 | 159 | lcd.cls(); |
mazmonem | 0:65b5886093c5 | 160 | lcd.locate(10, 10); |
mazmonem | 0:65b5886093c5 | 161 | |
mazmonem | 0:65b5886093c5 | 162 | // Check if minutes are less than 10 so a 0 can be prefixed onto the display |
mazmonem | 0:65b5886093c5 | 163 | char mins[10] = "00"; // String to display in lcd screen |
mazmonem | 0:65b5886093c5 | 164 | |
mazmonem | 0:65b5886093c5 | 165 | if (minutes < 10) { |
mazmonem | 0:65b5886093c5 | 166 | strcpy(mins, "0" + minutes); |
mazmonem | 0:65b5886093c5 | 167 | } else { |
mazmonem | 0:65b5886093c5 | 168 | strcpy(mins, "" + minutes); |
mazmonem | 0:65b5886093c5 | 169 | } |
mazmonem | 0:65b5886093c5 | 170 | lcd.printf("%s", mins); |
mazmonem | 0:65b5886093c5 | 171 | lcd.locate(20, 10); |
mazmonem | 0:65b5886093c5 | 172 | { |
mazmonem | 0:65b5886093c5 | 173 | lcd.printf("%d", hours); |
mazmonem | 0:65b5886093c5 | 174 | } |
mazmonem | 0:65b5886093c5 | 175 | } |
mazmonem | 0:65b5886093c5 | 176 | /** |
mazmonem | 0:65b5886093c5 | 177 | portion size settings |
mazmonem | 0:65b5886093c5 | 178 | */ |
mazmonem | 0:65b5886093c5 | 179 | if (down) |
mazmonem | 0:65b5886093c5 | 180 | if (centre) { //if centre joystick is pressed |
mazmonem | 0:65b5886093c5 | 181 | lcd.printf("1"); //will print a '1' on the lcd screen |
mazmonem | 0:65b5886093c5 | 182 | set = 1; //sets the value of set to 1 |
mazmonem | 0:65b5886093c5 | 183 | } |
mazmonem | 0:65b5886093c5 | 184 | if (down) { //if the down joystick is pressed |
mazmonem | 0:65b5886093c5 | 185 | lcd.printf("2"); //prints '2' on the lcd screen |
mazmonem | 0:65b5886093c5 | 186 | set = 2; //sets the value of set to 2 |
mazmonem | 0:65b5886093c5 | 187 | } |
mazmonem | 0:65b5886093c5 | 188 | if (up) { //if the up joystick is pressed |
mazmonem | 0:65b5886093c5 | 189 | lcd.printf("3"); //prints '3' on the lcd screen |
mazmonem | 0:65b5886093c5 | 190 | set = 3; //sets the value of set to 3 |
mazmonem | 0:65b5886093c5 | 191 | } |
mazmonem | 0:65b5886093c5 | 192 | } |
mazmonem | 0:65b5886093c5 | 193 | } |
mazmonem | 0:65b5886093c5 | 194 | /** |
mazmonem | 0:65b5886093c5 | 195 | automatically dispensing food at certain times of day |
mazmonem | 0:65b5886093c5 | 196 | */ |
mazmonem | 0:65b5886093c5 | 197 | if ((hours = 6) || (hours = 12) || (hours = 18)) { //if the hours is equal to either 6, 12 or 18 |
mazmonem | 0:65b5886093c5 | 198 | int i; |
mazmonem | 0:65b5886093c5 | 199 | for (i = set; i = i + 1;) { //will executed the code a number of times, dependent on what the value of set is |
mazmonem | 0:65b5886093c5 | 200 | servo(); //executes servo code |
mazmonem | 0:65b5886093c5 | 201 | } |
mazmonem | 0:65b5886093c5 | 202 | } |
mazmonem | 0:65b5886093c5 | 203 | } |
mazmonem | 0:65b5886093c5 | 204 | } |