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_rev_innlevering by
main.cpp@13:0622c228b67d, 2018-02-20 (annotated)
- Committer:
- Banyrud
- Date:
- Tue Feb 20 07:48:13 2018 +0000
- Revision:
- 13:0622c228b67d
- Parent:
- 12:0193024eab13
- Child:
- 14:8e7aaf0cf5d7
ryddet_rev
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 | 12:0193024eab13 | 5 | #include "mbed_error.h" |
andreped | 12:0193024eab13 | 6 | |
Banyrud | 13:0622c228b67d | 7 | /////DEFINISJONER///// |
andreped | 6:a3b97bf7902b | 8 | #define Inn 4 |
Banyrud | 13:0622c228b67d | 9 | Serial pc(USBTX, USBRX); |
andreped | 6:a3b97bf7902b | 10 | //#define Feil 0<1 |
andreped | 10:f0a6d74095d4 | 11 | |
Banyrud | 13:0622c228b67d | 12 | /////INNGANGER///// |
andreped | 0:33bf28fd1ac6 | 13 | DigitalIn Servicebryter(p20); |
andreped | 12:0193024eab13 | 14 | AnalogIn _Sone1(p15); |
andreped | 12:0193024eab13 | 15 | AnalogIn _Sone2(p16); |
andreped | 12:0193024eab13 | 16 | AnalogIn _Sone3(p17); |
andreped | 12:0193024eab13 | 17 | AnalogIn _Sone4(p19); |
Banyrud | 13:0622c228b67d | 18 | |
Banyrud | 13:0622c228b67d | 19 | /////UTGANGER///// |
Banyrud | 13:0622c228b67d | 20 | DigitalOut Driftslys(LED1); |
Banyrud | 13:0622c228b67d | 21 | 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 |
Banyrud | 13:0622c228b67d | 22 | 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 |
Banyrud | 13:0622c228b67d | 23 | //DigitalOut myled2(LED2); |
Banyrud | 13:0622c228b67d | 24 | |
Banyrud | 13:0622c228b67d | 25 | /////VARIABLER///// |
andreped | 12:0193024eab13 | 26 | float Soner[Inn]; |
andreped | 12:0193024eab13 | 27 | int tilstand = 0; |
andreped | 11:dad1798ed88f | 28 | int TilstandSjekk(int, int); |
andreped | 12:0193024eab13 | 29 | volatile bool running = true; |
Banyrud | 13:0622c228b67d | 30 | //volatile int Klokke = 0; |
andreped | 6:a3b97bf7902b | 31 | //nt Tid = 0; |
andreped | 1:6917ec4f5e6d | 32 | |
Banyrud | 13:0622c228b67d | 33 | /////FUNKSJON DEKLARASJONER///// |
Banyrud | 13:0622c228b67d | 34 | void Alarmf(); |
Banyrud | 13:0622c228b67d | 35 | Thread thread; |
andreped | 1:6917ec4f5e6d | 36 | int Tilstandssjekk(); |
andreped | 12:0193024eab13 | 37 | //void Alarm(); |
Banyrud | 13:0622c228b67d | 38 | //Ticker AlarmBlink; // 10Hz blinking for alarmfunksjon på lys og telling |
andreped | 1:6917ec4f5e6d | 39 | |
andreped | 12:0193024eab13 | 40 | |
Banyrud | 13:0622c228b67d | 41 | /////MAIN///// |
andreped | 1:6917ec4f5e6d | 42 | int main() |
andreped | 1:6917ec4f5e6d | 43 | { |
andreped | 1:6917ec4f5e6d | 44 | while(1) { |
andreped | 12:0193024eab13 | 45 | RtosTimer Alarm(Alarmf); |
andreped | 12:0193024eab13 | 46 | // AlarmBlink.attach(&Alarm, 1); |
andreped | 6:a3b97bf7902b | 47 | /*if(Servicebryter == 1) { |
andreped | 1:6917ec4f5e6d | 48 | Utganger1 = 0; |
andreped | 1:6917ec4f5e6d | 49 | Utganger2 = 0; |
andreped | 6:a3b97bf7902b | 50 | }*/ |
andreped | 6:a3b97bf7902b | 51 | Soner[0] = _Sone1; |
andreped | 6:a3b97bf7902b | 52 | //Soner[1] = _Sone2; |
andreped | 6:a3b97bf7902b | 53 | //Soner[2] = _Sone3; |
andreped | 6:a3b97bf7902b | 54 | //Soner[3] = _Sone4; |
andreped | 6:a3b97bf7902b | 55 | for(int i=0; i < Inn; i++) { |
andreped | 11:dad1798ed88f | 56 | //pc.printf("%f\n", Soner[i]); |
andreped | 6:a3b97bf7902b | 57 | if((Soner[i] > 0.75 )&&( Soner[i] < 0.83)) { //Tilstand1, Dør 1 åpen |
andreped | 11:dad1798ed88f | 58 | //pc.printf("Vi er i Tilstand1\n"); |
andreped | 11:dad1798ed88f | 59 | tilstand = 1; |
andreped | 11:dad1798ed88f | 60 | TilstandSjekk(i, tilstand); |
andreped | 6:a3b97bf7902b | 61 | } |
andreped | 6:a3b97bf7902b | 62 | if((Soner[i] > 0.55 )&&( Soner[i] < 0.59)) { //Normaldrift |
andreped | 12:0193024eab13 | 63 | //pc.printf("Vi er i Normaldrift\n") |
andreped | 11:dad1798ed88f | 64 | tilstand = 0; |
andreped | 11:dad1798ed88f | 65 | TilstandSjekk(i, tilstand); |
andreped | 6:a3b97bf7902b | 66 | } |
andreped | 6:a3b97bf7902b | 67 | if((Soner[i] > 0.65 )&&( Soner[i] < 0.69)) { //Tilstand 2, Dør 2 åpen |
andreped | 11:dad1798ed88f | 68 | //pc.printf("Vi er i tilstand2\n"); |
andreped | 12:0193024eab13 | 69 | tilstand = 2; |
andreped | 11:dad1798ed88f | 70 | TilstandSjekk(i, tilstand); |
andreped | 6:a3b97bf7902b | 71 | } |
andreped | 6:a3b97bf7902b | 72 | if(Soner[i] >= 1) { //Begge dører er åpne |
andreped | 11:dad1798ed88f | 73 | //pc.printf("Vi er i tilstand 3, begge doorer er aapne"); |
andreped | 11:dad1798ed88f | 74 | tilstand = 3; |
andreped | 11:dad1798ed88f | 75 | TilstandSjekk(i, tilstand); |
andreped | 6:a3b97bf7902b | 76 | } |
andreped | 11:dad1798ed88f | 77 | //wait(1.5); |
andreped | 1:6917ec4f5e6d | 78 | } |
andreped | 6:a3b97bf7902b | 79 | //pc.printf("%f\n", Sone1[]); |
andreped | 6:a3b97bf7902b | 80 | //Tilstandssjekk(Sone1[]); |
andreped | 6:a3b97bf7902b | 81 | //float volt = Sone1[]; |
andreped | 6:a3b97bf7902b | 82 | //pc.printf("%f\n", volt); |
Banyrud | 5:045ef3068f21 | 83 | //while(sysclock == 1); |
Banyrud | 5:045ef3068f21 | 84 | //while(sysclock == 0); |
andreped | 6:a3b97bf7902b | 85 | //Tid++; |
andreped | 0:33bf28fd1ac6 | 86 | } |
andreped | 0:33bf28fd1ac6 | 87 | } |
andreped | 1:6917ec4f5e6d | 88 | |
Banyrud | 13:0622c228b67d | 89 | /////FUNKSJONER///// |
Banyrud | 13:0622c228b67d | 90 | void Alarmf(void) { |
Banyrud | 13:0622c228b67d | 91 | Driftslys = !Driftslys; |
Banyrud | 13:0622c228b67d | 92 | } |
Banyrud | 13:0622c228b67d | 93 | |
andreped | 11:dad1798ed88f | 94 | int TilstandSjekk(int i, int tilstand) |
andreped | 11:dad1798ed88f | 95 | { |
andreped | 11:dad1798ed88f | 96 | if(tilstand == 0) { |
andreped | 11:dad1798ed88f | 97 | //pc.printf("Vi er i funksjonen normaldrift i sone: %d \n", i); |
andreped | 12:0193024eab13 | 98 | //AlarmBlink.detach(); |
andreped | 12:0193024eab13 | 99 | Alarm.stop(); |
andreped | 11:dad1798ed88f | 100 | double n = 2; // We want 2^i |
andreped | 11:dad1798ed88f | 101 | double Y = 1; |
andreped | 11:dad1798ed88f | 102 | for(int k=1; k<=i; k++) Y = Y*n; |
andreped | 12:0193024eab13 | 103 | if ( Y == AlarmUtganger1) AlarmUtganger1 = AlarmUtganger1 - Y; |
andreped | 12:0193024eab13 | 104 | if ( Y == AlarmUtganger2) AlarmUtganger2 = AlarmUtganger2 - Y; |
andreped | 12:0193024eab13 | 105 | int Utganger1 = AlarmUtganger1; |
andreped | 12:0193024eab13 | 106 | int Utganger2 = AlarmUtganger2; |
andreped | 11:dad1798ed88f | 107 | //pc.printf("Utgangsbuss1 har naa verdien: %d \n Utgangsbuss2 har naa verdien: %d", Utganger1, Utganger2); |
andreped | 11:dad1798ed88f | 108 | } |
andreped | 11:dad1798ed88f | 109 | if(tilstand == 1) { //Hvis dør 1 er åpen... |
andreped | 11:dad1798ed88f | 110 | //pc.printf("Vi er i funksjonen tilstandssjekk dor 1, med verdi: %d\n", i); |
andreped | 12:0193024eab13 | 111 | //Alarm(); |
andreped | 12:0193024eab13 | 112 | Alarm.start(50); |
andreped | 11:dad1798ed88f | 113 | double n = 2; // We want 2^i |
andreped | 11:dad1798ed88f | 114 | double Y = 1; |
andreped | 11:dad1798ed88f | 115 | for(int k=1; k<=i; k++) Y = Y*n; |
andreped | 11:dad1798ed88f | 116 | if( Y == AlarmUtganger2 ) AlarmUtganger2 = AlarmUtganger2 - Y; |
andreped | 11:dad1798ed88f | 117 | 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 | 118 | int Utganger1 = AlarmUtganger1; |
andreped | 11:dad1798ed88f | 119 | //pc.printf("Vi har verdien paa utgangsbuss: %d\n", Utganger1); |
andreped | 11:dad1798ed88f | 120 | } |
andreped | 11:dad1798ed88f | 121 | if(tilstand == 2) { //Hvis dør 2 er åpen... |
andreped | 11:dad1798ed88f | 122 | //pc.printf("Vi er i funksjonen tilstandssjekk dor 2, med verdi: %d\n", i); |
andreped | 12:0193024eab13 | 123 | //Alarm(); |
andreped | 12:0193024eab13 | 124 | Alarm.start(50); |
andreped | 11:dad1798ed88f | 125 | double n = 2; // We want 2^i |
andreped | 11:dad1798ed88f | 126 | double Y = 1; |
andreped | 11:dad1798ed88f | 127 | for(int k=1; k<=i; k++) Y = Y*n; |
andreped | 11:dad1798ed88f | 128 | if( Y == AlarmUtganger1 ) AlarmUtganger1 = AlarmUtganger1 - Y; |
andreped | 11:dad1798ed88f | 129 | 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 | 130 | int Utganger2 = AlarmUtganger2; |
andreped | 11:dad1798ed88f | 131 | //pc.printf("Vi har verdien paa utgangsbuss: %d\n", Utganger2); |
andreped | 11:dad1798ed88f | 132 | } |
andreped | 11:dad1798ed88f | 133 | if(tilstand == 3) { //Hvis begge dører er åpne |
andreped | 12:0193024eab13 | 134 | //pc.printf("Vi er i funksjonen tilstandssjekk alle dorer, med verdi: %d \n", i);; |
andreped | 12:0193024eab13 | 135 | // Alarm(); |
andreped | 12:0193024eab13 | 136 | Alarm.start(50); |
andreped | 11:dad1798ed88f | 137 | double n = 2; // We want 2^i |
andreped | 11:dad1798ed88f | 138 | double Y = 1; |
andreped | 11:dad1798ed88f | 139 | for(int k=1; k<=i; k++) Y = Y*n; |
andreped | 11:dad1798ed88f | 140 | if( Y!=AlarmUtganger1 ) AlarmUtganger1 = Y + AlarmUtganger1; |
andreped | 11:dad1798ed88f | 141 | if( Y!=AlarmUtganger2 ) AlarmUtganger2 = Y + AlarmUtganger2; |
andreped | 11:dad1798ed88f | 142 | int Utganger2 = AlarmUtganger2; |
andreped | 11:dad1798ed88f | 143 | int Utganger1 = AlarmUtganger2; |
andreped | 11:dad1798ed88f | 144 | //pc.printf("Utgangsbuss1 har verdien: %d \n Utgangsbuss2 har verdien: %d \n", Utganger1, Utganger2); |
andreped | 11:dad1798ed88f | 145 | } |
andreped | 11:dad1798ed88f | 146 | return 0; |
andreped | 10:f0a6d74095d4 | 147 | } |
andreped | 12:0193024eab13 | 148 | |
andreped | 12:0193024eab13 | 149 | /*void Alarm() |
andreped | 1:6917ec4f5e6d | 150 | { |
andreped | 12:0193024eab13 | 151 | Driftslys = !Driftslys; |
andreped | 12:0193024eab13 | 152 | wait(0.05); |
andreped | 12:0193024eab13 | 153 | }*/ |