new function block for controlling lights in a house

Begonnen von brose, 07. Juli 2010, 11:16:31

Vorheriges Thema - Nächstes Thema

0 Mitglieder und 2 Gäste betrachten dieses Thema.

brose

I made a new function block to control the lights of my house. With some simple configuring you can get some nice functionality. You can control the on/off function of the lamp in a classic way, you can specify a maximum on time. And you can also configure the long click to reset all the lights in the room, or the complete floor or even the complete house. So when you switch on a number of lights in the basement, you can hold the button on top of the stairs a bit longer and all lights in the basement will be switched off.

I hope it helps somebody, or someone can come up with a even better way of controlling the lights.

Local variables of the FB

FUNCTION_BLOCK TOGGLE
VAR_INPUT
INP: BOOL; (* This input is related to the button press *)
LAMP: BOOL; (* Which lamp is controlled by this button? *)
ON_TIME:TIME; (* How long may the lamp be activated?*)
ROOM: REAL; (* In which room will it control lights? *)
FLOOR: REAL; (* On which floor will it control lights? *)
GROUP: REAL; (* Within which group will it control lights? *)
MROOM: BOOL; (* Can it act as master of the room? *)
MFLOOR: BOOL; (* Can it act as master of the floor? *)
MGROUP: BOOL; (* Can it act as master of the group? *)
END_VAR
VAR_OUTPUT
OUTP: BOOL ; (* This output will control the light *)
END_VAR
VAR
BUTTON_PRESS:R_TRIG;
BUTTON_RELEASE:F_TRIG;
LONG: INT :=0;
TIMER_BUTTONPUSH: TON;
TIMER_LAMP: TON;
LAMP_ACTIVATOR: BOOL; (* Is this button the current activator of the lamp? *)
END_VAR


Code of the FB

BUTTON_PRESS(CLK:=INP); (* Reset variables at rising edge *)
IF BUTTON_PRESS.Q THEN
LONG:=0; (* Reset duration of button press *)
RESET_ROOM:=0; (* Clear possible ROOM reset *)
RESET_FLOOR:=0; (* Clear possible FLOOR reset *)
RESET_GROUP:=0; (* Clear possible GROUP reset *)
END_IF;

IF LAMP=FALSE
THEN LAMP_ACTIVATOR:=FALSE; (* If this button was the activator, it loses its role when *)
END_IF; (* the lamp is deactivated by another button *)


TIMER_BUTTONPUSH(IN:=INP , PT:=LONG_PUSH ); (* Start timer for LONG_PUSH detection*)
IF TIMER_BUTTONPUSH.Q THEN LONG:=1; (* If LONG_PUSH is detected then LONG=1*)
END_IF; (* Else, LONG stays 0 *)

BUTTON_RELEASE(CLK:=INP); (*define function_block outputs*)

CASE LONG OF
0: IF BUTTON_RELEASE.Q THEN
IF OUTP=FALSE (* In this case we have a normal button push&release *)
THEN
OUTP:=TRUE; (* Toggle: if false, make true *)
LAMP_ACTIVATOR:=TRUE; (* The lamp was activated by this button, only one is possible *)
ELSE
OUTP:=FALSE; (* Toggle: if true, make false *)
LAMP_ACTIVATOR:=FALSE; (* The lamp is deactivated - there is no activator *)
END_IF;
END_IF;
1: IF MGROUP THEN (* In this case we have a long button push, no release needed *)
RESET_GROUP:=GROUP; (* If master of the group, reset own group *)
ELSIF MFLOOR THEN
RESET_FLOOR:=FLOOR; (* If master of the floor, reset own floor *)
ELSIF MROOM THEN
RESET_ROOM:=ROOM; (* If master of the room, reset own room *)
END_IF;
LONG:=0; (* Reset the LONG variable, otherwise we stay in this CASE loop *)
END_CASE;

TIMER_LAMP(IN:=LAMP_ACTIVATOR, PT:=ON_TIME); (* Start the lamp timer when the lamp is turned on by the activator button *)

IF LAMP_ACTIVATOR  AND TIMER_LAMP.Q (* If the lamp timer is 0, shut down lamp*)
THEN
OUTP:=FALSE;
END_IF;


IF RESET_GROUP=GROUP OR RESET_FLOOR=FLOOR OR RESET_ROOM=ROOM
THEN OUTP:=FALSE; (* Are we part of a possible reset? *)
END_IF;



How to use, define global constants with the names of the rooms, floors, etc.

VAR_GLOBAL CONSTANT
LONG_PUSH : TIME := T#0.5s;

DELAY: TIME := T#0.6s;

NO: BOOL := FALSE;
YES: BOOL := TRUE;

OTHER: REAL := 1; (* The different rooms *)
BASEM1: REAL := 2;
BASEM2:         REAL := 3;
BASEM3: REAL := 4;
BASEM4: REAL := 5;
STAIRS: REAL := 6;

CELLAR: REAL := 2; (* The different floors *)
GROUND: REAL := 3;

HOUSE: REAL := 2; (* The different groups *)

END_VAR


Some locale variables

VAR
TOG_D01_1: TOGGLE;
TOG_D02_1: TOGGLE;
TOG_D03_1: TOGGLE;
TOG_D04_1: TOGGLE;
TOG_D06_2: TOGGLE;
END_VAR


And then you configure it like you want it. So in this case I configured that the long click of button that controls the lights of the stairs can switch of the lights in the complete cellar. And the button that controls the lamp LY3 in BASEM3, can switch off all lights in BASEM3.


TOG_D01_1(INP:=M11_lila, LAMP:=LY7, ON_TIME:=T#2h, ROOM:=BASEM1, FLOOR:=CELLAR, GROUP:=HOUSE, MROOM:=NO, MFLOOR:=NO,  MGROUP:=NO );
TOG_D02_1(INP:=M7_grijs, LAMP:=LY6, ON_TIME:=T#30m, ROOM:=BASEM2, FLOOR:=CELLAR, GROUP:=HOUSE, MROOM:=NO, MFLOOR:=NO,  MGROUP:=NO );
TOG_D03_1(INP:=M8_geel, LAMP:=LY5, ON_TIME:=T#2h, ROOM:=STAIRS, FLOOR:=CELLAR, GROUP:=HOUSE, MROOM:=NO, MFLOOR:=YES,  MGROUP:=NO );
TOG_D04_1(INP:=M14_blauw,LAMP:=LY3, ON_TIME:=T#4h, ROOM:=BASEM3, FLOOR:=CELLAR, GROUP:=HOUSE, MROOM:=YES,MFLOOR:=NO,MGROUP:=NO);
TOG_D06_2(INP:=M7_groen, LAMP:=LY1, ON_TIME:=T#4h, ROOM:=BASEM3, FLOOR:=CELLAR, GROUP:=HOUSE, MROOM:=NO, MFLOOR:=NO, MGROUP:=NO );

IF ALARM_ACTIVE=FALSE THEN
LY7:= TOG_D01_1.OUTP;
LY6:=         TOG_D02_1.OUTP;
LY5:=         TOG_D03_1.OUTP;
LY3:=         TOG_D04_1.OUTP;
LY1:= TOG_D06_2.OUTP;
END_IF