Can someone please edit this entire code so that the heater my micro arduino is connected to will turn on for 5 seconds when the button is pressed and turn off again when you press the button? Can you also please ensure the thermistor will read the temperature of the heater and control it so it does not go above 100 deg C? Thank you!!!
// Proof of Concept Heating Mode
#include <Bounce2.h>
#include <PID_v1.h>
// I/O Definition
const int THERMISTOR_PIN = A1; Does there need to be a thermistor implemented???
const int HEAT_PIN = 7;
const int BUTTON_PIN = 8;
//State variables
enum modes {
HEAT,
OFF
};
enum modes currentMode = OFF;
//Global vars
const int TEMP_MAX = 100; //Celsius What max temp do we want the heater to be????
const int RANGE = 1; //Celsius
const int DELAY = 500; //ms - for LED flash
unsigned long pulseStart = 0; //ms-timer for LED flash
unsigned long tempPulseStart = 0; //ms timer for ADC
const int ADC_DELAY = 50; //ms
double rollingAvgTemp = 25.0; //Celsius
double pidHotOutput = 0; //global for PID output (hot mode)
double pidHotTarget = double(TEMP_MAX); //Celsius
PID pidHotController(&rollingAvgTemp, &pidHotOutput, &pidHotTarget,2000, 20, 0, DIRECT);
//Debouncer setup
Bounce debouncer = Bounce();
void setup(){
//set up code here to run once
pinMode(HEAT_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLDOWN);
analogReference(DEFAULT); // use default 5V ref
debouncer.attach(BUTTON_PIN, INPUT_PULLDOWN);
debouncer.interval(50); // why 50???
pidHotController.SetOutputLimits(0, pidWindow); //PID outputs how long we should be on this cycle
Serial.begin(9600); // why 9600???
}
void loop () {
//main code here to run repeatadly
debouncer.update();
if(debouncer.fell() ){
isRunning = !isRunning;
Serial.println("mode toggle");
}
if (isRunning){
run_system();
}
else {
digitalWrite(HEAT_PIN, LOW); // heating element off
}
delay(50);
}
/////////////////////////////////////////////////////////////////////////
// run_system
//
// run the control system
// basic on/off control system with hysteresis
////////////////////////////////////////////////////////////////////////
void run_system(){
read_thermistor();
//very crude two-level hi-low-off control
if (rollingAvgTemp > TEMP_MAX){
analogWrite(HEAT_PIN, 0); //off if exceeds set temperature
Serial.println("Power: 0");
}
else if ((rollingAvgTemp >= (TEMP_MAX - RANGE)) & (rollingAvgTemp <= TEMP_MAX)){
analogWrite(HEAT_PIN, 25); //10% power when in the set temp range
Serial.println("Power: 25");
}
else if (rollingAvgTemp < (TEMP_MAX-RANGE)){
analogWrite(HEAT_PIN, 255); //full power
Serial.println("Power: 255");
}
else {
analogWrite(HEAT_PIN, 0); //catch all - turn off if in weird state
}
Serial.print(" Temp: ");
Serial.print(rollingAvgTemp);
Serial.print("\n");
}
///////////////////////////////////////////////////////////////////////////
// read_thermistor
//
// Reads the value of the thermistor and converts to degrees C
// ADC reads are rate limited using non-blocking timers
// Returns a running average to minimize noise
//////////////////////////////////////////////////////////////////////////
void read_thermistor(){
// ____________ (MF52C1103F3380) Thermistor
static const float T0 = 298.15; //T0 in K (per datasheet)
static const float B = 3380.0; //B in K (per datasheet)
static const float R0 = 10000.0; //Thermistor resistance at 25C
static const float R1 = 9807.00; //R used in voltage divider
float temp_C = 0.0;
float temp_C_avg = 0.0;
if((millis() - tempPulseStart) > ADC_DELAY){
//calculate temperature reading
float raw_value = analogRead(THERMISTOR_PIN);
float resistance = ((R1 * (1023.00 - raw_value)) / raw_value);
}
}

