Skip to content
Snippets Groups Projects
Commit 1481fadb authored by Sebastián Fernández's avatar Sebastián Fernández
Browse files

se agrega EEPROM y TIC con timer

parent c81c1614
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,9 @@
* v0.1
*
*
* Timer idea
* https://techtutorialsx.com/2017/10/07/esp32-arduino-timer-interrupts/
*
* LedControl library
* Instalarla y editar
* #include <avr/pgmspace.h>
......@@ -16,8 +19,8 @@
*/
#include <pgmspace.h>
#include <EEPROM.h>
#include <WiFi.h> // WiFi control for ESP32
/*
Editar "LedControl.h" y cambiar
# include <avr\pgmspace.h>
......@@ -30,11 +33,11 @@ por
*/
#include "LedControl.h" // 8 digit 7 segment. MAX
#include <WiFi.h> // WiFi control for ESP32
/****************************************
* Aplication parameters
* User parameters
****************************************/
// WiFi access point
#define WIFI_AP_NAME "este"
......@@ -45,9 +48,6 @@ int radiation_power_theorical = 20; // mW/cm2
//int target_dose = 2500; // mJ/cm2
int target_dose = 200; // mJ/cm2
// Main application loop delay
int quant = 100; //ms
// Period of sending information
//int send_delay = 600000; //10min = 10*60*1000 ms
int send_delay = 1000; //1seg = 1000 ms
......@@ -56,10 +56,23 @@ int send_delay = 1000; //1seg = 1000 ms
/****************************************
* Platform parameters
****************************************/
// Main application loop timers
int quant_passed = 0;
//const int quants_per_sec = 1000 / quant;
const int quants_per_sec = 1000 / quant;
// Main application loop delay
const int timer_mhz = 80;
const int tic_period = 100; //ms
const int timer_constant = tic_period*1000; //us 100000
int tics_passed = 0;
int time_on_tics = 0;
const int tics_per_sec = 1000 / tic_period;
const int tics_per_minute = 60*tics_per_sec;
const int tics_per_dec_minute = 10*tics_per_minute;
int tic = LOW;
volatile int interruptCounter;
int totalInterruptCounter;
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
// Baud rate for debug serial
#define SERIAL_DEBUG_BAUD 115200
......@@ -81,22 +94,40 @@ const int alarm_led_pin = 26;
const int alarm_buz_pin = 25;
int alarm_state = LOW;
// load detection
const int load_status_pin = 33;
int load_present = LOW;
// load position sensor
const int back_position_pin = 32;
int back_position = LOW; // HIGH when present
const int front_position_pin = 33;
int front_position = LOW; // HIGH when present
// Period of sending information
// Period for sending information
int send_passed = 0; // Time passed after data was sent, milliseconds.
// EEPROM
// define the number of byte needed
#define ADD_POWER 0 // address 0 and 1 store radiation power (16 bits)
#define ADD_ON_TIME 2 // address 1 and 2 store TIME on decens of minutes (16 bits)
#define ADD_CAL_1 4 // address 3 and 4 store Calibration parameter 1 (16 bits)
#define ADD_CAL_2 6 // address 5 and 6 store Calibration parameter 1 (16 bits)
#define EEPROM_SIZE 8 //
// UV metering
int radiation_power = radiation_power_theorical;
int total_desinfection_time = target_dose/radiation_power ; // seconds
int total_desinfection_time = target_dose/radiation_power; // seconds
int remaining_desinfection_time = total_desinfection_time;
// States
enum State_enum {POWER_UP, NO_LOAD_PRESENT, DOSE_CALCULATION, DOSE_APPLY, DOSE_END};
uint8_t state = POWER_UP;
int unload_complete = LOW; // flag to detect unload has been done.
void IRAM_ATTR onTimer() {
portENTER_CRITICAL_ISR(&timerMux);
tic = HIGH;
portEXIT_CRITICAL_ISR(&timerMux);
}
/****************************************
* SETUP
......@@ -115,9 +146,24 @@ pinMode(alarm_led_pin, OUTPUT);
ledcSetup(0,1E5,12);
ledcAttachPin(alarm_buz_pin,0);
// init load sensor pin
pinMode(load_status_pin, INPUT);
// init sensor pins
pinMode(back_position_pin, INPUT);
pinMode(front_position_pin, INPUT);
// Initialize EEPROM
EEPROM.begin(EEPROM_SIZE);
// Init values from EEPROM
// sss resolver primera vez
radiation_power = radiation_power_theorical; // sss cambiar por lecturas a eeprom
total_desinfection_time = target_dose/radiation_power; // sss cambiar por lecturas a eeprom
// Initialize TIC timer
timer = timerBegin(0, timer_mhz, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, timer_constant, true);
timerAlarmEnable(timer);
// Initialize Wifi
// Serial.println("==== wifi setup ====");
// WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
......@@ -130,71 +176,95 @@ pinMode(load_status_pin, INPUT);
****************************************/
void loop() {
delay(quant);
if (checkLoadInFrontSwitch() == HIGH){
measureRadiationPower(); // time to measure radiation and calculate dose time
if (state == DOSE_END){
// in DOSE END state waiting for unload complete
unload_complete= HIGH;
}
}
//delay(tic_period);
//tic = HIGH;
if (tic){
portENTER_CRITICAL(&timerMux);
tic = LOW;
portEXIT_CRITICAL(&timerMux);
on_tic();
}
}
void on_tic(){
time_on_tics += 1;
if (time_on_tics % tics_per_minute == 0){
update_time_on();
}
switch(state)
{
///////////////////////////////////////
case POWER_UP:
Serial.println("state: POWER_UP");
if(loadPresent()==LOW){
if(checkLoadPresent()==LOW){ // wait no load to start normal operation
state = NO_LOAD_PRESENT;
}
lc.clearDisplay(0);
// Get radiation power and show it.
radiation_power = getRadiationPower();
radiation_power = currentRadiationPower();
snprintf(digitos_disp1,5,"%04d", radiation_power);
displayRadiationPower(digitos_disp1);
// Remaining time display is off
break;
///////////////////////////////////////
case NO_LOAD_PRESENT:
Serial.println("state: NO_LOAD_PRESENT");
if(loadPresent()==HIGH){
state = DOSE_CALCULATION;
if(checkLoadPresent()==HIGH){ // wait load present
state = DOSE_APPLY;
tics_passed = 0;
remaining_desinfection_time = total_desinfection_time;
}
lc.clearDisplay(0);
// Get radiation power and show it.
radiation_power = getRadiationPower();
radiation_power = currentRadiationPower();
snprintf(digitos_disp1,5,"%04d", radiation_power);
displayRadiationPower(digitos_disp1);
break;
///////////////////////////////////////
case DOSE_CALCULATION:
Serial.println("state: DOSE_CALCULATION");
state = DOSE_APPLY;
quant_passed = 0;
remaining_desinfection_time = total_desinfection_time;
break;
///////////////////////////////////////
case DOSE_APPLY:
Serial.println("state: DOSE_APPLY");
//Serial.println("state: DOSE_APPLY");
quant_passed += 1;
tics_passed += 1;
if (quant_passed >= quants_per_sec){
if (tics_passed >= tics_per_sec){
remaining_desinfection_time -=1;
quant_passed = 0;
tics_passed = 0;
}
if(loadPresent()==LOW){
if(checkLoadPresent()==LOW){ // cancel if door opened
state = NO_LOAD_PRESENT;
} else if (remaining_desinfection_time == 0){
state = DOSE_END;
quant_passed = 0;
unload_complete = LOW;
tics_passed = 0;
alarm_state = LOW;
}
// Get radiation power and show it.
radiation_power = getRadiationPower();
radiation_power = currentRadiationPower();
snprintf(digitos_disp1,5,"%04d", radiation_power);
displayRadiationPower(digitos_disp1);
// Show remaining time
......@@ -205,12 +275,12 @@ switch(state)
///////////////////////////////////////
case DOSE_END:
Serial.println("state: DOSE_END");
//Serial.println("state: DOSE_END");
quant_passed += 1;
tics_passed += 1;
if (quant_passed >= quants_per_sec){
quant_passed = 0;
if (tics_passed >= tics_per_sec){
tics_passed = 0;
alarm_state = not(alarm_state);
digitalWrite(alarm_led_pin, alarm_state);
......@@ -228,7 +298,7 @@ switch(state)
}
if(loadPresent()==LOW){
if(unload_complete == HIGH){ //
state = NO_LOAD_PRESENT;
ledcWriteTone(0,0);
digitalWrite(alarm_led_pin, 0);
......@@ -251,31 +321,52 @@ switch(state)
* AUX FUNCTIONS
****************************************/
int loadPresent()
int checkLoadPresent()
// detect if both: front and back sensor high
{
// HIGH if load to germinize present
return digitalRead(load_status_pin);
int back_position, front_position;
front_position = digitalRead(front_position_pin);
back_position = digitalRead(back_position_pin);
return (front_position and back_position);
}
int getRadiationPower()
int checkLoadInFrontSwitch()
// detect if only front sensor high
{
int back_position, front_position;
front_position = digitalRead(front_position_pin); // sss cambiar. el mismo solo para debug
back_position = digitalRead(back_position_pin);
return (front_position and !back_position);
}
void update_time_on()
{
Serial.println("Updating Time On.");
}
int measureRadiationPower()
{
Serial.println("measureRadiationPower!!!!!!!!!!");
radiation_power = radiation_power_theorical; ///sss cambiar por funcion que lee sensor
total_desinfection_time = target_dose/radiation_power;
return radiation_power_theorical;
}
int currentRadiationPower()
{
return radiation_power_theorical;
}
void displayRadiationPower(const char *chars4) {
//lc.clearDisplay(0);
//for (int i = 0; i < 5 && chars4[i] != 0; i++) {
for (int i = 0; i < 4; i++) {
lc.setChar(0, 7 - i, chars4[i], false);
}
}
void displayRemainingDesinfectionTime(const char *chars4) {
//lc.clearDisplay(0);
for (int i = 0; i < 4; i++) {
Serial.print(i);
Serial.print("-->");
Serial.println(chars4[i]);
lc.setChar(0, 3 - i, chars4[i], false);
}
}
......@@ -307,6 +398,8 @@ void reconnect() {
}
}
/****************************************
* Trash
****************************************/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment