Temperature sensing phase control

Here is the code for temperature sensing phase control.

bool state=LOW;
int pin=10;// this pin connects to zero crossing detector circuit

int v=540;      // sets low at start of program until analog read at 1 minute
int vin = 2;   // this is the analog read pin
int us=12396;        // sets phase control turn on points
int ledPin = 11;   // pin 8 connects to random phase opto 
unsigned long previousMillis=0;

const long interval = 15000;
void setup() {
  Serial.begin(9600);
    pinMode(pin,INPUT);
    pinMode(ledPin, OUTPUT);}
   void loop() {
       unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
        v = analogRead(vin);
        us= (424*v-216564);
        Serial.println(v);
        Serial.println(us);
}
   

        while (us<=9004 && us>=100){ //these readings are in temp control range
           unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
        v = analogRead(vin);
        us= (424*v-216564);
    Serial.println(v);
        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);
  }
    }
    if (v>532){          // turns off power when maximum temp exceeded
       Serial.println(v);
          digitalWrite(ledPin,LOW);}
          if (v<511){             // turns on full power below minimum temp
             Serial.println(v);
           digitalWrite(ledPin,HIGH);}
    }
  
          


             
        ```
type or paste code here

Analog read on pin 2?

Post an annotated schematic showing exactly how you have wired this. Show all connections, power source, ground. etc. Links to technical information on each of the hardware items helps a lot. Trouble shooting a block of code without knowing what the hardware is very time consuming.

You should use long arithmetic here:

us = (424L * v - 216564); // Is it 216664?

Be aware that the result will be positive only when v >=511. If you call delayMicroseconds with a negative argument you will surely not get what you think.

What does that formula mean?


I know I should have shown the hardware. I did the same thing with analog electronics. I actually used the beetle controller because it was so small. The diagram shows a ltspice simulation. On the left is a zero crossing voltage detector which picks up when the mains goes low. I had to add the zener to clamp the high voltage which damaged the transistor and bridge otherwise.
Use a moc3023 or other random phase triac optocoupler. I use a ntc thermistor in series with a pot to set the desired temperature. There are only 3 analog and 3 digital pins on the beetle. The maths was necessary to convert analog voltage readings to delays in the phase controller. At 50cps it takes 10ms or 1000us for 1 half cycle.
I get a range of 1.5 centigrade between on and off. Easily adjusted at the analog input with thermistor and pot. I don't need any assistance with this as it is a completed project and works great. With the values on the LTspice simulation you will get a 50% duty cycle.

1 Like