Tying to get a ZC error detection correct here is my code and can't seem to find where the problem is. when i swtich off the zc source (Source being 12v) connected to H11AA1 with a 1K resistor in series and the output to PIN4 with 1K in series to arduino.
anishkgt:
Tying to get a ZC error detection correct here is my code and can't seem to find where the problem is. when i swtich off the zc source (Source being 12v) connected to H11AA1 with a 1K resistor in series and the output to PIN4 with 1K in series to arduino.
I recognize all the individual words but I'm sorry that I have no idea what you are trying to convey.
Please re-write this so that someone who knows nothing at all about your project would understand it. And use proper punctuation.
Here is my serial data
Start Time: 5724
Current Time: 9753
Elapsed Time: 4029
Time dif: 5725
I'm guessing that this is not the data you would like to see but you have not told us what is wrong with it.
The above code is working and does detect the ZC with an LED. No problem there. Now i've bought an 4x20 LCD with YwROBOT LCM1602 on its back.
The led blinks when it detects HIGH and LOW so basically blinking very fast. When using the LCD to update one like it just flickers when it should be showing no characters on line 0,2. here the led remains on. But no problem when the ac source for zc is switched off. The LCD displays the message.
Well, the end of loop() ALWAYS sets zeroCrossingFlag to false, so the beginning of loop() will always try to show the text. (Except in the rare case of finding a zero crossing during that nanosecond between ending and starting the loop.)
It takes a certain amount of time to write that stuff out to the LCD. So you start seeing those characters on the LCD.
Then the loop ALWAYS erases those characters, if zeroCrossingFlag has become true while you were writing out the message. So they flicker. A lot.
well that is true ! The could be some way to just display it on an LCD like with a condition that was when things got a little messier and here i am at the forum.
boolean zeroCrossingFlag = false;
unsigned long timeout = 1000;
const byte rdy = 4;//indicator led
void setup() {
Serial.begin(115200);
// lcd set up
pinMode(rdy, OUTPUT);
pinMode(2, INPUT_PULLUP);//zero cross interrupt pin
attachInterrupt(0, setFlag, FALLING);//zero cross detect
}
void loop() {
unsigned long startTime = millis();
while (!zeroCrossingFlag && (millis() - startTime < timeout))
{
//hang in while loop until either zeroCrossFlag or timeout occurs
//both conditions need to be met to stay in while loop
}
if (zeroCrossingFlag)//exit while loop due to flag set
{
Serial.println("exit while loop with zero cross detected");
//write lcd zero cross OK messsage
zeroCrossingFlag = false;
digitalWrite(rdy,HIGH);//led on when triggered
}
else //exit while loop due to time out error
{
Serial.println("zero cross time out error");
//write lcd error message
digitalWrite(rdy, LOW);
//while (1); // halt sketch on timeout?
}
}
void setFlag()
{
zeroCrossingFlag = true;//interrupt sets flag true
}
The if condition seems to be true always and does not skip to the else block.
Not in my testing. When I run the sketch and apply a 50Hz pulse signal to pin2 the led is on, and one second after I remove the signal the led goes off.
The zeroCrossFlag is set false after that if conditional is entered. It can only be set true again by the zero cross interrupt.