/******************************************************************************* * * I24: Apontador Laser Pan-Tilt com Arduino e Joystick * Autor: Angelo Luis Ferreira * Data: 21/03/2026 * * http://squids.com.br/arduino * *******************************************************************************/ #include // Servos Servo servoPan; Servo servoTilt; // Joystick int pinX = A1; int pinY = A0; // Botão e laser int pinButton = 2; int pinLaser = 7; // Posição dos servos int posPan; int posTilt; // Estado do laser bool estadoLaser = false; bool ultimoBotao = HIGH; // Controle de tempo unsigned long tempoBotao = 0; unsigned long tempoServo = 0; const int debounceDelay = 200; const int intervaloServo = 20; // LIMITES DE MOVIMENTO (ajuste fino aqui) const int panMin = 60; const int panMax = 120; const int tiltMin = 70; const int tiltMax = 110; // Zona morta do joystick const int deadZone = 30; void setup() { servoPan.attach(9); servoTilt.attach(10); pinMode(pinButton, INPUT_PULLUP); pinMode(pinLaser, OUTPUT); digitalWrite(pinLaser, LOW); // Centraliza os servos servoPan.write((panMin + panMax) / 2); servoTilt.write((tiltMin + tiltMax) / 2); } void loop() { unsigned long agora = millis(); // Atualização dos servos if (agora - tempoServo >= intervaloServo) { tempoServo = agora; int x = analogRead(pinX); int y = analogRead(pinY); // Aplica zona morta if (abs(x - 512) < deadZone) x = 512; if (abs(y - 512) < deadZone) y = 512; // Mapeamento com faixa reduzida posPan = map(x, 0, 1023, panMin, panMax); posTilt = map(y, 0, 1023, tiltMin, tiltMax); servoPan.write(posPan); servoTilt.write(posTilt); } // Leitura do botão bool leituraBotao = digitalRead(pinButton); if (leituraBotao == LOW && ultimoBotao == HIGH && (agora - tempoBotao > debounceDelay)) { tempoBotao = agora; estadoLaser = !estadoLaser; digitalWrite(pinLaser, estadoLaser); } ultimoBotao = leituraBotao; }