/*
Wokwi | questions
Help with a project
Ili - Tuesday, April 21, 2026 1:07 PM
This project originally called for the APDS-9960 sensor,
which detects gestures (up, down, left, and right).
However, since we are working in Wokwi and this simulator does
not have that sensor, the professor suggested using an
LDR sensor as a replacement.
The problem is that the LDR only detects changes in light
intensity, not gestures. Therefore, it is not possible to
directly replicate the functionality of the APDS-9960.
Due to this limitation, the proposed solution is to use buttons
to simulate gestures. Each button will represent a specific
direction (up, down, left, or right). When a button is pressed,
the system will interpret that event as a gesture in that direction.
Based on that signal, a corresponding LED will light up,
visually indicating the detected direction.
(Sorry I don't speak English)
https://wokwi.com/projects/461930971600428033
*/
// Pines (ajusta si tu cableado es distinto)
const int btnArriba = 7; // Amarillo (entrada)
const int btnIzquierda = 6; // Azul
const int btnAbajo = 4; // Verde
const int btnDerecha = 5; // Rojo
const int ledArriba = 12; // Amarillo (salida)
const int ledIzquierda = 11; // Azul
const int ledDerecha = 10; // Rojo
const int ledAbajo = 9; // Verde
// Estados anteriores y debounce
int ultimoEstadoArriba = HIGH;
int ultimoEstadoAbajo = HIGH;
int ultimoEstadoIzq = HIGH;
int ultimoEstadoDer = HIGH;
unsigned long ultimoTiempoArriba = 0;
unsigned long ultimoTiempoAbajo = 0;
unsigned long ultimoTiempoIzq = 0;
unsigned long ultimoTiempoDer = 0;
const unsigned long debounceDelay = 50; // ms
void setup() {
Serial.begin(9600);
// Entradas con pullup interno para evitar pines flotantes
pinMode(btnArriba, INPUT_PULLUP);
pinMode(btnIzquierda, INPUT_PULLUP);
pinMode(btnAbajo, INPUT_PULLUP);
pinMode(btnDerecha, INPUT_PULLUP);
pinMode(ledArriba, OUTPUT);
pinMode(ledIzquierda, OUTPUT);
pinMode(ledAbajo, OUTPUT);
pinMode(ledDerecha, OUTPUT);
// Asegurar LEDs apagadas
digitalWrite(ledArriba, LOW);
digitalWrite(ledIzquierda, LOW);
digitalWrite(ledAbajo, LOW);
digitalWrite(ledDerecha, LOW);
}
void loop() {
// Leer pines
int lecturaArriba = digitalRead(btnArriba);
int lecturaAbajo = digitalRead(btnAbajo);
int lecturaIzq = digitalRead(btnIzquierda);
int lecturaDer = digitalRead(btnDerecha);
unsigned long ahora = millis();
/*
// ARRIBA (activo-bajo: LOW = presionado)
if (lecturaArriba != ultimoEstadoArriba) {
ultimoTiempoArriba = ahora;
}
if ((ahora - ultimoTiempoArriba) > debounceDelay) {
if (lecturaArriba == LOW && ultimoEstadoArriba == HIGH) {
Serial.println("Movimiento detectado: ARRIBA");
parpadeo(ledArriba);
}
}
ultimoEstadoArriba = lecturaArriba;
*/
// ARRIBA (activo-bajo: LOW = presionado)
if (lecturaArriba != ultimoEstadoArriba) {
ultimoTiempoArriba = ahora;
}
if ((ahora - ultimoTiempoArriba) > debounceDelay) {
if (lecturaArriba == LOW) { // && ultimoEstadoArriba == HIGH) {
Serial.println("Movimiento detectado: ARRIBA");
parpadeo(ledArriba);
}
}
ultimoEstadoArriba = lecturaArriba;
// ABAJO
if (lecturaAbajo != ultimoEstadoAbajo) {
ultimoTiempoAbajo = ahora;
}
if ((ahora - ultimoTiempoAbajo) > debounceDelay) {
if (lecturaAbajo == LOW) { // && ultimoEstadoAbajo == HIGH) {
Serial.println("Movimiento detectado: ABAJO");
parpadeo(ledAbajo);
}
}
ultimoEstadoAbajo = lecturaAbajo;
// IZQUIERDA
if (lecturaIzq != ultimoEstadoIzq) {
ultimoTiempoIzq = ahora;
}
if ((ahora - ultimoTiempoIzq) > debounceDelay) {
if (lecturaIzq == LOW) { // && ultimoEstadoIzq == HIGH) {
Serial.println("Movimiento detectado: IZQUIERDA");
parpadeo(ledIzquierda);
}
}
ultimoEstadoIzq = lecturaIzq;
// DERECHA
if (lecturaDer != ultimoEstadoDer) {
ultimoTiempoDer = ahora;
}
if ((ahora - ultimoTiempoDer) > debounceDelay) {
if (lecturaDer == LOW) { // && ultimoEstadoDer == HIGH) {
Serial.println("Movimiento detectado: DERECHA");
parpadeo(ledDerecha);
}
}
ultimoEstadoDer = lecturaDer;
// Depuración: imprime estados (opcional, comentar si molesta)
Serial.print("A:"); Serial.print(lecturaArriba);
Serial.print(" I:"); Serial.print(lecturaIzq);
Serial.print(" Ab:"); Serial.print(lecturaAbajo);
Serial.print(" D:"); Serial.println(lecturaDer);
delay(20); // pequeño retardo para no saturar el serial
}
void parpadeo(int pinLed) {
digitalWrite(pinLed, HIGH);
delay(250);
digitalWrite(pinLed, LOW);
}