I've been preparing to dive into electric home brewing, where the kettles are heated with an electric heating element rather than a gas fire.
The purpose is so I can control the heating element, making it much easier to automate and maintain specific temperatures.
My goal with this exercise was to define a frequency (in milliseconds) and a duty cycle as a percentage (defined as a float, 0.0 to 1.0), then blink an LED to simulate a heating element firing.
In the end, rather than blinking an LED, the digital signal will go to a solid state relay which will be attached to the heating element.
float dutyCycle = .75; // Represents a duty cycle of 75%
int frequency = 3000; // Frequency in milliseconds. 50% duty cycle at a frequency of 3000 is 1.5 seconds on, 1.5 seconds off
int led = 9; // LED pin for simulation
void setup()
{
pinMode(led, OUTPUT);
}
void loop()
{
if (dutyCycle > 0.0) {
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
int on = frequency * dutyCycle;
int off = frequency - on;
delay(on);
if (off > 0) {
digitalWrite(led, LOW);
delay(off);
}
}