Negative leg with FET to common -.
Here is another way to change power levels that combines elements of the
first and second programs. It is a manual PWM that may seem clunky but is
probaly more useful to people that have an inverter running all the time
and want to use a space heater or water heater as a dump load. The UNO's
PWM can not be used with solid state relays even if you slow it down a
bunch. Any time you mix two frequencies the result will be the sum and
the difference. With a zero crossing relay this becomes even more
complicated. One option is monitor the power line and fire the scr at a
certain phase angle. It is much easier to just take a time period like
a second or more and turn on for a percentage. It divides the time period
into 12 sections. At each count it checks the power level and turns on if
it matches. At the end it turns off if it is at less than full power. A
heating element will average this power. Some inverters control loop may
balk at a load being turned on and off at certain rates. The time period
may be lengthened or shortened in that event.
// FAN05
// This program flashes the LED pin# on a long PWM of a second
// or more Calculates the ramp time & ramp values to send out
// on LED pin 13. This is for heating elements working on line power.
int second = 0;
int count = 0;
int powerlevel = 0;
int ramptime = 0;
int endtime = 100; // quick test value
//int endtime = 4000; // 60 seconds X 66 minutes
void setup()
{
Serial.begin(9600); // setup serial
pinMode(13, OUTPUT); // sets the digital LED pin 13 as output
}
void loop() {
ramptime = 2 * endtime / 3; // make beginning of ramp 2/3 of endtime
if (count > 12) // 12 counts = 1 second
{ // this bracket indicates start of actions
// when IF is true
count = 0; // reset count
second = second + 1; // twelve counts = 1 second
} // end of those operations
delay(82); // waits for a 1/12 second
powerlevel = map (second, ramptime, endtime, 12, 0); // calculate ramp powerlevel
if (powerlevel < 0) powerlevel = 0; // insure powerlevel is within range
if (powerlevel > 12) powerlevel = 12;
// digitalWrite is used with digital pins. Do not confuse with analogWrite for PWM.
// The higher the powerlevel, the longer LED stays on.
if (count == 1 && powerlevel == 12)digitalWrite (13, 1) ; // turn fan on
if (count == 2 && powerlevel == 11)digitalWrite (13, 1) ; // turn fan on
if (count == 3 && powerlevel == 10)digitalWrite (13, 1) ; // turn fan on
if (count == 4 && powerlevel == 9) digitalWrite (13, 1) ; // turn fan on
if (count == 5 && powerlevel ==
digitalWrite (13, 1) ; // turn fan on
if (count == 6 && powerlevel == 7) digitalWrite (13, 1) ; // turn fan on
if (count == 7 && powerlevel == 6) digitalWrite (13, 1) ; // turn fan on
if (count == 8 && powerlevel == 5) digitalWrite (13, 1) ; // turn fan on
if (count == 9 && powerlevel == 4) digitalWrite (13, 1) ; // turn fan on
if (count == 10 && powerlevel == 3)digitalWrite (13, 1) ; // turn fan on
if (count == 11 && powerlevel == 2)digitalWrite (13, 1) ; // turn fan on
if (count == 12 && powerlevel == 1)digitalWrite (13, 1) ; // turn fan on
if (count == 0 && powerlevel < 12) digitalWrite (13, 0); // turn fan OFF at end of cycle
// when power level is less than 12
// SEND DATA TO SERIAL IN TOOLS
// Printing takes time. Comment out this in final working program.
// There are two options; every second or every count.
//if (count == 0) Serial.print(second); // debug value 1 per second
//if (count == 0) Serial.print("S Power level "); // debug value
//if (count == 0) Serial.println(powerlevel); // debug value
Serial.print(second); // debug value every count
Serial.print("S Power level "); // debug value every count
Serial.print(powerlevel); // debug value every count
Serial.print(" count= "); // debug value every count
Serial.println(count); // debug value every count
if (second > 32000) (second = 32000); // limit upper second count or use
//unsigned int or long for higher count
count = count + 1; // increment count
}