My homework is to make the PWM start after reading a certain signal.
The signal I'm referring to is from a Zero Crossing Detector.
https://drive.google.com/file/d/0B7EHOI6yi950YXFCcUZFQk5GSUE/view?usp=sharing
Here, as attached, the blue color is ZCD wave. The yellow one is 12VAC.
For the time being, I connect that ZCD output signal to Arduino UNO A5 pin.
Hoping that the Arduino would read the pin and generate the PWM when the ZCD is at low state at the first time the Arduino read it and stop the PWM when the A5 pin is unplugged.
But instead, my code only let the PWM start after the A5 pin is unplugged and can't be stopped unless manually push the reset button or turn the Arduino off.
Here's the code I use:
int fasa, x=62;
void setup() {
Serial.begin(9600);
fasa=analogRead(A5); //to read zerocross
while (fasa!=0){
fasa=analogRead(A5);
Serial.println(fasa);
void loop() {
digitalWrite(3,HIGH);
digitalWrite(9,LOW);
delayMicroseconds(x); //x is 62, the 50% duty cycle for 8Khz
digitalWrite(3,LOW);
digitalWrite(9,HIGH);
delayMicroseconds(125-x);
}
So, the goal is: to make the Arduino UNO generate PWM but the time it starts is based on the input signal.
I have no idea how to code it.
Any response is appreciated.
Thanks and Regards.