Strawberry Greenhouse V3
Dependencies: mbed C12832 Servo HCSR04 DHT DHT11
main.cpp@0:c48b28944702, 2021-10-12 (annotated)
- Committer:
- jbrown57
- Date:
- Tue Oct 12 19:23:55 2021 +0000
- Revision:
- 0:c48b28944702
- Child:
- 1:81cec44cec2e
For use with HCSR04 sonic sensor and MBED aplication board with LCD screen and RGB LED
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jbrown57 | 0:c48b28944702 | 1 | #include "mbed.h" |
jbrown57 | 0:c48b28944702 | 2 | |
jbrown57 | 0:c48b28944702 | 3 | #include "C12832.h" |
jbrown57 | 0:c48b28944702 | 4 | |
jbrown57 | 0:c48b28944702 | 5 | #include "tank.h" |
jbrown57 | 0:c48b28944702 | 6 | |
jbrown57 | 0:c48b28944702 | 7 | #include "HCSR04.h" |
jbrown57 | 0:c48b28944702 | 8 | |
jbrown57 | 0:c48b28944702 | 9 | |
jbrown57 | 0:c48b28944702 | 10 | C12832 lcd(p5, p7, p6, p8, p11); // LCD screen |
jbrown57 | 0:c48b28944702 | 11 | PwmOut r (p23); // r = red LED |
jbrown57 | 0:c48b28944702 | 12 | PwmOut g (p24); // g = green LED |
jbrown57 | 0:c48b28944702 | 13 | |
jbrown57 | 0:c48b28944702 | 14 | |
jbrown57 | 0:c48b28944702 | 15 | int main() |
jbrown57 | 0:c48b28944702 | 16 | |
jbrown57 | 0:c48b28944702 | 17 | { |
jbrown57 | 0:c48b28944702 | 18 | |
jbrown57 | 0:c48b28944702 | 19 | //welcome message on LCD |
jbrown57 | 0:c48b28944702 | 20 | |
jbrown57 | 0:c48b28944702 | 21 | lcd.cls(); |
jbrown57 | 0:c48b28944702 | 22 | |
jbrown57 | 0:c48b28944702 | 23 | lcd.locate(25,5); |
jbrown57 | 0:c48b28944702 | 24 | |
jbrown57 | 0:c48b28944702 | 25 | lcd.printf("Tank Water Level"); |
jbrown57 | 0:c48b28944702 | 26 | |
jbrown57 | 0:c48b28944702 | 27 | lcd.locate(2,15); |
jbrown57 | 0:c48b28944702 | 28 | |
jbrown57 | 0:c48b28944702 | 29 | lcd.printf("Greenhouse Control System"); |
jbrown57 | 0:c48b28944702 | 30 | |
jbrown57 | 0:c48b28944702 | 31 | wait(2.5); |
jbrown57 | 0:c48b28944702 | 32 | |
jbrown57 | 0:c48b28944702 | 33 | lcd.cls(); //clear LCD |
jbrown57 | 0:c48b28944702 | 34 | |
jbrown57 | 0:c48b28944702 | 35 | Timer timer; |
jbrown57 | 0:c48b28944702 | 36 | |
jbrown57 | 0:c48b28944702 | 37 | { |
jbrown57 | 0:c48b28944702 | 38 | |
jbrown57 | 0:c48b28944702 | 39 | HCSR04 sensor(p9, p20); |
jbrown57 | 0:c48b28944702 | 40 | sensor.setRanges(0, 500); // water tank level 500mm |
jbrown57 | 0:c48b28944702 | 41 | |
jbrown57 | 0:c48b28944702 | 42 | |
jbrown57 | 0:c48b28944702 | 43 | |
jbrown57 | 0:c48b28944702 | 44 | lcd.print_bm(bitmEmptyTank,80,0); // print Empty Tank at location x=85, y=0 |
jbrown57 | 0:c48b28944702 | 45 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 46 | //if needed: lcd.locate(0,10); |
jbrown57 | 0:c48b28944702 | 47 | //if needed: lcd.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r", |
jbrown57 | 0:c48b28944702 | 48 | //If needed: sensor.getMinRange(), sensor.getMaxRange()); |
jbrown57 | 0:c48b28944702 | 49 | |
jbrown57 | 0:c48b28944702 | 50 | while(true) { |
jbrown57 | 0:c48b28944702 | 51 | timer.reset(); |
jbrown57 | 0:c48b28944702 | 52 | timer.start(); |
jbrown57 | 0:c48b28944702 | 53 | sensor.startMeasurement(); |
jbrown57 | 0:c48b28944702 | 54 | while(!sensor.isNewDataReady()) { |
jbrown57 | 0:c48b28944702 | 55 | // wait for new data |
jbrown57 | 0:c48b28944702 | 56 | // waiting time depends on the distance |
jbrown57 | 0:c48b28944702 | 57 | } |
jbrown57 | 0:c48b28944702 | 58 | lcd.locate(20,5); |
jbrown57 | 0:c48b28944702 | 59 | lcd.printf("Distance:"); |
jbrown57 | 0:c48b28944702 | 60 | lcd.locate(20,17); |
jbrown57 | 0:c48b28944702 | 61 | lcd.printf ("%5.1f mm\r", sensor.getDistance_mm()); |
jbrown57 | 0:c48b28944702 | 62 | timer.stop(); |
jbrown57 | 0:c48b28944702 | 63 | wait_ms(500 - timer.read_ms()); // time the loop |
jbrown57 | 0:c48b28944702 | 64 | |
jbrown57 | 0:c48b28944702 | 65 | //turn RGB LED red if distance >400mm |
jbrown57 | 0:c48b28944702 | 66 | if( sensor.getDistance_mm()< 400) r=1; //red light on |
jbrown57 | 0:c48b28944702 | 67 | else r = 0; |
jbrown57 | 0:c48b28944702 | 68 | |
jbrown57 | 0:c48b28944702 | 69 | if( sensor.getDistance_mm()> 400) g=1; //green light on |
jbrown57 | 0:c48b28944702 | 70 | else g=0; //green light off |
jbrown57 | 0:c48b28944702 | 71 | |
jbrown57 | 0:c48b28944702 | 72 | |
jbrown57 | 0:c48b28944702 | 73 | if( sensor.getDistance_mm() < 40 ) lcd.print_bm(bitmTank,80,0); //full tank |
jbrown57 | 0:c48b28944702 | 74 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 75 | if( sensor.getDistance_mm() > 80 ) lcd.print_bm(bitmNinetyTank,80,0); //90% full tank |
jbrown57 | 0:c48b28944702 | 76 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 77 | if( sensor.getDistance_mm() > 120 ) lcd.print_bm(bitmEightyTank,80,0); //80% full tank |
jbrown57 | 0:c48b28944702 | 78 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 79 | if( sensor.getDistance_mm() > 160 ) lcd.print_bm(bitmSeventyTank,80,0); //70% full tank |
jbrown57 | 0:c48b28944702 | 80 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 81 | if( sensor.getDistance_mm() > 200 ) lcd.print_bm(bitmSixtyTank,80,0); //60% full tank |
jbrown57 | 0:c48b28944702 | 82 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 83 | if( sensor.getDistance_mm() > 240 ) lcd.print_bm(bitmFiftyTank,80,0); //50% full tank |
jbrown57 | 0:c48b28944702 | 84 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 85 | if( sensor.getDistance_mm() > 280 ) lcd.print_bm(bitmFourtyTank,80,0); //40% full tank |
jbrown57 | 0:c48b28944702 | 86 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 87 | if( sensor.getDistance_mm() > 320 ) lcd.print_bm(bitmThirtyTank,80,0); //30% full tank |
jbrown57 | 0:c48b28944702 | 88 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 89 | if( sensor.getDistance_mm() > 360 ) lcd.print_bm(bitmTwentyTank,80,0); //20% full tank |
jbrown57 | 0:c48b28944702 | 90 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 91 | if( sensor.getDistance_mm() > 400 ) lcd.print_bm(bitmTenTank,80,0); //10% full tank |
jbrown57 | 0:c48b28944702 | 92 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 93 | if( sensor.getDistance_mm() > 440 ) lcd.print_bm(bitmEmptyTank,80,0); //empty tank |
jbrown57 | 0:c48b28944702 | 94 | lcd.copy_to_lcd(); |
jbrown57 | 0:c48b28944702 | 95 | |
jbrown57 | 0:c48b28944702 | 96 | } |
jbrown57 | 0:c48b28944702 | 97 | } |
jbrown57 | 0:c48b28944702 | 98 | |
jbrown57 | 0:c48b28944702 | 99 | |
jbrown57 | 0:c48b28944702 | 100 | // https://os.mbed.com/media/uploads/Njbuch/wiley_publishing_-_c_for_dummies_2nd.edition_-2004-.pdf |
jbrown57 | 0:c48b28944702 | 101 | |
jbrown57 | 0:c48b28944702 | 102 | |
jbrown57 | 0:c48b28944702 | 103 | |
jbrown57 | 0:c48b28944702 | 104 | |
jbrown57 | 0:c48b28944702 | 105 | } |
jbrown57 | 0:c48b28944702 | 106 |