Read digital signal status then timing cycle time between two cycle

Hi everyone,

I am currently working on a project which is read a digital input signal from a machine. The machine operation is as below:

when the mold close, the machine gives the "HIGH" signal, when mold is open the machine gives "LOW" signal. A cycle is counted when from the mold close --> open --> close.

My goal is to count the cycle and cycle time of each cycle. I have try with the code below but doesn't work. Appreciate your help or guidance for me to do this project.

int MoldInput = 5;
int Moldstt;
int count;
unsigned long Timer, Cycletime, Moldtiming;
void setup() {
  // put your setup code here, to run once:
  pinMode (MoldInput,INPUT);

void loop() {
  // put your main code here, to run repeatedly:
  Moldstt = digitalRead(MoldInput);
  if(Moldstt == HIGH){
    Timer = millis();
  if(Moldstt == LOW){
    Moldtiming = Timer + millis();
    if(Moldstt == HIGH){
      Cycletime = Moldtiming;
  Timer = 0;
  Moldtiming = 0;
  Cycletime =0;

You need to detect when the input becomes HIGH or LOW rather than when it is HIGH or LOW. Take a look at the StateChangeDetection example in the IDE