For use with MBED application board

Dependencies:   mbed C12832 HCSR04

Committer:
jbrown57
Date:
Tue Oct 19 17:14:52 2021 +0000
Revision:
1:81cec44cec2e
Parent:
0:c48b28944702
water tank

Who changed what in which revision?

UserRevisionLine numberNew 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 int main()
jbrown57 0:c48b28944702 15
jbrown57 0:c48b28944702 16 {
jbrown57 0:c48b28944702 17
jbrown57 0:c48b28944702 18 //welcome message on LCD
jbrown57 0:c48b28944702 19
jbrown57 0:c48b28944702 20 lcd.cls();
jbrown57 0:c48b28944702 21
jbrown57 0:c48b28944702 22 lcd.locate(25,5);
jbrown57 0:c48b28944702 23
jbrown57 0:c48b28944702 24 lcd.printf("Tank Water Level");
jbrown57 0:c48b28944702 25
jbrown57 0:c48b28944702 26 lcd.locate(2,15);
jbrown57 0:c48b28944702 27
jbrown57 0:c48b28944702 28 lcd.printf("Greenhouse Control System");
jbrown57 0:c48b28944702 29
jbrown57 0:c48b28944702 30 wait(2.5);
jbrown57 0:c48b28944702 31
jbrown57 0:c48b28944702 32 lcd.cls(); //clear LCD
jbrown57 0:c48b28944702 33
jbrown57 1:81cec44cec2e 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 1:81cec44cec2e 44 lcd.print_bm(bitmEmptyTank,80,0); // print Empty Tank at location x=80, 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 1:81cec44cec2e 66
jbrown57 1:81cec44cec2e 67 if( sensor.getDistance_mm()< 400) r= 1; //red light on
jbrown57 0:c48b28944702 68 else r = 0;
jbrown57 0:c48b28944702 69
jbrown57 1:81cec44cec2e 70 if( sensor.getDistance_mm()> 400) g=1; //green light on
jbrown57 0:c48b28944702 71 else g=0; //green light off
jbrown57 0:c48b28944702 72
jbrown57 0:c48b28944702 73
jbrown57 0:c48b28944702 74 if( sensor.getDistance_mm() < 40 ) lcd.print_bm(bitmTank,80,0); //full tank
jbrown57 0:c48b28944702 75 lcd.copy_to_lcd();
jbrown57 0:c48b28944702 76 if( sensor.getDistance_mm() > 80 ) lcd.print_bm(bitmNinetyTank,80,0); //90% full tank
jbrown57 0:c48b28944702 77 lcd.copy_to_lcd();
jbrown57 0:c48b28944702 78 if( sensor.getDistance_mm() > 120 ) lcd.print_bm(bitmEightyTank,80,0); //80% full tank
jbrown57 0:c48b28944702 79 lcd.copy_to_lcd();
jbrown57 0:c48b28944702 80 if( sensor.getDistance_mm() > 160 ) lcd.print_bm(bitmSeventyTank,80,0); //70% full tank
jbrown57 0:c48b28944702 81 lcd.copy_to_lcd();
jbrown57 0:c48b28944702 82 if( sensor.getDistance_mm() > 200 ) lcd.print_bm(bitmSixtyTank,80,0); //60% full tank
jbrown57 0:c48b28944702 83 lcd.copy_to_lcd();
jbrown57 0:c48b28944702 84 if( sensor.getDistance_mm() > 240 ) lcd.print_bm(bitmFiftyTank,80,0); //50% full tank
jbrown57 0:c48b28944702 85 lcd.copy_to_lcd();
jbrown57 0:c48b28944702 86 if( sensor.getDistance_mm() > 280 ) lcd.print_bm(bitmFourtyTank,80,0); //40% full tank
jbrown57 0:c48b28944702 87 lcd.copy_to_lcd();
jbrown57 0:c48b28944702 88 if( sensor.getDistance_mm() > 320 ) lcd.print_bm(bitmThirtyTank,80,0); //30% full tank
jbrown57 0:c48b28944702 89 lcd.copy_to_lcd();
jbrown57 0:c48b28944702 90 if( sensor.getDistance_mm() > 360 ) lcd.print_bm(bitmTwentyTank,80,0); //20% full tank
jbrown57 0:c48b28944702 91 lcd.copy_to_lcd();
jbrown57 0:c48b28944702 92 if( sensor.getDistance_mm() > 400 ) lcd.print_bm(bitmTenTank,80,0); //10% full tank
jbrown57 0:c48b28944702 93 lcd.copy_to_lcd();
jbrown57 0:c48b28944702 94 if( sensor.getDistance_mm() > 440 ) lcd.print_bm(bitmEmptyTank,80,0); //empty tank
jbrown57 0:c48b28944702 95 lcd.copy_to_lcd();
jbrown57 0:c48b28944702 96
jbrown57 1:81cec44cec2e 97
jbrown57 0:c48b28944702 98 }
jbrown57 0:c48b28944702 99 }
jbrown57 0:c48b28944702 100
jbrown57 0:c48b28944702 101
jbrown57 0:c48b28944702 102 // https://os.mbed.com/media/uploads/Njbuch/wiley_publishing_-_c_for_dummies_2nd.edition_-2004-.pdf
jbrown57 0:c48b28944702 103
jbrown57 0:c48b28944702 104
jbrown57 0:c48b28944702 105
jbrown57 0:c48b28944702 106
jbrown57 0:c48b28944702 107 }
jbrown57 0:c48b28944702 108