I want to print the time on the serial monitor at the start and end of a signal (Help)

Use the Arduino UNO with the DS1302 RTC Module to print the time on the serial monitor when it receives a continuous signal. I just want to get the start and end time. That is, I want to record the time when the signal is received and the time when it is interrupted.
I want to show this result:
Signal start time : YYYY/MM/DD HH:MM:SS
Signal break time : YYYY/MM/DD HH:MM:SS

I already tried a lot of codes but didn't find the solution.

if(Level <= 200)
 {
 
  do{
     Serial.print(RTC.dayofmonth);
  Serial.print("/");
  Serial.print(RTC.month);
  Serial.print("/");
  Serial.print(RTC.year);
  Serial.print(" ");
  Serial.print(RTC.hours);
  Serial.print(":");
  Serial.print(RTC.minutes);
  Serial.print(":");
  Serial.println(RTC.seconds);
  Serial.println("Signal  OFF ");
      break;
   }while (X = 0);
   
  
 }
     
else
  {
    
  do{
      Serial.print(RTC.dayofmonth);
  Serial.print("/");
  Serial.print(RTC.month);
  Serial.print("/");
  Serial.print(RTC.year);
  Serial.print(" ");
  Serial.print(RTC.hours);
  Serial.print(":");
  Serial.print(RTC.minutes);
  Serial.print(":");
  Serial.println(RTC.seconds);
  Serial.println("Signal  ON ");
     break;
   }while (X = 2);
  
   }
}```

Can you show us your code that displays the "YYYY/MM/DD HH:MM:SS" on the serial monitor? That is the first step in fixing you program.
Paul

If by continuous signal you just mean a pin changing from low to high and then back to low later (or high to low and back to high) you could probably draw on the approach taken in the state change detect example at file / examples / 2 digital in the ide. Just get it first to recognise those changes and report in the ide "start of signal" and "end of signal".

Now I'm using the iterative loop in order to do the code only once, because I didn't meet the condition on purpose, but because the code is inside the VOID LOOP, it wasn't applied correctly

if(Level <= 200)
 {
 
  do{
     Serial.print(RTC.dayofmonth);
  Serial.print("/");
  Serial.print(RTC.month);
  Serial.print("/");
  Serial.print(RTC.year);
  Serial.print(" ");
  Serial.print(RTC.hours);
  Serial.print(":");
  Serial.print(RTC.minutes);
  Serial.print(":");
  Serial.println(RTC.seconds);
  Serial.println("Signal  OFF ");
      break;
   }while (X = 0);
   
  
 }
     
else
  {
    
  do{
      Serial.print(RTC.dayofmonth);
  Serial.print("/");
  Serial.print(RTC.month);
  Serial.print("/");
  Serial.print(RTC.year);
  Serial.print(" ");
  Serial.print(RTC.hours);
  Serial.print(":");
  Serial.print(RTC.minutes);
  Serial.print(":");
  Serial.println(RTC.seconds);
  Serial.println("Signal  ON ");
      break;
   }while (X = 2);
  
   }
}

Hello
Who is supporting the value for the variable X?

   }while (X = 2);

should be:

}while (X == 2);

Can you show your entire sketch please?

The variable is constant I set a value of 1. In order to make the loop run once.

What is the difference between them.
The problem here is that the iterative loop is in conflict with the void loop function. Because instead of working only once and checking the condition, it continues to work every time.

X=1 is an assignment. X==1 is a comparison. Putting an assignment in a loop condition doesn't make sense. Indeed, a comparison in a loop conditional makes no sense unless you change the condition within the loop.

I have no idea what you mean by "iterative loop is in conflict with the void loop function".

+1. My guess, since I have to guess, is that X is an integer and somewhere you increment it to change the flow of the code.

Entire sketch pls OR a complete small sketch, setup() through loop(), compiles &c., that we can run for ourselves, that shows your attempt to get the behaviour you seek.

a7

My guess is the OP just wants to print when the signal level changes. OP needs to look for a change in the level and not the state of the level.

Untested. See if you can run your finger through this to see how it works. And if it does what you want.

void printDateTime()
{
  Serial.print(RTC.dayofmonth);
  Serial.print("/");
  Serial.print(RTC.month);
  Serial.print("/");
  Serial.print(RTC.year);
  Serial.print(" ");
  Serial.print(RTC.hours);
  Serial.print(":");
  Serial.print(RTC.minutes);
  Serial.print(":");
  Serial.println(RTC.seconds);
}

int printStatus() {
  static int flag = 0;

  if (Level <= 200) {
    if (flag != -1) {
      printDateTime();
      Serial.println("Signal  OFF ");
    }
    flag = -1;
  }
  else {		// Level > 200
    if (flag != 1) {
      printDateTime();
      Serial.println("Signal  ON ");
    }
    flag = 1;
  }
}

Sry, I don't usually like to hand you a fish. So do take a look. Just one of as many ways to do this as there are programmers, approximately. :expressionless:

It may print more than you want. You could code some hysteresis in there like your home thermostat employs so the A/C not be going on and off alla time.

a7

Tested. Here's a complete program in the wokwi simulator, my new favorite toy, easier than dragon an Arduino to the beach.

Misnamed right now. :expressionless:

a7

That's nice. You gave me some ideas, thank you very much

See #3 :sunglasses:

1 Like