#define PERIOD 1 // control loop period [ms] - max 4194 ms !!!
boolean state = LOW;
unsigned int heater_time = 0;
void setup() {
cli(); // disable interrupts
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
pinMode(A0, INPUT);
Serial.begin(115200);
Serial1.begin(115200); // Serial1 is connected to the custom chip
// Configure Timer 4 to generate interrupt each PERIOD
// (CTC mode; 8 prescaler; enable interrupt on compare match)
TCCR4A = 0;
TCCR4B = (1 << WGM42) | (1 << CS41);
TCNT4 = 0;
// output compare register
OCR4A = (16000000 / 8) * PERIOD / 1000;
// Output Compare A Match Interrupt Enable
TIMSK4 = (1 << OCIE4A);
sei(); // enable interrupts
}
void loop() {
// nothing to do
delay(100);
}
// Control loop goes here
ISR(TIMER4_COMPA_vect)
{
char heater;
++heater_time;
if (heater_time == 20000) // 20s cycle
{
heater_time = 0;
}
if (heater_time < 10000) // turn the heater on for 10s, power increased lineary
{
heater = heater_time / 100;
}
else
{
heater = 0;
}
// print analog input every 0.5 s
if (heater_time % 500 == 0)
{
Serial1.write(heater);
float temperature = (float)analogRead(A0) * 50 / 1024;
Serial.println(temperature, 1); // print to serial console, one decimal place
}
}