/*!
\file lab1.ino
\date 2022-08-02
\author Fulvio Vivas <[email protected]>
\brief Temperature control program.
\par Copyright
Information contained herein is proprietary to and constitutes valuable
confidential trade secrets of Unicauca, and
is subject to restrictions on use and disclosure.
\par
Copyright (c) Unicauca 2022. All rights reserved.
\par
The copyright notices above do not evidence any actual or
intended publication of this material.
******************************************************************************
*/
#include "pinout.h"
/********************************************//**
* Temperature sensor control functions
***********************************************/
#include "DHT.h"
DHT dht(DHT11_PIN, DHTTYPE);
/********************************************//**
* LCD control functions
***********************************************/
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define DEBUG(a) Serial.print(millis()); Serial.print(": "); Serial.println(a);
/********************************************//**
* Asynchronous Task control functions
***********************************************/
#include "AsyncTaskLib.h"
void timeout_T1();
AsyncTask asyncTask1(2500,timeout_T1);
void measure_Temp();
AsyncTask asyncTask3(500, true, measure_Temp);
void seguridad(void);
AsyncTask asyncTask2(100, true, seguridad);
void Funct_asyncTask_luz(void);
AsyncTask asyncTask_luz(1000, true, Funct_asyncTask_luz);
void Funct_asyncTask_tyh(void);
AsyncTask asyncTask_tyh(1000, true, Funct_asyncTask_tyh);
void Funct_Timeout_5sec();
AsyncTask TaskTimeout_5sec(5000,Funct_Timeout_5sec);
void Funct_Timeout_3sec();
AsyncTask TaskTimeout_3sec(3000,Funct_Timeout_3sec);
/********************************************//**
* State Machine control functions
***********************************************/
#include "StateMachineLib.h"
/** An enum type.
* define states in State Machine
*/
#include "Keyboard.h"
#include <Keypad.h>
/*definicion de variables*/
//Contraseña
//inicializacion del lcd
//const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
//LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
string password = "2345";
//char password[] = {'1','2','3','4'};
int passwordVerify [4];
int numLetter = 0;
int gblCountMistakes = 0;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {24, 26, 28, 30};
byte colPins[COLS] = {32, 34, 36, 38};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// para el led
const int redPin = 7; // R petal on RGB LED module connected to digital pin 11
const int greenPin = 8; // G petal on RGB LED module connected to digital pin 9
const int bluePin = 9; // B petal on RGB LED module connected to digital pin 10
// State Alias
enum State
{
BLOQUEADO = 1,
//LOW_A = 1,
INICIAL = 0,
/* NORMAL_B = 2,
HIGH_C = 3,
ALERT_D = 4,
INIT = 5,*/
MONITOR_LUZ = 3,
MONITOR_TYH = 2,
ALARMA = 4
};
/** An enum type.
* define Inputs in State Machine
*/
enum Input
{
/*Sign_R = 0,
Sign_T = 1,
Sign_D = 2,
Sign_P = 3,
Unknown = 4,
clavecorrecta =5,
bloqueado =6,
timeout_3sec,
timeout_5sec*/
AVANZARP = 0,
AVANZARN = 1,
RETROCEDER = 2,
PRUEBA = 3,
RETROCEDERN = 5,
DESCONOCIDO = 4
};
/*! Stores last user input */
Input currentInput, input;
/*! Create new StateMachine 4 states and 6 transitions */
StateMachine stateMachine(5, 8);
/********************************************//**
* Define global variables
***********************************************/
int temp_value;
String inString = ""; // string to hold input
void setup() {
/* // put your setup code here, to run once:
Serial.begin(115200);
Serial.println ("INIT");
pinMode(LED_RED_PIN,OUTPUT);
pinMode(LED_GREEN_PIN,OUTPUT);
pinMode(LED_YELLOW_PIN,OUTPUT);
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("State Machine!");
dht.begin();
delay(1000);
//default values
digitalWrite(LED_RED_PIN,LOW); //cerrar valvula de oxigeno
digitalWrite(LED_GREEN_PIN,LOW); //cerrar valvula de oxigeno
digitalWrite(LED_YELLOW_PIN,LOW); //cerrar valvula de oxigeno
//Starting State Machine
Serial.println("Starting State Machine...");
setup_State_Machine();
Serial.println("Start Machine Started");
stateMachine.SetState(State::INIT, true, true);
//Starting Asynchronous Task
asyncTask3.Start();
DEBUG("START");*/
pinMode(R,OUTPUT);
pinMode(G,OUTPUT);
pinMode(B,OUTPUT);
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("welcome");
delay(1000);
lcd.clear();
Serial.begin(9600);
Serial.println("starting state machine...");
setup_State_Machine();
Serial.println("start machine started");
StateMachine.SetState(State:: INICIAL, false , true)
}
void loop() {
contrasenia.update();
tm5.update();
temperatura.update();
tm52.update();
luz.update();
tm3.update();
alarm.update();
tm6.update();
stateMachine.update();
/* // Update Asynchronous Task
asyncTask1.Update();
asyncTask3.Update();
//Read value by Serial port
read_data();
// Update State Machine
stateMachine.Update();
*/
}
/*void seguridad(void){
/*char strpwd[5] = {"R2D2"};
int n;
if (strncmp (strpwd,"R2xx",2) == 0)
{
input = clavecorrecta;
}
else{
input = bloqueado;
}
}*/
String writepassword(){
lcd.print("introduce");
lcd.setCursor(0.1);
lcd.print("contrasenia");
String result = "";
while(result.length()<4){
char key = keypad.getKey();
if(key >= '0' && key <= '9'){
lcd.print(key);
result += key;
}
}
lcd.clear();
return result;
}
void trypassword(){
int c = 0;
while(c<3){
if(writepassword().equals(password)){
digitalWrite(R, LOW);
digitalWrite(G, HIGH);
digitalWrite(B, LOW);
lcd.print("contrasenia correcta");
currentInput= Input:: AVANZARP;
c=5
}else{
if(c>=2){
digitalWrite(R, HIGH);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
lcd.print("muchos intentos");
currentInput = Input :: AVANZARN;
lcd.print(currentInput);
c=5;
}else{
digitalWrite(R, LOW);
digitalWrite(G, LOW);
digitalWrite(B, HIGH);
lcd.print("CON INCORRECTA");
delay(500);
lcd.setCursor(0,1);
lcd.print("REINTENTALO");
delay(1000);
lcd.clear();
c++;
}
}
}
}
/*F**************************************************************************
* NAME: timeout_T1
*----------------------------------------------------------------------------
* PARAMS:
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Define timeout_T1 function for to leaf the state ALERT_D
*----------------------------------------------------------------------------
* NOTE:
*
*****************************************************************************/
void timeout_T1(){
DEBUG("T1_END");
currentInput = Input::Sign_P;
}
/*F**************************************************************************
* NAME: measure_Temp
*----------------------------------------------------------------------------
* PARAMS:
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Measure temperature and humidity
*----------------------------------------------------------------------------
* NOTE:
*
*****************************************************************************/
void measure_Temp(){
DEBUG("TEMPERATURE");
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float value_Hum = dht.readHumidity();
// Read temperature as Celsius (the default)
float value_Temp = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(value_Hum) || isnan(value_Temp) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, value_Hum);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(value_Temp, value_Hum, false);
Serial.print(F("Humidity: "));
Serial.print(value_Hum);
Serial.print(F("% Temperature: "));
Serial.print(value_Temp);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
if (value_Temp > 30){
currentInput = Input::Sign_D;
}
lcd.setCursor(0, 1);
lcd.print("TEMP:");
lcd.print(value_Temp);
lcd.print(" ");
lcd.print("HUM:");
lcd.print(value_Hum);
}