Try turning on the internal pullup resister on pin 4 so the output of the opto has something to pull low, and will be read as high when the opto is off.Once you put some load on the output of that diode bridge you will start getting 120Hz ripple also - so put 100nF cap and larger (uF) cap across the output as well.

I wasn't very clear - I'm not looking to measure the Hz cycle - I'm just looking to see if the power is on during one second as I want to record the number of seconds it's on during a 5 minute period.

Most of my Serial.prints are to figure out if my code (I'm really good at causing buffer overruns) was the issue or if fly-back noise is causing the arduino to lockup - so I understand that serial output can cause you to miss events.

I'll try and add some cap's.

I thought that my "digitalWrite(pin,1)" pulls sets the internal pullup resister so I'll look to see what's needed to do that.

I first wired it up with just one diode reverse the led - and that was giving me what I wanted until I would plug and unplug the 9vac transformer and the arduino would then lock up - I'm guessing from fly-back/emf noise. I then when to the bridge to see if that corrected the issue but it didn't.

At some point I'm actually looking to use the circuit at a 18vac (hvac) - But I figured I would start with 9volts first cause that's the transformer that I had.

Maybe it would - I was looking for digitalWrite(pin, HIGH); and didn't see the statement at all.Maybe add an external pullup then.

Also, try putting this section of code before setup, might have an impact. As is, its not it setup, its not in loop, its just kinda floating out there, which is expected before setup, I don't know about after.

void loop(){ int r = digitalRead(pin);//if hasn't been on in this second check if on now if (thisSecondOn == 0) { if (r == pinOn) { //do we have power Serial.print("+"); //debug comment only thisSecondOn = 1; //got power this second dont need to check again until next second onSeconds++; //add to power seconds on } }//new second so reset so that we check during this second time period if (millis() - millisLast > 1000) { millisLast = millis(); Serial.print(thisSecondOn); //debug comment only thisSecondOn = 0;

}

//after a minute save our data//byte for pin60[] would be better then int as should never be more then 60 for seconds in a minute - changed to int to make sure //not some overflow issue

Is it just me, or have all the files attached to posts disappeared? Not even an X in a box to say invalid links or anything?

You are wired up like this?I can see no reason for the arduino to be affected by the state of pin 4. You have only the arduino connected to ground & +5V, correct?All the stuff on the left side of the opto is not connected to anything?

Your showing a 4N25 - and I'm using a 4N35 is there much of a difference?

correct the opto input side is not connected to the output (arduino) side in anyway.

I also plugged in a different 4N35 and also tried a 4N33

arduino side is +5vbase - 1k? - Digital Pin 4

like I said it works great except if I unplug the transformer multiple times - I get my different on/off states (shown below as 1 and 0) if I remove one of the 10vac lines or use a switch to power off the transformer

4N25, no, I was just using that as a generic optoisolator, same with the other parts.

What do you mean "base - 1k? - Digital Pin 4"That is not correct; the collector goes to Pin 4. The base is not connected.

Try putting a load resister in parallel with the filter caps, something to bleed them down & make the input LED turn off when there is no power.Like a 1K.

I don't have an explanation for the power switching on/off causes issues with your code.Maybe try something simpler to start - just turn on the D13 LED whenever you see pin4 high, and off when low. If that will not work stable, then you have got some other basic problem.