Give water to plants if dry amd pla meanwhile music the music vou can define with note length bind and breake also select several instruments for the sound

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "waves.h"
00003 #include "EthernetNetIf.h"
00004 #include "HTTPServer.h"
00005 #include "DebugTrace.h"
00006 
00007 EthernetNetIf eth (IpAddr(192,168,1,101), IpAddr(255,255,255,0), IpAddr(192,168,1,1), IpAddr(195,50,140,246));
00008 //EthernetNetIf eth; //for DHCP
00009 HTTPServer svr;
00010 
00011 DigitalOut myled1(LED1);
00012 DigitalOut myled2(LED2);
00013 DigitalOut myled3(LED3);
00014 DigitalOut myled4(LED4);
00015 LocalFileSystem fs("local");
00016 DebugTrace file(ON, TO_FILE);    // i.e. file(ON, TO_FILE, "log.txt", 1024)
00017 
00018 float combind;
00019 
00020 bool Check_earth();
00021 void Motor_run();
00022 void Play_music(int track);
00023 void Play_Eine_kleine_Nachtmusik();
00024 void Play_5th_Symphonie();
00025 // Play_Note(welche Note, welchen Länge, gebunden, Lautstärke, welches Instrument)
00026 void Play_Note(float note, float lengh, bool combine, float loud, int instrument);
00027 void Play_Pause(float lengh);
00028 void Play_ladder();
00029 float Check_loud();
00030 int val = 122;
00031 float fval = 1.414;
00032 
00033 
00034 int main() {
00035     bool too_low;
00036     Base::add_rpc_class<AnalogIn>();
00037     Base::add_rpc_class<AnalogOut>();
00038     Base::add_rpc_class<DigitalIn>();
00039     Base::add_rpc_class<DigitalOut>();
00040     Base::add_rpc_class<PwmOut>();
00041     Base::add_rpc_class<Timer>();
00042     Base::add_rpc_class<SPI>();
00043     Base::add_rpc_class<BusOut>();
00044     Base::add_rpc_class<BusIn>();
00045 
00046     EthernetErr ethErr = eth.setup();
00047     if (ethErr) {
00048         printf("Error %d in setup.\n", ethErr);
00049         return -1;
00050     }
00051 
00052     FSHandler::mount("/local", "/files"); //Mount /webfs path on /files web path
00053     FSHandler::mount("/local", "/"); //Mount /webfs path on web root path
00054     svr.addHandler<SimpleHandler>("/hello");
00055     svr.addHandler<RPCHandler>("/rpc");
00056     svr.addHandler<FSHandler>("/files");
00057     svr.addHandler<FSHandler>("/"); //Default handler
00058     //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm
00059     IpAddr(192,168,1,101);
00060     
00061     svr.bind(80);
00062 
00063     while (1) {
00064         Net::poll();
00065         too_low = Check_earth();
00066         combind = 0;
00067         if (too_low == true) {
00068             Motor_run();
00069             myled1 = true;
00070             Play_music(0);
00071         } else {
00072             myled1 = false;
00073             Play_music(1);
00074 
00075         }
00076         myled2 = false;
00077         file.traceOut("Test message\r\n");
00078         file.traceOut("%x \r\n", val);
00079         file.traceOut("%d \r\n", val);
00080         file.traceOut("%f \r\n", fval);
00081     }
00082 }
00083 
00084 bool Check_earth() {
00085     float TRAY = 0.3;
00086     bool plant = false;
00087     AnalogIn  plant_(p20);
00088     //Einlesen = der Messwert
00089     if (plant_ > TRAY | plant_ == 0) {
00090         plant = true;
00091     }
00092     //Sollte ein Messwert <= TRAY sein, Pflanze zu trocken, muss die Pumpe anlaufen
00093     if (plant == false) {
00094         myled3 = true;
00095         return true;
00096     } else {
00097         myled3 = false;
00098         return false;
00099     }
00100 }
00101 
00102 void Motor_run() {
00103     DigitalOut motor(p14);
00104     motor = true;
00105     wait(0.2);
00106     motor = false;
00107 }
00108 
00109 void Play_music(int track) {
00110     if (track == 0) {
00111         Play_Eine_kleine_Nachtmusik();
00112     }
00113     if (track == 1) {
00114         Play_5th_Symphonie();
00115         //Play_ladder();
00116     }
00117 }
00118 
00119 void Play_Note(float note, float lengh, bool combine, float loud, int instrument) {
00120     int point_read;
00121     float lengh_index;
00122     long index;
00123     long index1;
00124     long index2;
00125     char* text;
00126     char* text2;
00127     float add;
00128     AnalogOut aout(p18);
00129 
00130     switch (instrument) { //what instrument, read number of points of Form of Instument, if not set 0
00131         case 1:
00132             point_read = Instrument1_;
00133             text = "Instrument1_";
00134             break;
00135         case 2:
00136             point_read = Instrument2_;
00137             text = "Instrument2_";
00138             break;
00139         case 3:
00140             point_read = Instrument3_;
00141             text = "Instrument3_";
00142             break;
00143         case 4:
00144             point_read = Instrument4_;
00145             text = "Instrument4_";
00146             break;
00147         case 5:
00148             point_read = Instrument5_;
00149             text = "Instrument5_";
00150             break;
00151         case 6:
00152             point_read = Instrument6_;
00153             text = "Instrument6_";
00154             break;
00155         case 7:
00156             point_read = Instrument7_;
00157             text = "Instrument7_";
00158             break;
00159         case 8:
00160             point_read = Instrument8_;
00161             text = "Instrument8_";
00162             break;
00163         case 9:
00164             point_read = Instrument9_;
00165             text = "Instrument9_";
00166             break;
00167         case 10:
00168             point_read = Instrument10_;
00169             text = "Instrument10_";
00170             break;
00171         case 11:
00172             point_read = Instrument11_;
00173             text = "Instrument11_";
00174             break;
00175         case 12:
00176             point_read = Instrument12_;
00177             text = "Instrument12_";
00178             break;
00179         case 13:
00180             point_read = Instrument13_;
00181             text = "Instrument13_";
00182             break;
00183         case 14:
00184             point_read = Instrument14_;
00185             text = "Instrument14_";
00186             break;
00187         case 15:
00188             point_read = Instrument15_;
00189             text = "Instrument15_";
00190             break;
00191         case 16:
00192             point_read = Instrument16_;
00193             text = "Instrument16_";
00194             break;
00195         case 17:
00196             point_read = Instrument17_;
00197             text = "Instrument17_";
00198             break;
00199         default:
00200             point_read = 0;
00201             text = "";
00202             break;
00203     } //end switch
00204 
00205     lengh_index = (1/(note * point_read))*100;
00206 
00207     for (index = 0; index < lengh; ++index) { //as long the tone up to 1s
00208         add = Check_loud();
00209         if (combind == 0) { //no combine
00210             myled2 = true;
00211             for (index1 = 0; index1 < point_read;++index1) { //upper wave
00212                 text = text + char(index1);
00213                 wait(lengh_index);
00214                 aout = 0.5 + (*text * (loud - add) * 0.5);
00215                 if (*text == 1) {
00216                 }
00217             }
00218             for (index2 = 0; index2 < point_read; ++index2) { //lower wave
00219                 text = text + char(index2);
00220                 wait(lengh_index);
00221                 aout = 0.5 - (*text * (loud - add) * 0.5);
00222                 if (*text == 1) {
00223                 }
00224             }
00225             myled2 = false;
00226         } else {
00227             myled3 = true;
00228             for (index1 = 0; index1 < point_read;++index1) { //upper wave
00229                 text = text + char(index1);
00230                 text2 = text + char(index1 - 1);
00231                 wait(lengh_index);
00232                 aout = 0.5 + ((*text + *text2) * 0.5 * (loud - add) * 0.5);
00233                 if (*text == 1) {
00234                 }
00235             }
00236             for (index2 = 0; index2 < point_read; ++index2) { //lower wave
00237                 text = text + char(index2);
00238                 text2 = text + char(index1 - 1);
00239                 wait(lengh_index);
00240                 aout = 0.5 - ((*text + *text2) * 0.5 * (loud - add) * 0.5);
00241                 if (*text == 1) {
00242                 }
00243             }
00244             myled3 = false;
00245             combind = 0; //combine clear
00246         }
00247     }
00248     if  (combine == true) { // next combine set
00249         combind = note;
00250     }
00251 }
00252 
00253 void Play_Pause(float lengh) {
00254     AnalogOut aout(p18);
00255 
00256     myled4 = true;
00257     aout = 0.5;
00258     wait(lengh);
00259     myled4 = false;
00260 }
00261 
00262 float Check_loud() {
00263     DigitalIn up(p5);
00264     DigitalIn down(p6);
00265 /*    while (up.read == 0 | index < 10000 */
00266 
00267             return 0;
00268         }