Senso_3

Committer:
balsijoe
Date:
Fri May 14 15:12:27 2021 +0000
Revision:
13:ab5d255db0f5
Parent:
12:014d7359785d
Sensoren Test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
boro 0:5d4d21d56334 1 #include "mbed.h"
boro 0:5d4d21d56334 2 #include "platform/mbed_thread.h"
pmic 9:c5e1e1facb02 3
pmic 3:aa1d854807fe 4 using namespace std::chrono;
pmic 9:c5e1e1facb02 5
pmic 7:dc463bf54be6 6 InterruptIn user_button(USER_BUTTON);
pmic 7:dc463bf54be6 7 DigitalOut led(LED1);
pmic 9:c5e1e1facb02 8
pmic 7:dc463bf54be6 9 bool executeMainTask = false;
pmic 7:dc463bf54be6 10 Timer user_button_timer, loop_timer;
pmic 7:dc463bf54be6 11 int Ts_ms = 50;
pmic 9:c5e1e1facb02 12
pmic 7:dc463bf54be6 13 void button_fall();
pmic 7:dc463bf54be6 14 void button_rise();
pmic 5:887081decd5c 15
balsijoe 13:ab5d255db0f5 16 //Peripherie
balsijoe 13:ab5d255db0f5 17 AnalogIn analog_in_A0(PA_0); //FS Futter Behälter Ultraschall
balsijoe 13:ab5d255db0f5 18 AnalogIn analog_in_A1(PA_1); //FS Wasser Behälter Ultraschall
balsijoe 13:ab5d255db0f5 19 AnalogIn analog_in_A2(PA_4); //FS Futter Napf Waage
balsijoe 13:ab5d255db0f5 20 AnalogIn analog_in_A3(PB_0); //FS Wasser Napf Wasserstand
balsijoe 13:ab5d255db0f5 21 //AnalogOut analog_out_PA_4(PA_4); //Futterausgabe DC-Motor
balsijoe 13:ab5d255db0f5 22 //AnalogOut analog_out_PA_5(PA_5); //Wasserausgabe Ventil
balsijoe 13:ab5d255db0f5 23 //DigitalOut Low_Level_PA_6(PA_6); //Warn-LED Füllstand
balsijoe 13:ab5d255db0f5 24
balsijoe 13:ab5d255db0f5 25 float Food_US = 0.0f;
balsijoe 13:ab5d255db0f5 26 float Water_US = 0.0f;
balsijoe 13:ab5d255db0f5 27 float Food_Bowl = 0.0f;
balsijoe 13:ab5d255db0f5 28 float Water_Bowl = 0.0f;
balsijoe 13:ab5d255db0f5 29 int counter = 0;
balsijoe 13:ab5d255db0f5 30
boro 0:5d4d21d56334 31 int main()
boro 0:5d4d21d56334 32 {
pmic 1:4e0e4d0363d9 33 user_button.fall(&button_fall);
pmic 1:4e0e4d0363d9 34 user_button.rise(&button_rise);
pmic 6:6c1c38d4faa4 35 loop_timer.start();
pmic 9:c5e1e1facb02 36
boro 0:5d4d21d56334 37 while (true) {
pmic 9:c5e1e1facb02 38
pmic 6:6c1c38d4faa4 39 loop_timer.reset();
pmic 9:c5e1e1facb02 40
pmic 1:4e0e4d0363d9 41 /* ------------- start hacking ------------- -------------*/
pmic 9:c5e1e1facb02 42
pmic 1:4e0e4d0363d9 43 if(executeMainTask) {
balsijoe 13:ab5d255db0f5 44 //Futterlager
balsijoe 13:ab5d255db0f5 45 Food_US = analog_in_A0.read();
balsijoe 13:ab5d255db0f5 46 //Wasserlager
balsijoe 13:ab5d255db0f5 47 Water_US = analog_in_A1.read();
balsijoe 13:ab5d255db0f5 48 //Futternapf
balsijoe 13:ab5d255db0f5 49 Food_Bowl = analog_in_A2.read();
balsijoe 13:ab5d255db0f5 50 //Wassernapf
balsijoe 13:ab5d255db0f5 51 Water_Bowl = analog_in_A3.read();
balsijoe 13:ab5d255db0f5 52
balsijoe 13:ab5d255db0f5 53 //Ausgabe
balsijoe 13:ab5d255db0f5 54 switch(counter) {
balsijoe 13:ab5d255db0f5 55 case 1:
balsijoe 13:ab5d255db0f5 56 printf("Futterlager: %d\r\n", (static_cast<int>(Food_US*100)));
balsijoe 13:ab5d255db0f5 57 break;
balsijoe 13:ab5d255db0f5 58 case 2:
balsijoe 13:ab5d255db0f5 59 printf("Wasserlager: %d\r\n", (static_cast<int>(Water_US*100)));
balsijoe 13:ab5d255db0f5 60 break;
balsijoe 13:ab5d255db0f5 61 case 3:
balsijoe 13:ab5d255db0f5 62 printf("Futterpegel: %d\r\n", (static_cast<int>(Food_Bowl*100)));
balsijoe 13:ab5d255db0f5 63 break;
balsijoe 13:ab5d255db0f5 64 case 4:
balsijoe 13:ab5d255db0f5 65 printf("Wasserpegel: %d\r\n\n\n", (static_cast<int>(Water_Bowl*100)));
balsijoe 13:ab5d255db0f5 66 break;
balsijoe 13:ab5d255db0f5 67 case 20:
balsijoe 13:ab5d255db0f5 68 counter = 0;
balsijoe 13:ab5d255db0f5 69 break;
balsijoe 13:ab5d255db0f5 70 default:
balsijoe 13:ab5d255db0f5 71 break;
balsijoe 13:ab5d255db0f5 72 }
balsijoe 13:ab5d255db0f5 73 counter++;
balsijoe 13:ab5d255db0f5 74
balsijoe 13:ab5d255db0f5 75 /* ------------- stop hacking ------------- -------------*/
pmic 6:6c1c38d4faa4 76 led = !led;
boro 0:5d4d21d56334 77 } else {
pmic 6:6c1c38d4faa4 78 led = 0;
boro 0:5d4d21d56334 79 }
balsijoe 13:ab5d255db0f5 80
pmic 6:6c1c38d4faa4 81 int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count();
pmic 6:6c1c38d4faa4 82 int dT_loop_ms = Ts_ms - T_loop_ms;
pmic 7:dc463bf54be6 83 thread_sleep_for(dT_loop_ms);
boro 0:5d4d21d56334 84 }
boro 0:5d4d21d56334 85 }
pmic 9:c5e1e1facb02 86
pmic 1:4e0e4d0363d9 87 void button_fall()
pmic 1:4e0e4d0363d9 88 {
pmic 1:4e0e4d0363d9 89 user_button_timer.reset();
pmic 1:4e0e4d0363d9 90 user_button_timer.start();
pmic 1:4e0e4d0363d9 91 }
pmic 9:c5e1e1facb02 92
pmic 1:4e0e4d0363d9 93 void button_rise()
pmic 1:4e0e4d0363d9 94 {
pmic 3:aa1d854807fe 95 int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count();
pmic 1:4e0e4d0363d9 96 user_button_timer.stop();
pmic 1:4e0e4d0363d9 97 if(t_button > 200) executeMainTask = !executeMainTask;
pmic 6:6c1c38d4faa4 98 }