Components and supplies
Arduino Nano R3
Relay Module (Generic)
HC-05 Bluetooth Module
Apps and platforms
Arduino IDE
Project description
Code
SerialTimer.zip
c_cpp
1inary file (no preview
SerialTimer.h
c_cpp
1/* -------------------------------------------------------------------------- */ 2/* Serial Timer */ 3/* -------------------------------------------------------------------------- */ 4/* 5Libreria para poder crear un temporizador apartir de la funcion millis 6el tiempo a poner esta en float por lo tanto puedes poner 0.5 lo que equivale a 730 segundos. 8 9 tiempo * 60s/1m 10 11 ejemplo 12 13 1.5min * 60s/1min = 90s 14 15*/ 16#ifndef SeriaTimer_h 17#define SerialTimer_h 18#include <Arduino.h> 19 20/* ---------------------------------- Clase --------------------------------- */ 21 22class Timer 23{ 24public: 25 Timer(int); 26 void set_timer(float, unsigned long); 27 void start_timer(void); 28 bool get_valor(void); 29 void switch_pin(bool); 30 bool get_pin(void); 31 float timer_register(void); 32 33private: 34 double isTime; 35 unsigned long _current, _past; 36 float Time; 37 int pin; 38 bool on_off = false; 39}; 40 41#endif
main.ino
c_cpp
1#include <string.h> 2#include "SerialTimer.h" 3//* Objects 4Timer 5 clock1(3); 6Timer clock2(4); 7Timer clock3(13); 8 9/* -------------------------------- 10 variables ------------------------------- */ 11String SerialData = ""; 12/* 13 -------------------------------------------------------------------------- */ 14 15/* 16 -------------------------------- funciones ------------------------------- */ 17void 18 menu(void); 19void rx_menu(int n_timers); 20void _n_timers(int __n); 21void timer_switch(int 22 isPin, float Time, bool serial_on_off, int mode); 23int pin_select(void); 24float 25 Read_time(void); 26String serialText(void); 27/* -------------------------------------------------------------------------- 28 */ 29 30void setup() 31{ 32 Serial.begin(9600); 33 menu(); 34} 35 36void 37 loop() 38{ 39 rx_menu(3); // menu to configure timers and configure how many 40 timers there are 41 42 clock1.get_valor(); // read if the time is complete 43 44 clock2.get_valor(); 45 clock3.get_valor(); 46} 47/* ---------------------Funciones--------------------- 48 */ 49void timer_switch(int isPin, float Time, bool serial_on_off, int mode) //Funcion 50 para los multiples timers 51{ //los 52 casos son a necesidad del usuario 53 unsigned long past = millis(); 54 switch 55 (mode) 56 { 57 case 0: 58 Serial.println("\ 59The timer has started"); 60 61 switch (isPin) // Aqui puedes agregar los casos que necesites 62 { 63 case 64 1: 65 clock1.set_timer(Time, past); 66 break; 67 case 2: 68 clock2.set_timer(Time, 69 past); 70 break; 71 case 3: 72 clock3.set_timer(Time, past); 73 74 break; 75 default: 76 Serial.println("Error"); 77 } 78 break; 79 80 case 1: 81 switch (isPin) // aqui tambien debes añadir el numero de casos 82 necesarios 83 { 84 case 1: 85 clock1.switch_pin(serial_on_off); 86 87 Serial.println(clock1.get_pin()); 88 break; 89 case 2: 90 clock2.switch_pin(serial_on_off); 91 92 Serial.println(clock2.get_pin()); 93 break; 94 case 3: 95 clock3.switch_pin(serial_on_off); 96 97 Serial.println(clock3.get_pin()); 98 break; 99 default: 100 Serial.println("Error"); 101 102 break; 103 } 104 break; 105 default: 106 Serial.println("Error"); 107 108 } 109} 110/* -------------------------------------------------------------------------- 111 */ 112void menu(void) 113{ 114 Serial.println("------------Timer-------------\ 115"); 116 117 Serial.println("1.Set Time\ 1182.ON_OFF Output\ 119"); 120 Serial.println("-------------------------------\ 121"); 122} 123 124String 125 serialText(void) // Funcion para poder obtener los datos ingresados 126{ 127 char 128 charData; 129 if (Serial.available() > 0) 130 { 131 charData = (char)Serial.read(); 132 133 SerialData += charData; 134 } 135 if (charData == '\ 136') 137 { 138 return 139 SerialData; 140 } 141 return ""; 142} 143 144float Read_time(void) // Funcion 145 que convierte los datos de String a Float 146{ 147 float time_out = 0; 148 String 149 time_text = serialText(); 150 if (time_text != "") 151 { 152 SerialData = 153 ""; 154 time_out = time_text.toFloat(); 155 Serial.println("Time: "); 156 157 Serial.print(time_out); 158 Serial.print(" minutes\ 159"); 160 time_text 161 = ""; 162 } 163 return time_out; 164} 165 166int pin_select(void) // Funcion 167 que convierte los datos de String a Int 168{ 169 int pin_out = 100; 170 String 171 pin_text = serialText(); 172 if (pin_text != "") 173 { 174 SerialData = ""; 175 176 pin_out = pin_text.toInt(); 177 if (pin_out > 0) 178 { 179 Serial.println("Selected 180 timer: "); 181 182 Serial.print(pin_out); 183 } 184 pin_text = ""; 185 186 } 187 return pin_out; 188} 189 190void _n_timers(int __n) //Funcion para imprimir 191{ 192 //el numero de objetos 193 for (int z = 1; z <= __n; z++) 194 195 { 196 Serial.print(z); 197 Serial.print(".Timer "); 198 Serial.print(z); 199 200 Serial.println(""); 201 } 202} 203 204/* -------------------------------------------------------------------------- 205 */ 206 207void rx_menu(int n_timers) // Menu de control por Serial 208{ 209 int 210 isPin; 211 float Time; 212 int serial_on_off; 213 char _command; 214 bool flag 215 = false; 216 217 if (Serial.available() > 0) // si el serial esta disponible 218 219 { 220 _command = (char)Serial.read(); // guarda el caracter en _command 221 222 flag = true; // Pon en true la flag 223 } 224 if (flag 225 == true) // si esta es true activa el menu 226 { 227 flag = false; // para 228 prevnir futuros bucles pasa a falso 229 switch (_command) // si el caracter tiene 230 algun sub-menu 231 { 232 case '1': // Seleccion para asignar timer 233 Serial.println("Select 234 a timer"); 235 _n_timers(n_timers); 236 while (true) 237 { 238 isPin 239 = pin_select(); 240 if (isPin > 0 && isPin != 100) //Hasta que el valor de 241 isPin 242 { //sea correcto terminara el bucle 243 infinito 244 break; 245 } 246 } 247 Serial.println("\ 248Enter 249 time:"); //Asignacion de tiempo al timer seleccionado 250 while (true) 251 252 { 253 Time = Read_time(); 254 if (Time > 0.0) // Hasta que sea 255 un valor valido terminara el bucle infinito 256 { 257 timer_switch(isPin, 258 Time, 0, 0); // Los datos son asignados 259 menu(); 260 break; 261 262 } 263 } 264 break; 265 case '2': // Cambiar el estado de una 266 salida 267 Serial.println("Select a timer"); 268 _n_timers(n_timers); 269 270 while (true) 271 { 272 isPin = pin_select(); //Hasta que 273 el valor sea valido terminara 274 if (isPin > 0 && isPin < 100) // el bucle 275 infinito 276 { 277 break; 278 } 279 } 280 Serial.println("\ 2810.OFF\ 2821.ON"); 283 284 while (true) 285 { 286 serial_on_off = pin_select(); 287 if 288 (serial_on_off == 0 || serial_on_off == 1) //Si los valores son validos 289 { 290 291 Serial.println("\ 292Output is on: "); 293 timer_switch(isPin, 294 Time, serial_on_off, 1); //Se asignan los valores 295 menu(); // 296 y termina el bucle infinito 297 break; 298 } 299 300 } 301 302 303 break; 304 } 305 } 306} 307
SerialTimer.h
c_cpp
1/* -------------------------------------------------------------------------- 2 */ 3/* Serial Timer */ 4/* 5 -------------------------------------------------------------------------- */ 6/* 7Libreria 8 para poder crear un temporizador apartir de la funcion millis 9el tiempo a poner 10 esta en float por lo tanto puedes poner 0.5 lo que equivale a 1130 segundos. 12 13 14 tiempo * 60s/1m 15 16 ejemplo 17 18 1.5min * 60s/1min = 90s 19 20*/ 21#ifndef 22 SeriaTimer_h 23#define SerialTimer_h 24#include <Arduino.h> 25 26/* ---------------------------------- 27 Clase --------------------------------- */ 28 29class Timer 30{ 31public: 32 33 Timer(int); 34 void set_timer(float, unsigned long); 35 void start_timer(void); 36 37 bool get_valor(void); 38 void switch_pin(bool); 39 bool get_pin(void); 40 41 float timer_register(void); 42 43private: 44 double isTime; 45 unsigned 46 long _current, _past; 47 float Time; 48 int pin; 49 bool on_off = false; 50}; 51 52#endif
SerialTimer.cpp
c_cpp
1#include <Arduino.h> 2#include "SerialTimer.h" 3/* -------------------------- 4 Funciones de la clase ------------------------- */ 5 6Timer::Timer(int _pin) 7{ 8 9 pin = _pin; 10 pinMode(pin, OUTPUT); 11} 12 13void Timer::start_timer(void) 14{ 15 16 if ((_current - _past >= isTime) && isTime > 0) 17 { 18 Serial.println("Time 19 finished"); 20 digitalWrite(pin, !digitalRead(pin)); 21 on_off 22 = true; 23 isTime = 0; 24 } 25} 26 27float Timer::timer_register() 28{ 29 30 return isTime / 60000; 31} 32 33void Timer::set_timer(float time, unsigned 34 long isPast) 35{ 36 isTime = time * 60000; 37 _past = isPast; 38} 39 40bool 41 Timer::get_valor(void) 42{ 43 on_off = false; 44 _current = millis(); 45 46 start_timer(); 47 if (on_off == true) 48 { 49 return true; 50 51 } 52 else 53 { 54 return false; 55 } 56} 57 58void Timer::switch_pin(bool 59 estado) 60{ 61 digitalWrite(pin, estado); 62} 63 64bool Timer::get_pin(void) 65{ 66 67 bool _status; 68 _status = digitalRead(pin); 69 return _status; 70} 71
SerialTimer.zip
c_cpp
1inary file (no preview
main.ino
c_cpp
1#include <string.h> 2#include "SerialTimer.h" 3//* Objects 4Timer clock1(3); 5Timer clock2(4); 6Timer clock3(13); 7 8/* -------------------------------- variables ------------------------------- */ 9String SerialData = ""; 10/* -------------------------------------------------------------------------- */ 11 12/* -------------------------------- funciones ------------------------------- */ 13void menu(void); 14void rx_menu(int n_timers); 15void _n_timers(int __n); 16void timer_switch(int isPin, float Time, bool serial_on_off, int mode); 17int pin_select(void); 18float Read_time(void); 19String serialText(void); 20/* -------------------------------------------------------------------------- */ 21 22void setup() 23{ 24 Serial.begin(9600); 25 menu(); 26} 27 28void loop() 29{ 30 rx_menu(3); // menu to configure timers and configure how many timers there are 31 32 clock1.get_valor(); // read if the time is complete 33 clock2.get_valor(); 34 clock3.get_valor(); 35} 36/* ---------------------Funciones--------------------- */ 37void timer_switch(int isPin, float Time, bool serial_on_off, int mode) //Funcion para los multiples timers 38{ //los casos son a necesidad del usuario 39 unsigned long past = millis(); 40 switch (mode) 41 { 42 case 0: 43 Serial.println("\ 44The timer has started"); 45 switch (isPin) // Aqui puedes agregar los casos que necesites 46 { 47 case 1: 48 clock1.set_timer(Time, past); 49 break; 50 case 2: 51 clock2.set_timer(Time, past); 52 break; 53 case 3: 54 clock3.set_timer(Time, past); 55 break; 56 default: 57 Serial.println("Error"); 58 } 59 break; 60 case 1: 61 switch (isPin) // aqui tambien debes añadir el numero de casos necesarios 62 { 63 case 1: 64 clock1.switch_pin(serial_on_off); 65 Serial.println(clock1.get_pin()); 66 break; 67 case 2: 68 clock2.switch_pin(serial_on_off); 69 Serial.println(clock2.get_pin()); 70 break; 71 case 3: 72 clock3.switch_pin(serial_on_off); 73 Serial.println(clock3.get_pin()); 74 break; 75 default: 76 Serial.println("Error"); 77 break; 78 } 79 break; 80 default: 81 Serial.println("Error"); 82 } 83} 84/* -------------------------------------------------------------------------- */ 85void menu(void) 86{ 87 Serial.println("------------Timer-------------\ 88"); 89 Serial.println("1.Set Time\ 902.ON_OFF Output\ 91"); 92 Serial.println("-------------------------------\ 93"); 94} 95 96String serialText(void) // Funcion para poder obtener los datos ingresados 97{ 98 char charData; 99 if (Serial.available() > 0) 100 { 101 charData = (char)Serial.read(); 102 SerialData += charData; 103 } 104 if (charData == '\n') 105 { 106 return SerialData; 107 } 108 return ""; 109} 110 111float Read_time(void) // Funcion que convierte los datos de String a Float 112{ 113 float time_out = 0; 114 String time_text = serialText(); 115 if (time_text != "") 116 { 117 SerialData = ""; 118 time_out = time_text.toFloat(); 119 Serial.println("Time: "); 120 Serial.print(time_out); 121 Serial.print(" minutes\ 122"); 123 time_text = ""; 124 } 125 return time_out; 126} 127 128int pin_select(void) // Funcion que convierte los datos de String a Int 129{ 130 int pin_out = 100; 131 String pin_text = serialText(); 132 if (pin_text != "") 133 { 134 SerialData = ""; 135 pin_out = pin_text.toInt(); 136 if (pin_out > 0) 137 { 138 Serial.println("Selected timer: "); 139 140 Serial.print(pin_out); 141 } 142 pin_text = ""; 143 } 144 return pin_out; 145} 146 147void _n_timers(int __n) //Funcion para imprimir 148{ //el numero de objetos 149 for (int z = 1; z <= __n; z++) 150 { 151 Serial.print(z); 152 Serial.print(".Timer "); 153 Serial.print(z); 154 Serial.println(""); 155 } 156} 157 158/* -------------------------------------------------------------------------- */ 159 160void rx_menu(int n_timers) // Menu de control por Serial 161{ 162 int isPin; 163 float Time; 164 int serial_on_off; 165 char _command; 166 bool flag = false; 167 168 if (Serial.available() > 0) // si el serial esta disponible 169 { 170 _command = (char)Serial.read(); // guarda el caracter en _command 171 flag = true; // Pon en true la flag 172 } 173 if (flag == true) // si esta es true activa el menu 174 { 175 flag = false; // para prevnir futuros bucles pasa a falso 176 switch (_command) // si el caracter tiene algun sub-menu 177 { 178 case '1': // Seleccion para asignar timer 179 Serial.println("Select a timer"); 180 _n_timers(n_timers); 181 while (true) 182 { 183 isPin = pin_select(); 184 if (isPin > 0 && isPin != 100) //Hasta que el valor de isPin 185 { //sea correcto terminara el bucle infinito 186 break; 187 } 188 } 189 Serial.println("\ 190Enter time:"); //Asignacion de tiempo al timer seleccionado 191 while (true) 192 { 193 Time = Read_time(); 194 if (Time > 0.0) // Hasta que sea un valor valido terminara el bucle infinito 195 { 196 timer_switch(isPin, Time, 0, 0); // Los datos son asignados 197 menu(); 198 break; 199 } 200 } 201 break; 202 case '2': // Cambiar el estado de una salida 203 Serial.println("Select a timer"); 204 _n_timers(n_timers); 205 while (true) 206 { 207 isPin = pin_select(); //Hasta que el valor sea valido terminara 208 if (isPin > 0 && isPin < 100) // el bucle infinito 209 { 210 break; 211 } 212 } 213 Serial.println("\ 2140.OFF\ 2151.ON"); 216 while (true) 217 { 218 serial_on_off = pin_select(); 219 if (serial_on_off == 0 || serial_on_off == 1) //Si los valores son validos 220 { 221 Serial.println("\ 222Output is on: "); 223 timer_switch(isPin, Time, serial_on_off, 1); //Se asignan los valores 224 menu(); // y termina el bucle infinito 225 break; 226 } 227 228 } 229 230 break; 231 } 232 } 233} 234
SerialTimer.cpp
c_cpp
1#include <Arduino.h> 2#include "SerialTimer.h" 3/* -------------------------- Funciones de la clase ------------------------- */ 4 5Timer::Timer(int _pin) 6{ 7 pin = _pin; 8 pinMode(pin, OUTPUT); 9} 10 11void Timer::start_timer(void) 12{ 13 if ((_current - _past >= isTime) && isTime > 0) 14 { 15 Serial.println("Time finished"); 16 digitalWrite(pin, !digitalRead(pin)); 17 on_off = true; 18 isTime = 0; 19 } 20} 21 22float Timer::timer_register() 23{ 24 return isTime / 60000; 25} 26 27void Timer::set_timer(float time, unsigned long isPast) 28{ 29 isTime = time * 60000; 30 _past = isPast; 31} 32 33bool Timer::get_valor(void) 34{ 35 on_off = false; 36 _current = millis(); 37 start_timer(); 38 if (on_off == true) 39 { 40 return true; 41 } 42 else 43 { 44 return false; 45 } 46} 47 48void Timer::switch_pin(bool estado) 49{ 50 digitalWrite(pin, estado); 51} 52 53bool Timer::get_pin(void) 54{ 55 bool _status; 56 _status = digitalRead(pin); 57 return _status; 58} 59
Downloadable files
circuit_YyUUv66KRH.jpg
circuit_YyUUv66KRH.jpg
Circuit
Circuit
circuit_YyUUv66KRH.jpg
circuit_YyUUv66KRH.jpg
Comments
Only logged in users can leave comments