phase control of mains voltage using Arduino

Thread Starter

denison

Joined Oct 13, 2018
252
Hi, I have an excellent sketch for setting turn on times of the mains AC voltage or what is known technically as phase control. I am using a random phase optocoupler and triac to vary the brightness of a 240vac filament globe. There is no flicker at all in the globe. The controlled device could just as easily be any mains operated device.
I have attached a copy of the sketch (program). Now I need to exit the loop every say 1 minute to read an analog voltage. This analog voltage automatically sets the delay times. Can anybody provide me with the programming to do this? It will probably involve a goto statement to go to that part of the sketch which reads the analog voltage. The reading of this voltage would be so fast at computer speeds that the phase control would not noticeably be affected.
Can the programming needed be as simple as possible. Everybody likes simplicity.
Sorry couldn't upload file. Not allowed file extension. So here it is;
bool state=LOW;
int pin=7; // this pin connects to zero crossing detector circuit

int ledPin = 8; // pin 8 connects to random phase opto

void setup() {

Serial.begin(9600);
pinMode(pin,INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
if(digitalRead(pin)==HIGH && state==LOW){ //rising edge detection
Serial.println(5); //picks up rising edge but not falling edge
digitalWrite(ledPin,LOW);
delayMicroseconds (4500);
digitalWrite(ledPin,HIGH);
delayMicroseconds (4500);
digitalWrite(ledPin,LOW);
}
}
 

Thread Starter

denison

Joined Oct 13, 2018
252
hi deni,
Consider using the Arduino millis feature, to call the ADC.
E
https://forum.arduino.cc/t/using-millis-for-timing-a-beginners-guide/483573
Yes I am considering that. In the meantime I have tried the following to set phase control points in Arduino. Doesn't work. Program works great without reading an analog input. Can't attach files of arduino sketches wrong file extension. So have to list it here. Whats wrong with this sketch. No errors indicated in compiling.


C:
bool state=LOW;
int pin=7;         // this pin connects to zero crossing detector circuit
int v;
int vin = 3;
int us; 

int ledPin = 8;   // pin 8 connects to random phase opto

void setup() {
 
    Serial.begin(9600);
    pinMode(pin,INPUT);
    pinMode(ledPin, OUTPUT);
  }
void loop() {
 
    while (us<=9004 && us>=100){  
     v = analogRead(vin);
     us= (424*v-216564);
     Serial.println(us);
     if(digitalRead(pin)==HIGH && state==LOW){ //wait for hi pulse at zero crossing
     digitalWrite(ledPin,LOW); // if high pulse received execute these 5 lines
     delayMicroseconds (us);   // if no hi pulse continue waiting in while loop
     digitalWrite(ledPin,HIGH);
     delayMicroseconds (9100-us);
    digitalWrite(ledPin,LOW);
  }
  }
     while (us>9004){
       v = analogRead(vin);
     us= (424*v-216564);
      digitalWrite(ledPin,LOW);
      Serial.println(us);
     
     }
     while (us<100){
      v = analogRead(vin);
     us= (424*v-216564);
      digitalWrite(ledPin,HIGH);
      Serial.println(us);
     
     }
  }
 
Last edited by a moderator:

ericgibbs

Joined Jan 29, 2010
14,192
hi Deni,
Did you try Insert > C Like for your Code post.?

E

Test.
C-like:
bool state=LOW;

int pin=7;         // this pin connects to zero crossing detector circuit

int v;

int vin = 3;

int us;


int ledPin = 8;   // pin 8 connects to random phase opto


void setup() {



    Serial.begin(9600);

    pinMode(pin,INPUT);

    pinMode(ledPin, OUTPUT);

  }

void loop() {



    while (us<=9004 && us>=100){

     v = analogRead(vin);

     us= (424*v-216564);

     Serial.println(us);

     if(digitalRead(pin)==HIGH && state==LOW){ //wait for hi pulse at zero crossing

     digitalWrite(ledPin,LOW); // if high pulse received execute these 5 lines

     delayMicroseconds (us);   // if no hi pulse continue waiting in while loop

     digitalWrite(ledPin,HIGH);

     delayMicroseconds (9100-us);

    digitalWrite(ledPin,LOW);

  }

  }

     while (us>9004){

       v = analogRead(vin);

     us= (424*v-216564);

      digitalWrite(ledPin,LOW);

      Serial.println(us);

   

     }

     while (us<100){

      v = analogRead(vin);

     us= (424*v-216564);

      digitalWrite(ledPin,HIGH);

      Serial.println(us);

   

     }

  }
 

Thread Starter

denison

Joined Oct 13, 2018
252
hi Deni,
Did you try Insert > C Like for your Code post.?

E

Test.
C-like:
bool state=LOW;

int pin=7;         // this pin connects to zero crossing detector circuit

int v;

int vin = 3;

int us;


int ledPin = 8;   // pin 8 connects to random phase opto


void setup() {



    Serial.begin(9600);

    pinMode(pin,INPUT);

    pinMode(ledPin, OUTPUT);

  }

void loop() {



    while (us<=9004 && us>=100){

     v = analogRead(vin);

     us= (424*v-216564);

     Serial.println(us);

     if(digitalRead(pin)==HIGH && state==LOW){ //wait for hi pulse at zero crossing

     digitalWrite(ledPin,LOW); // if high pulse received execute these 5 lines

     delayMicroseconds (us);   // if no hi pulse continue waiting in while loop

     digitalWrite(ledPin,HIGH);

     delayMicroseconds (9100-us);

    digitalWrite(ledPin,LOW);

  }

  }

     while (us>9004){

       v = analogRead(vin);

     us= (424*v-216564);

      digitalWrite(ledPin,LOW);

      Serial.println(us);

  

     }

     while (us<100){

      v = analogRead(vin);

     us= (424*v-216564);

      digitalWrite(ledPin,HIGH);

      Serial.println(us);

  

     }

  }
Sorry Eric I don't understand what you mean with
Test.
C-like:
  Are you suggesting I add something here in my definitions at the top of the program?
 

Deleted member 115935

Joined Dec 31, 1969
0
How long does your println statement take ?
I'd suggest thats probably takes longer than one mains cycle,

you have a variable STATE, that i can not see set any where .
 
Top