This project is intended to release you the basic Automatic Sliding Gate. This project is based on the NUCLEO-L152RE.

Dependencies:   mbed

Committer:
emcu
Date:
Sat May 10 23:16:49 2014 +0000
Revision:
1:ab2cd6391528
Parent:
0:b05cd2add81b
Free use and responsibility

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emcu 0:b05cd2add81b 1 #include "mbed.h"
emcu 0:b05cd2add81b 2
emcu 1:ab2cd6391528 3 // By: www.emcu.it
emcu 1:ab2cd6391528 4 // For more detailed; schematics, explanations, etc, see:
emcu 1:ab2cd6391528 5 // http://www.emcu.it/NUCLEOevaBoards/Nucleo-L152RE-AutomaticSlidingGate/AutomSlidGate.html
emcu 1:ab2cd6391528 6
emcu 1:ab2cd6391528 7 /*
emcu 1:ab2cd6391528 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
emcu 1:ab2cd6391528 9 * of this software and associated documentation files (the "Software"), to deal
emcu 1:ab2cd6391528 10 * in the Software without restriction, including without limitation the rights
emcu 1:ab2cd6391528 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
emcu 1:ab2cd6391528 12 * copies of the Software, and to permit persons to whom the Software is
emcu 1:ab2cd6391528 13 * furnished to do so, subject to the following conditions:
emcu 1:ab2cd6391528 14 *
emcu 1:ab2cd6391528 15 * The above copyright notice and this permission notice shall be included in
emcu 1:ab2cd6391528 16 * all copies or substantial portions of the Software.
emcu 1:ab2cd6391528 17 *
emcu 1:ab2cd6391528 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
emcu 1:ab2cd6391528 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
emcu 1:ab2cd6391528 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
emcu 1:ab2cd6391528 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
emcu 1:ab2cd6391528 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
emcu 1:ab2cd6391528 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
emcu 1:ab2cd6391528 24 * THE SOFTWARE.
emcu 1:ab2cd6391528 25 */
emcu 1:ab2cd6391528 26
emcu 1:ab2cd6391528 27
emcu 0:b05cd2add81b 28 // Up to now the automatic closure start after 10sec. This because is not implemented the TbC.
emcu 0:b05cd2add81b 29 // Up to now the the following functions are not implemented:
emcu 0:b05cd2add81b 30 // AT (AntiTamper)
emcu 0:b05cd2add81b 31 // TbC
emcu 0:b05cd2add81b 32 // TempCurLight and consequently the ReleLight
emcu 0:b05cd2add81b 33 // LightSensor
emcu 0:b05cd2add81b 34 // SensCrep
emcu 0:b05cd2add81b 35 //
emcu 0:b05cd2add81b 36
emcu 0:b05cd2add81b 37 //------------------------------------
emcu 0:b05cd2add81b 38 // Hyperterminal configuration
emcu 0:b05cd2add81b 39 // 9600 bauds, 8-bit data, no parity
emcu 0:b05cd2add81b 40 //------------------------------------
emcu 0:b05cd2add81b 41
emcu 0:b05cd2add81b 42 Serial pc(SERIAL_TX, SERIAL_RX);
emcu 0:b05cd2add81b 43
emcu 0:b05cd2add81b 44 Timeout to1; // Interrupt use for flashing
emcu 0:b05cd2add81b 45 Timeout to2; // Interrupt use for flashing the GREEN led that is on NUCLEO-L152RE board
emcu 0:b05cd2add81b 46 Timeout to3; // Interrupt used for automatic reclosure
emcu 0:b05cd2add81b 47
emcu 0:b05cd2add81b 48
emcu 0:b05cd2add81b 49 // ADC Signals
emcu 0:b05cd2add81b 50 AnalogIn TempCurLight(PA_1); // Value from a potentiometer that sets the turn-on time of the courtesy light
emcu 0:b05cd2add81b 51 AnalogIn SensCrep(PA_0); // Value from a potentiometer that sets the threshold for day and night
emcu 0:b05cd2add81b 52 AnalogIn LightSensor(PA_4); // Crepuscular sensor
emcu 0:b05cd2add81b 53
emcu 0:b05cd2add81b 54 // Input Signals
emcu 0:b05cd2add81b 55 DigitalIn PP(PB_3);
emcu 0:b05cd2add81b 56 DigitalIn FOTO(PB_5);
emcu 0:b05cd2add81b 57 DigitalIn EMERGENCY(PB_4);
emcu 0:b05cd2add81b 58 DigitalIn FCA(PA_7);
emcu 0:b05cd2add81b 59 DigitalIn FCC(PA_6);
emcu 0:b05cd2add81b 60 DigitalIn RA(PA_10);
emcu 0:b05cd2add81b 61 DigitalIn BlueButton(USER_BUTTON); // This is Blue-Button and is on NUCLEO-L153RE
emcu 0:b05cd2add81b 62
emcu 0:b05cd2add81b 63 // OutPut Signals
emcu 0:b05cd2add81b 64 DigitalOut ReleLight(PB_10);
emcu 0:b05cd2add81b 65 DigitalOut M1Com(PA_8);
emcu 0:b05cd2add81b 66 DigitalOut M1Open(PA_9);
emcu 0:b05cd2add81b 67 DigitalOut M1Close(PC_7);
emcu 0:b05cd2add81b 68 DigitalOut Flashing(PB_6);
emcu 0:b05cd2add81b 69 DigitalOut myled(LED1); // This LED is on NUCLEO-L153RE
emcu 0:b05cd2add81b 70
emcu 0:b05cd2add81b 71 void SelfTest(void);
emcu 0:b05cd2add81b 72 void TestPP(void);
emcu 0:b05cd2add81b 73 void TestFCA(void);
emcu 0:b05cd2add81b 74 void TestFCC(void);
emcu 0:b05cd2add81b 75 void TestFOTO(void);
emcu 0:b05cd2add81b 76 void TestEMERGENCY(void);
emcu 0:b05cd2add81b 77 void TestRA(void);
emcu 0:b05cd2add81b 78 void cb1(void);
emcu 0:b05cd2add81b 79 void IntFlash(void);
emcu 0:b05cd2add81b 80 void IntMyled(void);
emcu 0:b05cd2add81b 81 void IntDLYforRA(void);
emcu 0:b05cd2add81b 82
emcu 0:b05cd2add81b 83 #define DLY 0.2 // Delay 200mS for Debounce
emcu 0:b05cd2add81b 84 #define DLYFlash 0.6 // DLY for Flashing
emcu 0:b05cd2add81b 85 #define DLYMyled 0.5
emcu 0:b05cd2add81b 86 #define DLYallarmFoto 0.2 // 0.2 == 200mS
emcu 0:b05cd2add81b 87 #define DLYbeforeToStart 1.5 // 1.5 == 1,5sec
emcu 0:b05cd2add81b 88 #define DLYforAutoReclosure 10 // 10 == 10sec
emcu 0:b05cd2add81b 89 #define Pressed 0
emcu 0:b05cd2add81b 90 #define NotPressed 1
emcu 0:b05cd2add81b 91 #define OK 0
emcu 0:b05cd2add81b 92 #define NotOK 1
emcu 0:b05cd2add81b 93 #define FAIL 0
emcu 0:b05cd2add81b 94 #define NotFAIL 1
emcu 0:b05cd2add81b 95 #define YES 1
emcu 0:b05cd2add81b 96 #define NoYES 0
emcu 0:b05cd2add81b 97
emcu 0:b05cd2add81b 98 #define OFF 0
emcu 0:b05cd2add81b 99 #define ON 1
emcu 0:b05cd2add81b 100 #define Opening 0
emcu 0:b05cd2add81b 101 #define Closing 1
emcu 0:b05cd2add81b 102 #define Undefine 3
emcu 0:b05cd2add81b 103
emcu 0:b05cd2add81b 104 int PP_mem = Pressed; // Last value of PP
emcu 0:b05cd2add81b 105 int EMERGENCY_mem = Pressed;
emcu 0:b05cd2add81b 106 int FOTO_mem = OK;
emcu 0:b05cd2add81b 107 int FCA_mem = Pressed;
emcu 0:b05cd2add81b 108 int FCC_mem = Pressed;
emcu 0:b05cd2add81b 109 int RA_mem = ON;
emcu 0:b05cd2add81b 110 uint16_t meas = 0;
emcu 0:b05cd2add81b 111 uint16_t meas1 = 0;
emcu 0:b05cd2add81b 112 uint16_t meas2 = 0;
emcu 0:b05cd2add81b 113 int ONOFF_Flashing = OFF;
emcu 0:b05cd2add81b 114 int LastMovement = Undefine;
emcu 0:b05cd2add81b 115 int RA_OnOff = OFF;
emcu 0:b05cd2add81b 116 int ChiudiBy_IntDLYforRA = OFF;
emcu 0:b05cd2add81b 117
emcu 0:b05cd2add81b 118
emcu 0:b05cd2add81b 119
emcu 0:b05cd2add81b 120 int main()
emcu 0:b05cd2add81b 121 {
emcu 0:b05cd2add81b 122
emcu 0:b05cd2add81b 123 // Turn on the InPut PullUp resistors
emcu 0:b05cd2add81b 124 // For more immunity connect a 4K7 PullUp resistor to the InPut pins.
emcu 0:b05cd2add81b 125 PP.mode(PullUp);
emcu 0:b05cd2add81b 126 FOTO.mode(PullUp);
emcu 0:b05cd2add81b 127 EMERGENCY.mode(PullUp);
emcu 0:b05cd2add81b 128 FCA.mode(PullUp);
emcu 0:b05cd2add81b 129 FCC.mode(PullUp);
emcu 0:b05cd2add81b 130 RA.mode(PullUp);
emcu 0:b05cd2add81b 131
emcu 0:b05cd2add81b 132 to2.attach(&IntMyled, DLYMyled); // Enabling flassing GREEN led that is on NUCLEO-L152RE board
emcu 0:b05cd2add81b 133 // Interrupt Delay
emcu 0:b05cd2add81b 134 ONOFF_Flashing = OFF;
emcu 0:b05cd2add81b 135
emcu 0:b05cd2add81b 136 to1.attach(&IntFlash, DLYFlash); // Enabling flassing of FLASH lamp that indicate: GATE movement
emcu 0:b05cd2add81b 137 Flashing = 1; // Means Flashing Lamp OFF
emcu 0:b05cd2add81b 138
emcu 0:b05cd2add81b 139 // to2.attach(&IntDLYforRA, DLYforAutoReclosure); // Enabling time for auto reclosure
emcu 0:b05cd2add81b 140
emcu 0:b05cd2add81b 141
emcu 0:b05cd2add81b 142
emcu 0:b05cd2add81b 143 // If at PowerOn BlueButton is pressed is call the SelfTest routine
emcu 0:b05cd2add81b 144 if (BlueButton == Pressed)
emcu 0:b05cd2add81b 145 SelfTest();
emcu 0:b05cd2add81b 146
emcu 0:b05cd2add81b 147 // MAIN Loop
emcu 0:b05cd2add81b 148 pc.printf("\n\r\n\rMAIN loop\n\r");
emcu 0:b05cd2add81b 149
emcu 0:b05cd2add81b 150 while(1)
emcu 0:b05cd2add81b 151 {
emcu 0:b05cd2add81b 152 TestFOTO();
emcu 0:b05cd2add81b 153 if (FOTO_mem == OK)
emcu 0:b05cd2add81b 154 ONOFF_Flashing = OFF;
emcu 0:b05cd2add81b 155
emcu 0:b05cd2add81b 156 meas = TempCurLight.read_u16(); // Value from a potentiometer that sets the turn-on time of the courtesy light
emcu 0:b05cd2add81b 157 meas1 = SensCrep.read_u16(); // Value from a potentiometer that sets the threshold for day and night
emcu 0:b05cd2add81b 158 meas2 = LightSensor.read_u16(); // Crepuscular sensor
emcu 0:b05cd2add81b 159 // pc.printf("TempCurLight is:%d - SensCrep is:%d - LightSensor is:%d\n", meas, meas1, meas2);
emcu 0:b05cd2add81b 160
emcu 0:b05cd2add81b 161
emcu 0:b05cd2add81b 162 TestRA(); // Test the status of RA -> Automatic Reclosure
emcu 0:b05cd2add81b 163 // RA == 1 == Automatic Reclosure
emcu 0:b05cd2add81b 164 // RA == 0 == NO automatic reclosure
emcu 0:b05cd2add81b 165 // The delay before to close depend of the value of TbC
emcu 0:b05cd2add81b 166
emcu 0:b05cd2add81b 167
emcu 0:b05cd2add81b 168 // Test if there is the request for Auto Reclosure
emcu 0:b05cd2add81b 169 if (ChiudiBy_IntDLYforRA == ON)
emcu 0:b05cd2add81b 170 {
emcu 0:b05cd2add81b 171 TestPP();
emcu 0:b05cd2add81b 172 TestFCC();
emcu 0:b05cd2add81b 173 TestFCA();
emcu 0:b05cd2add81b 174 TestFOTO();
emcu 0:b05cd2add81b 175 TestEMERGENCY();
emcu 0:b05cd2add81b 176 ChiudiBy_IntDLYforRA = OFF; // Disable the Auto Reclosure
emcu 0:b05cd2add81b 177 if (FCA_mem == Pressed & FOTO_mem == OK & EMERGENCY_mem == NotPressed)
emcu 0:b05cd2add81b 178 {
emcu 0:b05cd2add81b 179 pc.printf("Start to CLOSE\n\r");
emcu 0:b05cd2add81b 180 LastMovement = Closing; // Memorizing the current CLOSING movement
emcu 0:b05cd2add81b 181 M1Com = ON;
emcu 0:b05cd2add81b 182 wait(0.1);
emcu 0:b05cd2add81b 183 M1Close = ON;
emcu 0:b05cd2add81b 184 wait(0.1);
emcu 0:b05cd2add81b 185 // Waiting FCC or PP Again)
emcu 0:b05cd2add81b 186 TestPP(); // Called for clean the buffer
emcu 0:b05cd2add81b 187 TestFOTO();
emcu 0:b05cd2add81b 188 while (FCC_mem == NotPressed & PP_mem == NotPressed & FOTO_mem == OK & EMERGENCY_mem == NotPressed)
emcu 0:b05cd2add81b 189 {
emcu 0:b05cd2add81b 190 TestEMERGENCY();
emcu 0:b05cd2add81b 191 TestFOTO();
emcu 0:b05cd2add81b 192 TestFCC();
emcu 0:b05cd2add81b 193 TestPP();
emcu 0:b05cd2add81b 194 }
emcu 0:b05cd2add81b 195 M1Com = OFF;
emcu 0:b05cd2add81b 196 wait(0.1);
emcu 0:b05cd2add81b 197 M1Close = OFF;
emcu 0:b05cd2add81b 198 wait(0.1);
emcu 0:b05cd2add81b 199 ONOFF_Flashing = OFF;
emcu 0:b05cd2add81b 200 if (FOTO_mem == NotOK)
emcu 0:b05cd2add81b 201 pc.printf("END CLOSE - STOP by FOTO ***\n\r");
emcu 0:b05cd2add81b 202 else if (EMERGENCY_mem == Pressed)
emcu 0:b05cd2add81b 203 pc.printf("END CLOSE - STOP by EMERGENCY ***\n\r");
emcu 0:b05cd2add81b 204 else
emcu 0:b05cd2add81b 205 pc.printf("END CLOSE\n\r");
emcu 0:b05cd2add81b 206 wait(1);
emcu 0:b05cd2add81b 207 }
emcu 0:b05cd2add81b 208 }
emcu 0:b05cd2add81b 209
emcu 0:b05cd2add81b 210
emcu 0:b05cd2add81b 211 // Test the status of PP
emcu 0:b05cd2add81b 212 TestPP();
emcu 0:b05cd2add81b 213 if (PP_mem == Pressed)
emcu 0:b05cd2add81b 214 {
emcu 0:b05cd2add81b 215 ONOFF_Flashing = ON;
emcu 0:b05cd2add81b 216 to1.attach(&IntFlash, DLYFlash); // Enabling flassing of FLASH lamp that indicate: GATE movement
emcu 0:b05cd2add81b 217 // Waiting the release of PP
emcu 0:b05cd2add81b 218 while(PP_mem == Pressed)
emcu 0:b05cd2add81b 219 TestPP();
emcu 0:b05cd2add81b 220 // Delay before start the gate
emcu 0:b05cd2add81b 221 wait(DLYbeforeToStart);
emcu 0:b05cd2add81b 222
emcu 0:b05cd2add81b 223 // Test the status of FCC, FCA & FOTO
emcu 0:b05cd2add81b 224 TestFCA();
emcu 0:b05cd2add81b 225 TestFCC();
emcu 0:b05cd2add81b 226 TestFOTO();
emcu 0:b05cd2add81b 227 TestEMERGENCY();
emcu 0:b05cd2add81b 228 TestRA();
emcu 0:b05cd2add81b 229
emcu 0:b05cd2add81b 230 pc.printf("FCA==%d - FCC==%d - FOTO==%d - EMERGENCY==%d - RA==%d\n\r", FCA_mem, FCC_mem, FOTO_mem, EMERGENCY_mem, RA_mem );
emcu 0:b05cd2add81b 231
emcu 0:b05cd2add81b 232 // Check if must be OPEN
emcu 0:b05cd2add81b 233 if (FCC_mem == Pressed & FOTO_mem == OK & EMERGENCY_mem == NotPressed)
emcu 0:b05cd2add81b 234 {
emcu 0:b05cd2add81b 235 pc.printf("Start to OPEN\n\r");
emcu 0:b05cd2add81b 236 LastMovement = Opening; // Memorizing the current OPENING movement
emcu 0:b05cd2add81b 237 M1Com = ON;
emcu 0:b05cd2add81b 238 wait(0.1);
emcu 0:b05cd2add81b 239 M1Open = ON;
emcu 0:b05cd2add81b 240 wait(0.1);
emcu 0:b05cd2add81b 241 // Waiting FCA or PP Again)
emcu 0:b05cd2add81b 242 TestPP(); // Called for clean the buffer
emcu 0:b05cd2add81b 243 while (FCA_mem == NotPressed & PP_mem == NotPressed & EMERGENCY_mem == NotPressed)
emcu 0:b05cd2add81b 244 {
emcu 0:b05cd2add81b 245 TestEMERGENCY();
emcu 0:b05cd2add81b 246 TestFCA();
emcu 0:b05cd2add81b 247 TestPP();
emcu 0:b05cd2add81b 248 }
emcu 0:b05cd2add81b 249 M1Com = OFF;
emcu 0:b05cd2add81b 250 wait(0.1);
emcu 0:b05cd2add81b 251 M1Open = OFF;
emcu 0:b05cd2add81b 252 wait(0.1);
emcu 0:b05cd2add81b 253 ONOFF_Flashing = OFF;
emcu 0:b05cd2add81b 254 if (EMERGENCY_mem == Pressed)
emcu 0:b05cd2add81b 255 pc.printf("END OPEN - STOP by EMERGENCY ***\n\r");
emcu 0:b05cd2add81b 256 else if (PP_mem == Pressed)
emcu 0:b05cd2add81b 257 pc.printf("END OPEN - STOP by PP ***\n\r");
emcu 0:b05cd2add81b 258 else
emcu 0:b05cd2add81b 259 {
emcu 0:b05cd2add81b 260 pc.printf("END OPEN\n\r");
emcu 0:b05cd2add81b 261 TestRA();
emcu 0:b05cd2add81b 262 if (RA_mem == ON)
emcu 0:b05cd2add81b 263 {
emcu 0:b05cd2add81b 264 RA_OnOff = ON;
emcu 0:b05cd2add81b 265 to3.attach(&IntDLYforRA, DLYforAutoReclosure); // Enabling time for auto reclosure
emcu 0:b05cd2add81b 266 }
emcu 0:b05cd2add81b 267 }
emcu 0:b05cd2add81b 268 wait(1);
emcu 0:b05cd2add81b 269 }
emcu 0:b05cd2add81b 270
emcu 0:b05cd2add81b 271 // Check if must be CLOSE
emcu 0:b05cd2add81b 272 else if (FCA_mem == Pressed & FOTO_mem == OK & EMERGENCY_mem == NotPressed)
emcu 0:b05cd2add81b 273 {
emcu 0:b05cd2add81b 274 pc.printf("Start to CLOSE\n\r");
emcu 0:b05cd2add81b 275 LastMovement = Closing; // Memorizing the current CLOSING movement
emcu 0:b05cd2add81b 276 M1Com = ON;
emcu 0:b05cd2add81b 277 wait(0.1);
emcu 0:b05cd2add81b 278 M1Close = ON;
emcu 0:b05cd2add81b 279 wait(0.1);
emcu 0:b05cd2add81b 280 // Waiting FCC or PP Again)
emcu 0:b05cd2add81b 281 TestPP(); // Called for clean the buffer
emcu 0:b05cd2add81b 282 TestFOTO();
emcu 0:b05cd2add81b 283 while (FCC_mem == NotPressed & PP_mem == NotPressed & FOTO_mem == OK & EMERGENCY_mem == NotPressed)
emcu 0:b05cd2add81b 284 {
emcu 0:b05cd2add81b 285 TestEMERGENCY();
emcu 0:b05cd2add81b 286 TestFOTO();
emcu 0:b05cd2add81b 287 TestFCC();
emcu 0:b05cd2add81b 288 TestPP();
emcu 0:b05cd2add81b 289 }
emcu 0:b05cd2add81b 290 M1Com = OFF;
emcu 0:b05cd2add81b 291 wait(0.1);
emcu 0:b05cd2add81b 292 M1Close = OFF;
emcu 0:b05cd2add81b 293 wait(0.1);
emcu 0:b05cd2add81b 294 ONOFF_Flashing = OFF;
emcu 0:b05cd2add81b 295 if (FOTO_mem == NotOK)
emcu 0:b05cd2add81b 296 pc.printf("END CLOSE - STOP by FOTO ***\n\r");
emcu 0:b05cd2add81b 297 else if (EMERGENCY_mem == Pressed)
emcu 0:b05cd2add81b 298 pc.printf("END CLOSE - STOP by EMERGENCY ***\n\r");
emcu 0:b05cd2add81b 299 else
emcu 0:b05cd2add81b 300 pc.printf("END CLOSE\n\r");
emcu 0:b05cd2add81b 301 wait(1);
emcu 0:b05cd2add81b 302 }
emcu 0:b05cd2add81b 303
emcu 0:b05cd2add81b 304 else if ( (LastMovement == Closing | LastMovement == Undefine) & (FOTO_mem == OK & EMERGENCY_mem == NotPressed))
emcu 0:b05cd2add81b 305 {
emcu 0:b05cd2add81b 306 pc.printf("Start to OPEN\n\r");
emcu 0:b05cd2add81b 307 LastMovement = Opening; // Memorizing the current OPENING movement
emcu 0:b05cd2add81b 308 M1Com = ON;
emcu 0:b05cd2add81b 309 wait(0.1);
emcu 0:b05cd2add81b 310 M1Open = ON;
emcu 0:b05cd2add81b 311 wait(0.1);
emcu 0:b05cd2add81b 312 // Waiting FCA or PP Again)
emcu 0:b05cd2add81b 313 TestPP(); // Called for clean the buffer
emcu 0:b05cd2add81b 314 while (FCA_mem == NotPressed & PP_mem == NotPressed & EMERGENCY_mem == NotPressed)
emcu 0:b05cd2add81b 315 {
emcu 0:b05cd2add81b 316 TestEMERGENCY();
emcu 0:b05cd2add81b 317 TestFCA();
emcu 0:b05cd2add81b 318 TestPP();
emcu 0:b05cd2add81b 319 }
emcu 0:b05cd2add81b 320 M1Com = OFF;
emcu 0:b05cd2add81b 321 wait(0.1);
emcu 0:b05cd2add81b 322 M1Open = OFF;
emcu 0:b05cd2add81b 323 wait(0.1);
emcu 0:b05cd2add81b 324 ONOFF_Flashing = OFF;
emcu 0:b05cd2add81b 325 if (EMERGENCY_mem == Pressed)
emcu 0:b05cd2add81b 326 pc.printf("END OPEN - STOP by EMERGENCY ***\n\r");
emcu 0:b05cd2add81b 327 else if (PP_mem == Pressed)
emcu 0:b05cd2add81b 328 pc.printf("END OPEN - STOP by PP ***\n\r");
emcu 0:b05cd2add81b 329 else
emcu 0:b05cd2add81b 330 {
emcu 0:b05cd2add81b 331 pc.printf("END OPEN\n\r");
emcu 0:b05cd2add81b 332 TestRA();
emcu 0:b05cd2add81b 333 if (RA_mem == ON)
emcu 0:b05cd2add81b 334 {
emcu 0:b05cd2add81b 335 RA_OnOff = ON;
emcu 0:b05cd2add81b 336 to3.attach(&IntDLYforRA, DLYforAutoReclosure); // Enabling time for auto reclosure
emcu 0:b05cd2add81b 337 }
emcu 0:b05cd2add81b 338 }
emcu 0:b05cd2add81b 339 wait(1);
emcu 0:b05cd2add81b 340 }
emcu 0:b05cd2add81b 341
emcu 0:b05cd2add81b 342 else if ( LastMovement == Opening & FOTO_mem == OK & EMERGENCY_mem == NotPressed)
emcu 0:b05cd2add81b 343 {
emcu 0:b05cd2add81b 344 pc.printf("Start to CLOSE\n\r");
emcu 0:b05cd2add81b 345 LastMovement = Closing; // Memorizing the current CLOSING movement
emcu 0:b05cd2add81b 346 M1Com = ON;
emcu 0:b05cd2add81b 347 wait(0.1);
emcu 0:b05cd2add81b 348 M1Close = ON;
emcu 0:b05cd2add81b 349 wait(0.1);
emcu 0:b05cd2add81b 350 // Waiting FCC or PP Again)
emcu 0:b05cd2add81b 351 TestPP(); // Called for clean the buffer
emcu 0:b05cd2add81b 352 TestFOTO();
emcu 0:b05cd2add81b 353 while (FCC_mem == NotPressed & PP_mem == NotPressed & FOTO_mem == OK & EMERGENCY_mem == NotPressed)
emcu 0:b05cd2add81b 354 {
emcu 0:b05cd2add81b 355 TestEMERGENCY();
emcu 0:b05cd2add81b 356 TestFOTO();
emcu 0:b05cd2add81b 357 TestFCC();
emcu 0:b05cd2add81b 358 TestPP();
emcu 0:b05cd2add81b 359 }
emcu 0:b05cd2add81b 360 M1Com = OFF;
emcu 0:b05cd2add81b 361 wait(0.1);
emcu 0:b05cd2add81b 362 M1Close = OFF;
emcu 0:b05cd2add81b 363 wait(0.1);
emcu 0:b05cd2add81b 364 ONOFF_Flashing = OFF;
emcu 0:b05cd2add81b 365 if (EMERGENCY_mem == Pressed)
emcu 0:b05cd2add81b 366 pc.printf("END CLOSE - STOP by EMERGENCY ***\n\r");
emcu 0:b05cd2add81b 367 else
emcu 0:b05cd2add81b 368 pc.printf("END CLOSE\n\r");
emcu 0:b05cd2add81b 369 wait(1);
emcu 0:b05cd2add81b 370 }
emcu 0:b05cd2add81b 371
emcu 0:b05cd2add81b 372 }
emcu 0:b05cd2add81b 373 }
emcu 0:b05cd2add81b 374 }
emcu 0:b05cd2add81b 375
emcu 0:b05cd2add81b 376
emcu 0:b05cd2add81b 377
emcu 0:b05cd2add81b 378 // **************************** FUNCTIONS **************************************
emcu 0:b05cd2add81b 379
emcu 0:b05cd2add81b 380 // Flshing period for gate lamp indicator - It is define in DLYFlash
emcu 0:b05cd2add81b 381 void IntFlash(void) {
emcu 0:b05cd2add81b 382 if (ONOFF_Flashing == ON)
emcu 0:b05cd2add81b 383 Flashing = !Flashing;
emcu 0:b05cd2add81b 384 else
emcu 0:b05cd2add81b 385 Flashing = 1; // Means Flashing Lamp OFF
emcu 0:b05cd2add81b 386 to1.detach();
emcu 0:b05cd2add81b 387 if (FOTO_mem == NotOK)
emcu 0:b05cd2add81b 388 to1.attach(&IntFlash, DLYallarmFoto); // Highlight a problem on the FOTO (photocell) - this line reload Interrupt
emcu 0:b05cd2add81b 389 else
emcu 0:b05cd2add81b 390 to1.attach(&IntFlash, DLYFlash); // this line reload Interrupt
emcu 0:b05cd2add81b 391 }
emcu 0:b05cd2add81b 392
emcu 0:b05cd2add81b 393
emcu 0:b05cd2add81b 394 // Flashing Green LED that is on NUCLEO board
emcu 0:b05cd2add81b 395 void IntMyled(void) {
emcu 0:b05cd2add81b 396 myled = !myled;
emcu 0:b05cd2add81b 397 to2.detach();
emcu 0:b05cd2add81b 398 to2.attach(&IntMyled, DLYMyled); // this line reload Interrupt
emcu 0:b05cd2add81b 399 }
emcu 0:b05cd2add81b 400
emcu 0:b05cd2add81b 401
emcu 0:b05cd2add81b 402 // Time must be wait before do the automatic reclosure
emcu 0:b05cd2add81b 403 void IntDLYforRA(void) {
emcu 0:b05cd2add81b 404 if (RA_OnOff == ON)
emcu 0:b05cd2add81b 405 {
emcu 0:b05cd2add81b 406 pc.printf("Adesso deve partire la richiusura automatica\n\r");
emcu 0:b05cd2add81b 407 ChiudiBy_IntDLYforRA = ON;
emcu 0:b05cd2add81b 408 }
emcu 0:b05cd2add81b 409 to3.detach();
emcu 0:b05cd2add81b 410 // to2.attach(&IntDLYforRA, DLYforAutoReclosure); // this line reload Interrupt
emcu 0:b05cd2add81b 411 }
emcu 0:b05cd2add81b 412
emcu 0:b05cd2add81b 413 // Test the status of FOTO and save it in FOTO_mem
emcu 0:b05cd2add81b 414 void TestFOTO(void)
emcu 0:b05cd2add81b 415 {
emcu 0:b05cd2add81b 416 if (FOTO == 0 & FOTO_mem == NotOK)
emcu 0:b05cd2add81b 417 { // FOTO is pressed, we do the delay for Debounce
emcu 0:b05cd2add81b 418 wait(DLY);
emcu 0:b05cd2add81b 419 if (FOTO == 0)
emcu 0:b05cd2add81b 420 {
emcu 0:b05cd2add81b 421 FOTO_mem = OK;
emcu 0:b05cd2add81b 422 pc.printf("FOTO is OK (0)\n\r");
emcu 0:b05cd2add81b 423 }
emcu 0:b05cd2add81b 424 else
emcu 0:b05cd2add81b 425 FOTO_mem = NotOK;
emcu 0:b05cd2add81b 426 }
emcu 0:b05cd2add81b 427 if (FOTO == 1)
emcu 0:b05cd2add81b 428 FOTO_mem = NotOK;
emcu 0:b05cd2add81b 429 }
emcu 0:b05cd2add81b 430
emcu 0:b05cd2add81b 431
emcu 0:b05cd2add81b 432 // Test the status of EMERGENCY and save it in EMERGENCY_mem
emcu 0:b05cd2add81b 433 void TestEMERGENCY(void)
emcu 0:b05cd2add81b 434 {
emcu 0:b05cd2add81b 435 if (EMERGENCY == 0 & EMERGENCY_mem == NotPressed)
emcu 0:b05cd2add81b 436 { // EMERGENCY is pressed, we do the delay for Debounce
emcu 0:b05cd2add81b 437 wait(DLY);
emcu 0:b05cd2add81b 438 if (EMERGENCY == 0)
emcu 0:b05cd2add81b 439 {
emcu 0:b05cd2add81b 440 EMERGENCY_mem = Pressed;
emcu 0:b05cd2add81b 441 pc.printf("EMERGENCY is PRESSED (0)\n\r");
emcu 0:b05cd2add81b 442 }
emcu 0:b05cd2add81b 443 else
emcu 0:b05cd2add81b 444 EMERGENCY_mem = NotPressed;
emcu 0:b05cd2add81b 445 }
emcu 0:b05cd2add81b 446 if (EMERGENCY == 1)
emcu 0:b05cd2add81b 447 EMERGENCY_mem = NotPressed;
emcu 0:b05cd2add81b 448 }
emcu 0:b05cd2add81b 449
emcu 0:b05cd2add81b 450
emcu 0:b05cd2add81b 451 // Test the status of RA and save it in RA_mem
emcu 0:b05cd2add81b 452 void TestRA(void)
emcu 0:b05cd2add81b 453 {
emcu 0:b05cd2add81b 454 if (RA == 0 & RA_mem == ON)
emcu 0:b05cd2add81b 455 { // RA is OFF (0), we do the delay for Debounce
emcu 0:b05cd2add81b 456 wait(DLY);
emcu 0:b05cd2add81b 457 if (RA == 0)
emcu 0:b05cd2add81b 458 {
emcu 0:b05cd2add81b 459 RA_mem = OFF;
emcu 0:b05cd2add81b 460 pc.printf("RA is OFF (0)\n\r");
emcu 0:b05cd2add81b 461 }
emcu 0:b05cd2add81b 462 else
emcu 0:b05cd2add81b 463 RA_mem = ON;
emcu 0:b05cd2add81b 464 }
emcu 0:b05cd2add81b 465 if (RA == 1)
emcu 0:b05cd2add81b 466 RA_mem = ON;
emcu 0:b05cd2add81b 467 }
emcu 0:b05cd2add81b 468
emcu 0:b05cd2add81b 469
emcu 0:b05cd2add81b 470 // Test the status of FCC and save it in FCC_mem
emcu 0:b05cd2add81b 471 void TestFCC(void)
emcu 0:b05cd2add81b 472 {
emcu 0:b05cd2add81b 473 if (FCC == 0 & FCC_mem == NotPressed)
emcu 0:b05cd2add81b 474 { // FCC is pressed, we do the delay for Debounce
emcu 0:b05cd2add81b 475 wait(DLY);
emcu 0:b05cd2add81b 476 if (FCC == 0)
emcu 0:b05cd2add81b 477 {
emcu 0:b05cd2add81b 478 FCC_mem = Pressed;
emcu 0:b05cd2add81b 479 pc.printf("FCC is PRESSED (0)\n\r");
emcu 0:b05cd2add81b 480 }
emcu 0:b05cd2add81b 481 else
emcu 0:b05cd2add81b 482 FCC_mem = NotPressed;
emcu 0:b05cd2add81b 483 }
emcu 0:b05cd2add81b 484 if (FCC == 1)
emcu 0:b05cd2add81b 485 FCC_mem = NotPressed;
emcu 0:b05cd2add81b 486 }
emcu 0:b05cd2add81b 487
emcu 0:b05cd2add81b 488
emcu 0:b05cd2add81b 489 // Test the status of FCA and save it in FCA_mem
emcu 0:b05cd2add81b 490 void TestFCA(void)
emcu 0:b05cd2add81b 491 {
emcu 0:b05cd2add81b 492 if (FCA == 0 & FCA_mem == NotPressed)
emcu 0:b05cd2add81b 493 { // FCA is pressed, we do the delay for Debounce
emcu 0:b05cd2add81b 494 wait(DLY);
emcu 0:b05cd2add81b 495 if (FCA == 0)
emcu 0:b05cd2add81b 496 {
emcu 0:b05cd2add81b 497 FCA_mem = Pressed;
emcu 0:b05cd2add81b 498 pc.printf("FCA is PRESSED (0)\n\r");
emcu 0:b05cd2add81b 499 }
emcu 0:b05cd2add81b 500 else
emcu 0:b05cd2add81b 501 FCA_mem = NotPressed;
emcu 0:b05cd2add81b 502 }
emcu 0:b05cd2add81b 503 if (FCA == 1)
emcu 0:b05cd2add81b 504 FCA_mem = NotPressed;
emcu 0:b05cd2add81b 505 }
emcu 0:b05cd2add81b 506
emcu 0:b05cd2add81b 507
emcu 0:b05cd2add81b 508 // Test the status of PP and save it in PP_mem
emcu 0:b05cd2add81b 509 void TestPP(void)
emcu 0:b05cd2add81b 510 {
emcu 0:b05cd2add81b 511 // Test PP
emcu 0:b05cd2add81b 512 if (PP == 0 & PP_mem == NotPressed)
emcu 0:b05cd2add81b 513 { // PP is pressed, we do the delay for Debounce
emcu 0:b05cd2add81b 514 wait(DLY);
emcu 0:b05cd2add81b 515 if (PP == 0)
emcu 0:b05cd2add81b 516 {
emcu 0:b05cd2add81b 517 PP_mem = Pressed;
emcu 0:b05cd2add81b 518 pc.printf("PP is PRESSED (0)\n\r");
emcu 0:b05cd2add81b 519 }
emcu 0:b05cd2add81b 520 else
emcu 0:b05cd2add81b 521 PP_mem = NotPressed;
emcu 0:b05cd2add81b 522 }
emcu 0:b05cd2add81b 523 if (PP == 1)
emcu 0:b05cd2add81b 524 PP_mem = NotPressed;
emcu 0:b05cd2add81b 525 }
emcu 0:b05cd2add81b 526
emcu 0:b05cd2add81b 527
emcu 0:b05cd2add81b 528
emcu 0:b05cd2add81b 529 // Function for testing I/O and ADC used in the program ***********************
emcu 0:b05cd2add81b 530 void SelfTest(void)
emcu 0:b05cd2add81b 531 {
emcu 0:b05cd2add81b 532 pc.printf("\n\rProgram for Test CancSCO I/O and ADC\n\r");
emcu 0:b05cd2add81b 533 pc.printf("ADC test, press PP for exit from ADC test\n\r");
emcu 0:b05cd2add81b 534
emcu 0:b05cd2add81b 535 while(PP == NotPressed)
emcu 0:b05cd2add81b 536 {
emcu 0:b05cd2add81b 537 meas = TempCurLight.read_u16(); // Converts and read the analog input value
emcu 0:b05cd2add81b 538 meas1 = SensCrep.read_u16(); // Converts and read the analog input value
emcu 0:b05cd2add81b 539 meas2 = LightSensor.read_u16(); // Converts and read the analog input value
emcu 0:b05cd2add81b 540 pc.printf("TempCurLight is:%d - SensCrep is:%d - LightSensor is:%d\n\r", meas, meas1, meas2);
emcu 0:b05cd2add81b 541 wait(2);
emcu 0:b05cd2add81b 542 }
emcu 0:b05cd2add81b 543
emcu 0:b05cd2add81b 544 pc.printf("I/O test, press EMERGENCY for exit from I/O test\n\r");
emcu 0:b05cd2add81b 545 while(EMERGENCY == NotPressed)
emcu 0:b05cd2add81b 546 {
emcu 0:b05cd2add81b 547 // Test OutPut pin ********************************
emcu 0:b05cd2add81b 548 if (BlueButton == Pressed)
emcu 0:b05cd2add81b 549 {
emcu 0:b05cd2add81b 550 pc.printf("Sequence for testing OutPut\n\r");
emcu 0:b05cd2add81b 551 ReleLight = 1;
emcu 0:b05cd2add81b 552 wait(0.5);
emcu 0:b05cd2add81b 553 ReleLight = 0;
emcu 0:b05cd2add81b 554 M1Com = 1;
emcu 0:b05cd2add81b 555 wait(0.5);
emcu 0:b05cd2add81b 556 M1Com = 0;
emcu 0:b05cd2add81b 557 M1Open = 1;
emcu 0:b05cd2add81b 558 wait(0.5);
emcu 0:b05cd2add81b 559 M1Open = 0;
emcu 0:b05cd2add81b 560 M1Close = 1;
emcu 0:b05cd2add81b 561 wait(0.5);
emcu 0:b05cd2add81b 562 M1Close = 0;
emcu 0:b05cd2add81b 563 Flashing = 1;
emcu 0:b05cd2add81b 564 wait(0.5);
emcu 0:b05cd2add81b 565 Flashing = 0;
emcu 0:b05cd2add81b 566 }
emcu 0:b05cd2add81b 567 // END - Test OutPut pin **************************
emcu 0:b05cd2add81b 568
emcu 0:b05cd2add81b 569
emcu 0:b05cd2add81b 570 // Test Input Signals *****************************
emcu 0:b05cd2add81b 571
emcu 0:b05cd2add81b 572 // Test FCA
emcu 0:b05cd2add81b 573 if (FCA == 0 & FCA_mem == NotPressed)
emcu 0:b05cd2add81b 574 { // FCA is pressed, we do the delay for Debounce
emcu 0:b05cd2add81b 575 wait(DLY);
emcu 0:b05cd2add81b 576 if (FCA == 0)
emcu 0:b05cd2add81b 577 {
emcu 0:b05cd2add81b 578 M1Close = 1;
emcu 0:b05cd2add81b 579 FCA_mem = Pressed;
emcu 0:b05cd2add81b 580 pc.printf("FCA is PRESSED (0)\n\r");
emcu 0:b05cd2add81b 581 }
emcu 0:b05cd2add81b 582 else
emcu 0:b05cd2add81b 583 FCA_mem = NotPressed;
emcu 0:b05cd2add81b 584 }
emcu 0:b05cd2add81b 585 if (FCA == 1)
emcu 0:b05cd2add81b 586 {
emcu 0:b05cd2add81b 587 FCA_mem = NotPressed;
emcu 0:b05cd2add81b 588 M1Close = 0;
emcu 0:b05cd2add81b 589 }
emcu 0:b05cd2add81b 590
emcu 0:b05cd2add81b 591 // Test FCC
emcu 0:b05cd2add81b 592 if (FCC == 0 & FCC_mem == NotPressed)
emcu 0:b05cd2add81b 593 { // FCC is pressed, we do the delay for Debounce
emcu 0:b05cd2add81b 594 wait(DLY);
emcu 0:b05cd2add81b 595 if (FCC == 0)
emcu 0:b05cd2add81b 596 {
emcu 0:b05cd2add81b 597 Flashing = 1;
emcu 0:b05cd2add81b 598 FCC_mem = Pressed;
emcu 0:b05cd2add81b 599 pc.printf("FCC is PRESSED (0)\n\r");
emcu 0:b05cd2add81b 600 }
emcu 0:b05cd2add81b 601 else
emcu 0:b05cd2add81b 602 FCC_mem = NotPressed;
emcu 0:b05cd2add81b 603 }
emcu 0:b05cd2add81b 604 if (FCC == 1)
emcu 0:b05cd2add81b 605 {
emcu 0:b05cd2add81b 606 FCC_mem = NotPressed;
emcu 0:b05cd2add81b 607 Flashing = 0;
emcu 0:b05cd2add81b 608 }
emcu 0:b05cd2add81b 609
emcu 0:b05cd2add81b 610 // Test PP
emcu 0:b05cd2add81b 611 if (PP == 0 & PP_mem == NotPressed)
emcu 0:b05cd2add81b 612 { // PP is pressed, we do the delay for Debounce
emcu 0:b05cd2add81b 613 wait(DLY);
emcu 0:b05cd2add81b 614 if (PP == 0)
emcu 0:b05cd2add81b 615 {
emcu 0:b05cd2add81b 616 ReleLight = 1;
emcu 0:b05cd2add81b 617 PP_mem = Pressed;
emcu 0:b05cd2add81b 618 pc.printf("PP is PRESSED (0)\n\r");
emcu 0:b05cd2add81b 619 }
emcu 0:b05cd2add81b 620 else
emcu 0:b05cd2add81b 621 PP_mem = NotPressed;
emcu 0:b05cd2add81b 622 }
emcu 0:b05cd2add81b 623 if (PP == 1)
emcu 0:b05cd2add81b 624 {
emcu 0:b05cd2add81b 625 PP_mem = NotPressed;
emcu 0:b05cd2add81b 626 ReleLight = 0;
emcu 0:b05cd2add81b 627 }
emcu 0:b05cd2add81b 628
emcu 0:b05cd2add81b 629 // Test FOTO
emcu 0:b05cd2add81b 630 if (FOTO == 0 & FOTO_mem == NotOK)
emcu 0:b05cd2add81b 631 { // FOTO is pressed, we do the delay for Debounce
emcu 0:b05cd2add81b 632 wait(DLY);
emcu 0:b05cd2add81b 633 if (FOTO == 0)
emcu 0:b05cd2add81b 634 {
emcu 0:b05cd2add81b 635 M1Com = 1;
emcu 0:b05cd2add81b 636 FOTO_mem = OK;
emcu 0:b05cd2add81b 637 pc.printf("FOTO is ACTIVE (0)\n\r");
emcu 0:b05cd2add81b 638 }
emcu 0:b05cd2add81b 639 else
emcu 0:b05cd2add81b 640 FOTO_mem = NotOK;
emcu 0:b05cd2add81b 641 }
emcu 0:b05cd2add81b 642 if (FOTO == 1)
emcu 0:b05cd2add81b 643 {
emcu 0:b05cd2add81b 644 FOTO_mem = NotOK;
emcu 0:b05cd2add81b 645 M1Com = 0;
emcu 0:b05cd2add81b 646 }
emcu 0:b05cd2add81b 647
emcu 0:b05cd2add81b 648 // Test EMERGENCY
emcu 0:b05cd2add81b 649 if (EMERGENCY == 0 & EMERGENCY_mem == NotPressed)
emcu 0:b05cd2add81b 650 { // EMERGENCY is pressed, we do the delay for Debounce
emcu 0:b05cd2add81b 651 wait(DLY);
emcu 0:b05cd2add81b 652 if (EMERGENCY == 0)
emcu 0:b05cd2add81b 653 {
emcu 0:b05cd2add81b 654 M1Open = 1;
emcu 0:b05cd2add81b 655 EMERGENCY_mem = Pressed;
emcu 0:b05cd2add81b 656 pc.printf("EMERGENCY is PRESSED (0)\n\r");
emcu 0:b05cd2add81b 657 }
emcu 0:b05cd2add81b 658 else
emcu 0:b05cd2add81b 659 EMERGENCY_mem = NotPressed;
emcu 0:b05cd2add81b 660 }
emcu 0:b05cd2add81b 661 if (EMERGENCY == 1)
emcu 0:b05cd2add81b 662 {
emcu 0:b05cd2add81b 663 EMERGENCY_mem = NotPressed;
emcu 0:b05cd2add81b 664 M1Open = 0;
emcu 0:b05cd2add81b 665 }
emcu 0:b05cd2add81b 666 // END - Test Input Signals **************************
emcu 0:b05cd2add81b 667 }
emcu 0:b05cd2add81b 668 }
emcu 0:b05cd2add81b 669 // END - Function for testing I/O and ADC used in the program ***********************
emcu 0:b05cd2add81b 670
emcu 0:b05cd2add81b 671 // **************************** END FUNCTIONS ***************************************