/*
We need your help to stop forest fires and bake tasty cookies!
See `requirements.md` for how to help.
Then check `notes.md`.
*/
#include "api.h"
bool initialisation_complete = false; // inhibit any interrupts until initialisation is complete
volatile bool ovn_opn = false; // intially oven door is closed
void setup() {
Serial.begin(115200);
setup_api();
Serial.println("Elf oven 2000 starting up.");
serial_printf("Days without fire incident: %i\n", 0);
initialisation_complete = true; // open interrupt processing for business
// Test 1
analogWrite(A1, 0);
}
void door_sensor_interrupt_handler(bool voltage_high)
{
// TODO: implement
if (initialisation_complete == true)
{
if ((voltage_high))
{
serial_printf("Oven shutting down...oven door open (interrupt: %i)\n", voltage_high);
set_output(IGNITER, 0); // turn off IGNITER
set_output(GAS_VALVE, 0); // turn off GAS_VALVE
ovn_opn = true;
}
}
}
void start_oven(bool switch_oven)
{
if ((switch_oven))
{
serial_printf("switch_oven: %i\n", switch_oven);
set_output(GAS_VALVE, 1); // turn on GAS_VALVE
set_output(IGNITER, 1); // turn on IGNITER
delay(500); // Need 5s delay >> change to 5000
set_output(IGNITER, 0); // turn off IGNITER
}
else
{
serial_printf("switch_oven: %i\n", switch_oven);
set_output(GAS_VALVE, 0); // turn off GAS_VALVE
set_output(IGNITER, 0); // turn off IGNITER
}
}
void loop() {
// Some example code below to help show how to use API.
// Please delete it and replace with your own code.
uint16_t sensor_voltage = read_voltage(TEMPERATURE_SENSOR);
uint16_t vref = read_voltage(TEMPERATURE_SENSOR_REFERENCE);
float temp; // Assume this is enough accuracy float selected
float opt_temp = 180;
uint16_t vref_min = 4500; // min thershold vref
uint16_t vref_max = 5500; // max thershold vref
serial_printf("sensor_voltage %i\n", sensor_voltage);
serial_printf("vref %i\n", vref);
if ((vref >= vref_min) && (vref <= vref_max)) // check vref range
{
if ((sensor_voltage>=vref*0.1) && (sensor_voltage<=vref*0.9)) // check linear temperature range
{
temp = (sensor_voltage-vref*0.1)*310/(vref*0.9-vref*0.1) - 10;
Serial.println(temp);
if (temp<opt_temp)
{
if (ovn_opn)
{
ovn_opn = false; // Reset the interrupt flag
Serial.println("Oven door closed. Oven heating mode...");
}
start_oven(1);
}
else if (temp>opt_temp)
{
start_oven(0);
}
}
else // if not linear temperature range
{
Serial.println("unstable temperature. Oven shutting down...");
start_oven(0);
}
}
else // if not vref range
{
Serial.println("Oven is not operating properly. Unstable temperature. Oven shutting down...");
start_oven(0);
}
//set_output(GAS_VALVE, sensor_voltage > 2000);
//set_output(IGNITER, 1); // turn on IGNITER
//delay(500); // Need 5s delay >> change to 5000
//set_output(IGNITER, 0); // turn off IGNITER
delay(1000); // feel free to change. What would you use for an actual iteration period?
}