Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Alarmsystem_log_funksjon_funker_ikke by
main.cpp@12:0193024eab13, 2018-02-19 (annotated)
- Committer:
- andreped
- Date:
- Mon Feb 19 08:17:48 2018 +0000
- Revision:
- 12:0193024eab13
- Parent:
- 11:dad1798ed88f
- Child:
- 13:0622c228b67d
MBed os5 versjon;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| andreped | 0:33bf28fd1ac6 | 1 | #include "mbed.h" |
| andreped | 12:0193024eab13 | 2 | #include "rtos.h" |
| andreped | 12:0193024eab13 | 3 | #include <RtosTimer.h> |
| andreped | 11:dad1798ed88f | 4 | #include <math.h> |
| andreped | 0:33bf28fd1ac6 | 5 | |
| andreped | 12:0193024eab13 | 6 | |
| andreped | 12:0193024eab13 | 7 | #include "mbed_error.h" |
| andreped | 12:0193024eab13 | 8 | |
| andreped | 6:a3b97bf7902b | 9 | #define Inn 4 |
| andreped | 6:a3b97bf7902b | 10 | //#define Feil 0<1 |
| andreped | 10:f0a6d74095d4 | 11 | |
| andreped | 6:a3b97bf7902b | 12 | |
| andreped | 6:a3b97bf7902b | 13 | //DigitalOut myled2(LED2); |
| andreped | 4:aa2cff91ff4f | 14 | Serial pc(USBTX, USBRX); |
| andreped | 0:33bf28fd1ac6 | 15 | DigitalOut Driftslys(LED1); |
| andreped | 10:f0a6d74095d4 | 16 | BusOut AlarmUtganger1(p21, p22, p23, p24); //1 - Dør 1 Sone 1, 2 - Dør 1 Sone 2, 4 - Dør 1 Sone 3, 8 - Dør 1 Sone 4 |
| andreped | 10:f0a6d74095d4 | 17 | BusOut AlarmUtganger2(p25, p26, p27, p28); //1 - Dør 2 Sone 1, 2 - Dør 2 Sone 2, 4 - Dør 2 Sone 3, 8 - Dør 2 Sone 4 |
| andreped | 0:33bf28fd1ac6 | 18 | DigitalIn Servicebryter(p20); |
| andreped | 12:0193024eab13 | 19 | AnalogIn _Sone1(p15); |
| andreped | 12:0193024eab13 | 20 | AnalogIn _Sone2(p16); |
| andreped | 12:0193024eab13 | 21 | AnalogIn _Sone3(p17); |
| andreped | 12:0193024eab13 | 22 | AnalogIn _Sone4(p19); |
| andreped | 12:0193024eab13 | 23 | float Soner[Inn]; |
| andreped | 12:0193024eab13 | 24 | int tilstand = 0; |
| andreped | 11:dad1798ed88f | 25 | int TilstandSjekk(int, int); |
| andreped | 12:0193024eab13 | 26 | void Alarmf(); |
| andreped | 12:0193024eab13 | 27 | Thread thread; |
| andreped | 12:0193024eab13 | 28 | volatile bool running = true; |
| andreped | 12:0193024eab13 | 29 | //Ticker AlarmBlink; // 10Hz blinking for alarmfunksjon på lys og telling |
| andreped | 6:a3b97bf7902b | 30 | //nt Tid = 0; |
| andreped | 1:6917ec4f5e6d | 31 | |
| andreped | 1:6917ec4f5e6d | 32 | int Tilstandssjekk(); |
| andreped | 12:0193024eab13 | 33 | //void Alarm(); |
| andreped | 6:a3b97bf7902b | 34 | //volatile int Klokke = 0; |
| andreped | 1:6917ec4f5e6d | 35 | |
| andreped | 12:0193024eab13 | 36 | void Alarmf(void) { |
| andreped | 12:0193024eab13 | 37 | Driftslys = !Driftslys; |
| andreped | 12:0193024eab13 | 38 | } |
| andreped | 12:0193024eab13 | 39 | |
| andreped | 1:6917ec4f5e6d | 40 | int main() |
| andreped | 1:6917ec4f5e6d | 41 | { |
| andreped | 1:6917ec4f5e6d | 42 | while(1) { |
| andreped | 12:0193024eab13 | 43 | RtosTimer Alarm(Alarmf); |
| andreped | 12:0193024eab13 | 44 | // AlarmBlink.attach(&Alarm, 1); |
| andreped | 6:a3b97bf7902b | 45 | /*if(Servicebryter == 1) { |
| andreped | 1:6917ec4f5e6d | 46 | Utganger1 = 0; |
| andreped | 1:6917ec4f5e6d | 47 | Utganger2 = 0; |
| andreped | 6:a3b97bf7902b | 48 | }*/ |
| andreped | 6:a3b97bf7902b | 49 | Soner[0] = _Sone1; |
| andreped | 6:a3b97bf7902b | 50 | //Soner[1] = _Sone2; |
| andreped | 6:a3b97bf7902b | 51 | //Soner[2] = _Sone3; |
| andreped | 6:a3b97bf7902b | 52 | //Soner[3] = _Sone4; |
| andreped | 6:a3b97bf7902b | 53 | for(int i=0; i < Inn; i++) { |
| andreped | 11:dad1798ed88f | 54 | //pc.printf("%f\n", Soner[i]); |
| andreped | 6:a3b97bf7902b | 55 | if((Soner[i] > 0.75 )&&( Soner[i] < 0.83)) { //Tilstand1, Dør 1 åpen |
| andreped | 11:dad1798ed88f | 56 | //pc.printf("Vi er i Tilstand1\n"); |
| andreped | 11:dad1798ed88f | 57 | tilstand = 1; |
| andreped | 11:dad1798ed88f | 58 | TilstandSjekk(i, tilstand); |
| andreped | 6:a3b97bf7902b | 59 | } |
| andreped | 6:a3b97bf7902b | 60 | if((Soner[i] > 0.55 )&&( Soner[i] < 0.59)) { //Normaldrift |
| andreped | 12:0193024eab13 | 61 | //pc.printf("Vi er i Normaldrift\n") |
| andreped | 11:dad1798ed88f | 62 | tilstand = 0; |
| andreped | 11:dad1798ed88f | 63 | TilstandSjekk(i, tilstand); |
| andreped | 6:a3b97bf7902b | 64 | } |
| andreped | 6:a3b97bf7902b | 65 | if((Soner[i] > 0.65 )&&( Soner[i] < 0.69)) { //Tilstand 2, Dør 2 åpen |
| andreped | 11:dad1798ed88f | 66 | //pc.printf("Vi er i tilstand2\n"); |
| andreped | 12:0193024eab13 | 67 | tilstand = 2; |
| andreped | 11:dad1798ed88f | 68 | TilstandSjekk(i, tilstand); |
| andreped | 6:a3b97bf7902b | 69 | } |
| andreped | 6:a3b97bf7902b | 70 | if(Soner[i] >= 1) { //Begge dører er åpne |
| andreped | 11:dad1798ed88f | 71 | //pc.printf("Vi er i tilstand 3, begge doorer er aapne"); |
| andreped | 11:dad1798ed88f | 72 | tilstand = 3; |
| andreped | 11:dad1798ed88f | 73 | TilstandSjekk(i, tilstand); |
| andreped | 6:a3b97bf7902b | 74 | } |
| andreped | 11:dad1798ed88f | 75 | //wait(1.5); |
| andreped | 1:6917ec4f5e6d | 76 | } |
| andreped | 6:a3b97bf7902b | 77 | //pc.printf("%f\n", Sone1[]); |
| andreped | 6:a3b97bf7902b | 78 | //Tilstandssjekk(Sone1[]); |
| andreped | 6:a3b97bf7902b | 79 | //float volt = Sone1[]; |
| andreped | 6:a3b97bf7902b | 80 | //pc.printf("%f\n", volt); |
| Banyrud | 5:045ef3068f21 | 81 | //while(sysclock == 1); |
| Banyrud | 5:045ef3068f21 | 82 | //while(sysclock == 0); |
| andreped | 6:a3b97bf7902b | 83 | //Tid++; |
| andreped | 0:33bf28fd1ac6 | 84 | } |
| andreped | 0:33bf28fd1ac6 | 85 | } |
| andreped | 1:6917ec4f5e6d | 86 | |
| andreped | 11:dad1798ed88f | 87 | int TilstandSjekk(int i, int tilstand) |
| andreped | 11:dad1798ed88f | 88 | { |
| andreped | 11:dad1798ed88f | 89 | if(tilstand == 0) { |
| andreped | 11:dad1798ed88f | 90 | //pc.printf("Vi er i funksjonen normaldrift i sone: %d \n", i); |
| andreped | 12:0193024eab13 | 91 | //AlarmBlink.detach(); |
| andreped | 12:0193024eab13 | 92 | Alarm.stop(); |
| andreped | 11:dad1798ed88f | 93 | double n = 2; // We want 2^i |
| andreped | 11:dad1798ed88f | 94 | double Y = 1; |
| andreped | 11:dad1798ed88f | 95 | for(int k=1; k<=i; k++) Y = Y*n; |
| andreped | 12:0193024eab13 | 96 | if ( Y == AlarmUtganger1) AlarmUtganger1 = AlarmUtganger1 - Y; |
| andreped | 12:0193024eab13 | 97 | if ( Y == AlarmUtganger2) AlarmUtganger2 = AlarmUtganger2 - Y; |
| andreped | 12:0193024eab13 | 98 | int Utganger1 = AlarmUtganger1; |
| andreped | 12:0193024eab13 | 99 | int Utganger2 = AlarmUtganger2; |
| andreped | 11:dad1798ed88f | 100 | //pc.printf("Utgangsbuss1 har naa verdien: %d \n Utgangsbuss2 har naa verdien: %d", Utganger1, Utganger2); |
| andreped | 11:dad1798ed88f | 101 | } |
| andreped | 11:dad1798ed88f | 102 | if(tilstand == 1) { //Hvis dør 1 er åpen... |
| andreped | 11:dad1798ed88f | 103 | //pc.printf("Vi er i funksjonen tilstandssjekk dor 1, med verdi: %d\n", i); |
| andreped | 12:0193024eab13 | 104 | //Alarm(); |
| andreped | 12:0193024eab13 | 105 | Alarm.start(50); |
| andreped | 11:dad1798ed88f | 106 | double n = 2; // We want 2^i |
| andreped | 11:dad1798ed88f | 107 | double Y = 1; |
| andreped | 11:dad1798ed88f | 108 | for(int k=1; k<=i; k++) Y = Y*n; |
| andreped | 11:dad1798ed88f | 109 | if( Y == AlarmUtganger2 ) AlarmUtganger2 = AlarmUtganger2 - Y; |
| andreped | 11:dad1798ed88f | 110 | if( Y!=AlarmUtganger1) AlarmUtganger1 = Y + AlarmUtganger1; //Sjekk om denne verdien har blitt sjekket før, hvis ikke starter alarm på neste dør. |
| andreped | 11:dad1798ed88f | 111 | int Utganger1 = AlarmUtganger1; |
| andreped | 11:dad1798ed88f | 112 | //pc.printf("Vi har verdien paa utgangsbuss: %d\n", Utganger1); |
| andreped | 11:dad1798ed88f | 113 | } |
| andreped | 11:dad1798ed88f | 114 | if(tilstand == 2) { //Hvis dør 2 er åpen... |
| andreped | 11:dad1798ed88f | 115 | //pc.printf("Vi er i funksjonen tilstandssjekk dor 2, med verdi: %d\n", i); |
| andreped | 12:0193024eab13 | 116 | //Alarm(); |
| andreped | 12:0193024eab13 | 117 | Alarm.start(50); |
| andreped | 11:dad1798ed88f | 118 | double n = 2; // We want 2^i |
| andreped | 11:dad1798ed88f | 119 | double Y = 1; |
| andreped | 11:dad1798ed88f | 120 | for(int k=1; k<=i; k++) Y = Y*n; |
| andreped | 11:dad1798ed88f | 121 | if( Y == AlarmUtganger1 ) AlarmUtganger1 = AlarmUtganger1 - Y; |
| andreped | 11:dad1798ed88f | 122 | if( Y!=AlarmUtganger2) AlarmUtganger2 = Y + AlarmUtganger2; //Sjekk om denne verdien har blitt sjekket før, hvis ikke starter alarm på neste dør. |
| andreped | 11:dad1798ed88f | 123 | int Utganger2 = AlarmUtganger2; |
| andreped | 11:dad1798ed88f | 124 | //pc.printf("Vi har verdien paa utgangsbuss: %d\n", Utganger2); |
| andreped | 11:dad1798ed88f | 125 | } |
| andreped | 11:dad1798ed88f | 126 | if(tilstand == 3) { //Hvis begge dører er åpne |
| andreped | 12:0193024eab13 | 127 | //pc.printf("Vi er i funksjonen tilstandssjekk alle dorer, med verdi: %d \n", i);; |
| andreped | 12:0193024eab13 | 128 | // Alarm(); |
| andreped | 12:0193024eab13 | 129 | Alarm.start(50); |
| andreped | 11:dad1798ed88f | 130 | double n = 2; // We want 2^i |
| andreped | 11:dad1798ed88f | 131 | double Y = 1; |
| andreped | 11:dad1798ed88f | 132 | for(int k=1; k<=i; k++) Y = Y*n; |
| andreped | 11:dad1798ed88f | 133 | if( Y!=AlarmUtganger1 ) AlarmUtganger1 = Y + AlarmUtganger1; |
| andreped | 11:dad1798ed88f | 134 | if( Y!=AlarmUtganger2 ) AlarmUtganger2 = Y + AlarmUtganger2; |
| andreped | 11:dad1798ed88f | 135 | int Utganger2 = AlarmUtganger2; |
| andreped | 11:dad1798ed88f | 136 | int Utganger1 = AlarmUtganger2; |
| andreped | 11:dad1798ed88f | 137 | //pc.printf("Utgangsbuss1 har verdien: %d \n Utgangsbuss2 har verdien: %d \n", Utganger1, Utganger2); |
| andreped | 11:dad1798ed88f | 138 | } |
| andreped | 11:dad1798ed88f | 139 | return 0; |
| andreped | 10:f0a6d74095d4 | 140 | } |
| andreped | 1:6917ec4f5e6d | 141 | |
| andreped | 12:0193024eab13 | 142 | |
| andreped | 12:0193024eab13 | 143 | |
| andreped | 12:0193024eab13 | 144 | /*void Alarm() |
| andreped | 1:6917ec4f5e6d | 145 | { |
| andreped | 12:0193024eab13 | 146 | Driftslys = !Driftslys; |
| andreped | 12:0193024eab13 | 147 | wait(0.05); |
| andreped | 12:0193024eab13 | 148 | }*/ |
