#include <TinyWireM.h>
#include <Tiny4kOLED.h>
#include <Adafruit_SoftServo.h> // Librería para controlar el servo
Adafruit_SoftServo myServo; // Crear un objeto servo
const unsigned long TIEMPO_ESPERA = 3000; // 3 segundos en milisegundos
unsigned long tiempoInicio; // Variable para almacenar el tiempo de inicio
bool cicloCompletado = false; // Variable para indicar si el ciclo ha sido completado
void setup() {
// Inicializar la pantalla OLED
oled.begin();
oled.clear();
oled.on();
oled.setFont(FONT8X16);
// Inicializar el servo
myServo.attach(3); // Conectar el servo al pin 3 (PB3) del ATtiny85
// Configuración inicial de la pantalla
oled.setCursor(0, 0);
oled.print(F(""));
// Registrar el tiempo de inicio
tiempoInicio = millis();
}
void loop() {
unsigned long tiempoActual = millis(); // Obtener el tiempo actual
if (!cicloCompletado) {
// Mostrar el tiempo transcurrido en la pantalla OLED
mostrarTiempoTranscurrido(tiempoActual - tiempoInicio);
// Verificar si han pasado 10 segundos
if (tiempoActual - tiempoInicio >= TIEMPO_ESPERA) {
// Mover el servo a 90 grados
// moverServo(90);
// delay(1000); // Espera 1 segundo
// Mover el servo a 0 grados
moverServo(0);
delay(1000); // Espera 1 segundo
// Marcar el ciclo como completado
cicloCompletado = true;
// Detener el programa después de completar el ciclo
while (true) {
// Bucle infinito para detener el proceso
}
}
}
}
void moverServo(int angulo) {
myServo.write(angulo);
// Refrescar el servo solo si es necesario
for (int i = 0; i < 20; i++) {
myServo.refresh();
delay(5); // Intervalo más corto para mayor precisión
}
}
void mostrarTiempoTranscurrido(unsigned long tiempo) {
// Mostrar el tiempo transcurrido a la derecha de los dos puntos
oled.setCursor(50, 0); // Posicionar el cursor en la primera línea, a la derecha de "Tiempo:"
//oled.print(F(" ")); // Limpiar el área a la derecha del texto
oled.setCursor(50, 0); // Volver a posicionar el cursor
oled.print(tiempo / 1000); // Convertir milisegundos a segundos
oled.print(F(" seg"));
}