Reading "ac voltage pressence" using software only.

I do not mind to buy another Arduino board if this one is fried.

I know that some of the purists do not even want to try to answer if they see approach like "I don't care if I burn it."

As I said, this is code wise question, can you look at it like that.

So it is not the point will I fry the board or not. It is completely clear what are the consequences.

Here is what I do have so far.

My test got a NEW level of stupid, yes I know but it is perfect environment for this test. I do not have to go downstairs upstairs.

Audio output from my laptop is enough for Arduino sensor analogRead(A1) to read the current when on positive side.

So now I can "emulate" ringing when I play a song. I do have ZERO ( negative side of wave ) and random positive values ( positive side of wave ).

Here is the code that partially works.

void setup() {
  Serial.begin(9600);
}

unsigned long currentMillis;
unsigned long ringStartTime = 0;
bool ringing = false;

void loop() {
  currentMillis = millis();
  if ( analogRead(A1) > 0 ) {
    if ( !ringing ) {
      ringStartTime = currentMillis;
      ringing = true;
    }
  }
  if ( ringing and currentMillis - ringStartTime > 100 and analogRead(A1) == 0) {
    Serial.println("OPENING!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    delay( currentMillis - ringStartTime );

    ringStartTime = 0;
    ringing = false;
  }



  Serial.print("RingStartTime: ");
  Serial.println(ringStartTime);
  Serial.print("CurrentMillis: ");
  Serial.println(currentMillis);
}

Idea is that I ignore ZERO each 100 millis. But than again if I still RING and want to RING more than 100 millis. I get into issue that sometimes A1 will be zero and condition is met for OPEN.

My approach is completely wrong I see that from the code. But I've posted it to have something to work with.

I want to achieve to ignore negative reading as long as I am ringing. I know this sounds really stupid because ringing will stop once it hits negative side.

I guess I need backOff time check. So when I really do STOP ringing wait for 1 second before opening.

If under one second there is ringing pressent do nothing again until next backoff time.

I guess I need to reset some checks at some point, but just cant grasp it.