Created
July 22, 2023 08:06
-
-
Save Emgicraft/b54b93d76e3dcb05ebdbd13230e13b6f to your computer and use it in GitHub Desktop.
C04 CAF2 Proyecto Final: Código del proyecto Arduino con el qué se cronometró los tiempos de respuesta y funcionamiento de los electroimanes, así como su control por medio de pulsadores.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Definición de los pines | |
const int pinBtn1 = 2; | |
const int pinBtn2 = 3; | |
const int pinSB1 = 4; | |
const int pinSB2 = 5; | |
const int pinRNB2 = 7; | |
const int pinRNB1 = 8; | |
const int pinRPB2 = 9; | |
const int pinRPB1 = 10; | |
// Variables de control de tiempo | |
unsigned long tiempoInicio = 0; | |
unsigned long tiempoRespuesta = 0; | |
unsigned long tiempoFuncionando = 0; | |
// Variables para el debouncing | |
const unsigned long debounceDelay = 80; // Tiempo de rebote en milisegundos | |
unsigned long lastDebounceTime = 0; // Último momento en que se detectó un cambio de estado | |
int estadoAnteriorBtn1 = LOW; // Estado anterior del botón 1 | |
int estadoAnteriorBtn2 = LOW; // Estado anterior del botón 2 | |
// Variable para almacenar el estado de los botones y sensores | |
int estadoBtn1 = LOW; | |
int estadoBtn2 = LOW; | |
// Variable de control para pulsador y sensor | |
int control = 0; | |
int ctrlSensor = 0; | |
void setup() { | |
// Configurar los pines | |
pinMode(pinBtn1, INPUT); | |
pinMode(pinBtn2, INPUT); | |
pinMode(pinSB1, INPUT); | |
pinMode(pinSB2, INPUT); | |
pinMode(pinRPB1, OUTPUT); | |
pinMode(pinRNB1, OUTPUT); | |
pinMode(pinRPB2, OUTPUT); | |
pinMode(pinRNB2, OUTPUT); | |
// Las salidas están invertidas | |
digitalWrite(pinRPB1, HIGH); | |
digitalWrite(pinRNB1, HIGH); | |
digitalWrite(pinRPB2, HIGH); | |
digitalWrite(pinRNB2, HIGH); | |
// Inicializar el monitor serie | |
Serial.begin(9600); | |
} | |
void loop() { | |
// Leer el estado actual de las entradas | |
estadoBtn1 = digitalRead(pinBtn1); | |
estadoBtn2 = digitalRead(pinBtn2); | |
// ===[ Control de Encendido del Electroimán 1 ]=== | |
if (estadoBtn1 == HIGH && control == 0) { | |
// Toma el tiempo desde que se presionó el pinBtn1 | |
if (estadoBtn1 != estadoAnteriorBtn1) { | |
lastDebounceTime = millis(); | |
estadoAnteriorBtn1 = estadoBtn1; | |
} | |
// Verificar si ha pasado el tiempo de rebote para el botón 1 | |
if ((millis() - lastDebounceTime) > debounceDelay && tiempoInicio == 0) { | |
// Activar pinRPB1 en LOW | |
digitalWrite(pinRPB1, LOW); | |
delay(20); | |
// Activar pinRNB1 en LOW | |
digitalWrite(pinRNB1, LOW); | |
tiempoInicio = millis(); // Toma tiempo desde que se mando activar el Electroimán 1 | |
control = 1; | |
} | |
} | |
// Para cuando suelte el pulsador, se actualizará el estado anterior del btn1 | |
if (estadoBtn1 == LOW && control == 1 && estadoAnteriorBtn1 == HIGH) { | |
estadoAnteriorBtn1 = estadoBtn1; | |
} | |
// Si vuelvo presionar el pulsador, apagará los relés | |
if (estadoBtn1 == HIGH && estadoAnteriorBtn1 == LOW && control == 1) { | |
// Desactivar pinRPB1 en HIGH | |
digitalWrite(pinRPB1, HIGH); | |
delay(20); | |
// Desactivar pinRNB1 en HIGH | |
digitalWrite(pinRNB1, HIGH); | |
tiempoFuncionando = millis() - tiempoInicio; | |
tiempoInicio = 0; | |
control = 0; | |
// Imprimir tiempos | |
Serial.println("\nBobina 1:"); | |
Serial.print("Tiempo de respuesta (ms) = "); | |
Serial.println(tiempoRespuesta); | |
Serial.print("Tiempo en funcionamiento (s) = "); | |
Serial.println(tiempoFuncionando); | |
delay(100); | |
ctrlSensor = 0; | |
} | |
// ===[ Control de Encendido del Electroimán 2 ]=== | |
if (estadoBtn2 == HIGH && control == 0) { | |
// Toma el tiempo desde que se presionó el pinBtn1 | |
if (estadoBtn2 != estadoAnteriorBtn2) { | |
lastDebounceTime = millis(); | |
estadoAnteriorBtn2 = estadoBtn2; | |
} | |
// Verificar si ha pasado el tiempo de rebote para el botón 1 | |
if ((millis() - lastDebounceTime) > debounceDelay && tiempoInicio == 0) { | |
// Activar pinRPB2 en LOW | |
digitalWrite(pinRPB2, LOW); | |
delay(20); | |
// Activar pinRNB2 en LOW | |
digitalWrite(pinRNB2, LOW); | |
tiempoInicio = millis(); // Toma tiempo desde que se mando activar el Electroimán 2 | |
control = 1; | |
} | |
} | |
// Para cuando suelte el pulsador, se actualizará el estado anterior del btn1 | |
if (estadoBtn2 == LOW && control == 1 && estadoAnteriorBtn2 == HIGH) { | |
estadoAnteriorBtn2 = estadoBtn2; | |
} | |
// Si vuelvo presionar el pulsador, apagará los relés | |
if (estadoBtn2 == HIGH && estadoAnteriorBtn2 == LOW && control == 1) { | |
// Desactivar pinRPB2 en HIGH | |
digitalWrite(pinRPB2, HIGH); | |
delay(20); | |
// Desactivar pinRNB2 en HIGH | |
digitalWrite(pinRNB2, HIGH); | |
tiempoFuncionando = millis() - tiempoInicio; | |
tiempoInicio = 0; | |
control = 0; | |
// Imprimir tiempos | |
Serial.println("\nBobina 2:"); | |
Serial.print("Tiempo de respuesta (ms) = "); | |
Serial.println(tiempoRespuesta); | |
Serial.print("Tiempo en funcionamiento (s) = "); | |
Serial.println(tiempoFuncionando); | |
delay(100); | |
ctrlSensor = 0; | |
} | |
// Detección de encendido por Reed Switches | |
if (digitalRead(pinSB1) == HIGH && ctrlSensor == 0) { // Sensor del Electroimán 1 | |
tiempoRespuesta = millis() - tiempoInicio; | |
ctrlSensor = 1; | |
} | |
if (digitalRead(pinSB2) == HIGH && ctrlSensor == 0) { // Sensor del Electroimán 2 | |
tiempoRespuesta = millis() - tiempoInicio; | |
ctrlSensor = 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment