I have a problem with my CODESYS program. I have three buttons, which are defined as input. For each button a number is stored. For example the number 1. I have now created a program, which recognizes an edge on the button and stores the numerical value (2) of the button in an array. If you now press another button with the value (3), the value is also stored in a variable again. The two variables should be added together. 2 + 3 = 23. In my program I have the problem that if I press the button tester with the value 2, I get 22. This is wrong. I think the problem is due to the bruise of the push button. Several edges are detected. So I wanted to solve this software technically with a delay. Do you have any idea how I could program that?
CODE:
IF (PLC_PRG.calls[5].gpio = TRUE) THEN // edge detection on gpio
IF (counter = 0) THEN // counter for the first value
floorstorage2[0] := PLC_PRG.calls[5].message.floorstore[5]; // save button value in the array to calculate the total
counter := 1;
ELSE
floorstorage2[1] := PLC_PRG.calls[5].message.floorstore[5]; // save button value in the array to calculate the total
counter := 0;
END_IF
END_IF
IF (PLC_PRG.calls[6].gpio = TRUE) THEN // edge detection on gpio
IF (counter = 0) THEN // counter for the first value
floorstorage2[0] := PLC_PRG.calls[6].message.floorstore[6]; // save button value in the array to calculate the total
counter := 1;
ELSE
floorstorage2[1] := PLC_PRG.calls[6].message.floorstore[6]; // save button value in the array to calculate the total
counter := 0;
END_IF
END_IF
IF (PLC_PRG.calls[7].gpio = TRUE) THEN // edge detection on gpio
IF (counter = 0) THEN // counter for the first value
floorstorage2[0] := PLC_PRG.calls[7].message.floorstore[7]; // save button value in the array to calculate the total
counter := 1;
ELSE
floorstorage2[1] := PLC_PRG.calls[7].message.floorstore[7]; // save button value in the array to calculate the total
counter := 0;
END_IF
END_IF
GlobalVar.floorstorage := concat(floorstorage2[0],floorstorage2[1]); // Total of value 1 and value 2 (1 + 2 = 12)
You need to implement edge detection. Here is a code template you can use:
// Generate Oneshot Signal
VAR_INPUT
SIGNAL : BOOL; // Input
END_VAR
VAR
LATCH_SIGNAL : BOOL; // Latch
END_VAR
VAR_TEMP
OS_P_SIGNAL : BOOL; // Oneshot - Rising edge detection
OS_N_SIGNAL : BOOL; // Oneshot - Falling edge detection
END_VAR
//Code - Rising edge detection
OS_P_SIGNAL := SIGNAL AND NOT LATCH_SIGNAL;
LATCH_SIGNAL := SIGNAL;
//Code - Falling edge detection
OS_N_SIGNAL := NOT SIGNAL AND NOT LATCH_SIGNAL;
LATCH_SIGNAL := NOT SIGNAL;