/*
I was having issues printing doubles, so i used serial.println
given more time i would use the method in the api
*/
#include "api.h"
#include <stdio.h>
uint16_t timer = 0; //variable to track how long the ignition switch has been on for, assumption is that everyloop iteration is 1 second
double threshold = 180; //threshold temp for turning on the oven
double no_data = 999; //hardcoded variable for when the temperture reading can't be used
void setup() {
Serial.begin(115200);
setup_api();
//mock unit tests
//I don't know how to actually implement these with out doing some more research
//but theses are two example of cases to be tested and values to test them
//unit test for door sensor voltage inbetween 5V and ground
/*
sensor_voltage = 2000;
reference_voltage = 5000;
door_voltage = 2500;
*/
//unit test for dividing by zero when calcuating temp
/*
uint16_t sensor_voltage = 2000;
uint16_t reference_voltage = 0;
uint16_t door_voltage = 0;
*/
Serial.println("Elf oven 2000 starting up.");
serial_printf("Days without fire incident: %i\n", 0);
}
void loop() {
//read the various sensor voltages
uint16_t sensor_voltage = read_voltage(TEMPERATURE_SENSOR);
uint16_t reference_voltage = read_voltage(TEMPERATURE_SENSOR_REFERENCE);
uint16_t door_voltage = read_voltage(DOOR_SENSOR);
Serial.println("sensor_voltage " + String(sensor_voltage));
Serial.println("reference_voltage " + String(reference_voltage));
Serial.println("door_voltage " + String(door_voltage));
//linearly scale the refernce voltage so that the possible values for the reference voltages is 4v to 6V
//this was done to make the pot easier to operate was as ass the fact that the requirmentes state the normal
//opertaing voltage is 4.5V - 5.5V
//TODO: migrate this to the api and make read_voltage return this scaled value
double reference = 4000 + (double)read_voltage(TEMPERATURE_SENSOR_REFERENCE) * (2/5);
//open door or no reference voltage
//therefore oven is shut off, igntion timer is reset
if(door_voltage > 0 || reference ==0)
{
set_output(GAS_VALVE, 0);
set_output(IGNITER, 0);
timer = 0;
}
//door sensor indicates closed door
//therefore oven operates normally
else
{
double temp = calculate_temperature();
Serial.println("temp: " + String(temp));
//oven off: temperature is above the threshold or unreliable temperature data
if(temp > threshold || temp == no_data)
{
set_output(GAS_VALVE, 0);
set_output(IGNITER, 0);
timer = 0;
}
//oven on: temperature below threshold
else
{
//ignition has been on for less than 5 seconds
//therefore ignition and gas remain on, add second to timer
if(timer < 5)
{
set_output(GAS_VALVE, 1);
set_output(IGNITER, 1);
timer = timer + 1;
}
//ignition has been on for 5 seconds
//therefore ignition shut off, gas remain on, timer value remains for next loop iteration
else
{
set_output(GAS_VALVE, 1);
set_output(IGNITER, 0);
}
}
}
delay(1000);
}