#include <Arduino.h>
#include <U8g2lib.h>
// Ecrã 128x64 ST7920 (Pinos padrão RAMPS 1.4 / Mega)
//U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, 23, 17, 16, 25);
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
// Pinos do Encoder (2 e 3 têm interrupção rápida no Arduino Mega)
#define ENCODER_CLK 2
#define ENCODER_DT 3
#define ENCODER_SW 4 // Clique do botão do encoder
#define BUZZER_PIN 37
const unsigned long PADDLE_RATE = 32;
const unsigned long BALL_RATE = 16;
const uint8_t PADDLE_HEIGHT = 12;
const uint8_t SCORE_LIMIT = 9;
bool game_over = false, win = false;
uint8_t player_score = 0, mcu_score = 0;
uint8_t ball_x = 53, ball_y = 26;
int8_t ball_dir_x = 1, ball_dir_y = 1;
unsigned long ball_update;
unsigned long paddle_update;
const uint8_t MCU_X = 12;
int8_t mcu_y = 16;
const uint8_t PLAYER_X = 115;
volatile int8_t player_y = 16; // 'volatile' porque é alterado na interrupção
// Esta função corre automaticamente sempre que roda o encoder
void lerEncoder() {
if (digitalRead(ENCODER_DT) == HIGH) {
player_y -= 3; // Sobe (aumente ou diminua o '3' para ajustar a sensibilidade)
} else {
player_y += 3; // Desce
}
// Impede a raquete de sair do campo
if (player_y < 1) player_y = 1;
if (player_y + PADDLE_HEIGHT > 53) player_y = 53 - PADDLE_HEIGHT;
}
void setup() {
u8g2.begin();
u8g2.setFont(u8g2_font_6x10_tf);
pinMode(ENCODER_CLK, INPUT_PULLUP);
pinMode(ENCODER_DT, INPUT_PULLUP);
pinMode(ENCODER_SW, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
// Ativa a interrupção no pino 2 (CLK)
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), lerEncoder, FALLING);
ball_update = millis();
paddle_update = ball_update;
}
void loop() {
unsigned long time = millis();
// Atualização da Bola
if (time > ball_update) {
uint8_t new_x = ball_x + ball_dir_x;
uint8_t new_y = ball_y + ball_dir_y;
if (new_x <= 0 || new_x >= 127) {
ball_dir_x = -ball_dir_x;
new_x += ball_dir_x * 2;
if (new_x < 64) {
tone(BUZZER_PIN, 250, 40);
player_score++;
} else {
tone(BUZZER_PIN, 200, 40);
mcu_score++;
}
if (player_score == SCORE_LIMIT || mcu_score == SCORE_LIMIT) {
win = player_score > mcu_score;
game_over = true;
}
}
if (new_y <= 0 || new_y >= 53) {
tone(BUZZER_PIN, 200, 20);
ball_dir_y = -ball_dir_y;
new_y += ball_dir_y * 2;
}
if (new_x == MCU_X && new_y >= mcu_y && new_y <= mcu_y + PADDLE_HEIGHT) {
tone(BUZZER_PIN, 225, 20);
ball_dir_x = -ball_dir_x;
new_x += ball_dir_x * 2;
}
if (new_x == PLAYER_X && new_y >= player_y && new_y <= player_y + PADDLE_HEIGHT) {
tone(BUZZER_PIN, 250, 20);
ball_dir_x = -ball_dir_x;
new_x += ball_dir_x * 2;
}
ball_x = new_x;
ball_y = new_y;
ball_update += BALL_RATE;
}
// Atualização da Raquete do CPU
if (time > paddle_update) {
paddle_update += PADDLE_RATE;
const uint8_t half_paddle = PADDLE_HEIGHT >> 1;
if (mcu_y + half_paddle > ball_y) {
mcu_y += (ball_x > MCU_X) ? -1 : 1;
} else if (mcu_y + half_paddle < ball_y) {
mcu_y += (ball_x > MCU_X) ? 1 : -1;
}
if (mcu_y < 1) mcu_y = 1;
if (mcu_y + PADDLE_HEIGHT > 53) mcu_y = 53 - PADDLE_HEIGHT;
}
// Desenho no ecrã
u8g2.clearBuffer();
if (game_over) {
u8g2.setCursor(38, 28);
u8g2.print(win ? "YOU WIN!!" : "YOU LOSE!");
u8g2.setCursor(22, 44);
u8g2.print("Prima para jogar");
u8g2.sendBuffer();
// Espera que clique no botão do encoder para recomeçar
while (digitalRead(ENCODER_SW) == HIGH);
delay(200);
ball_x = 53; ball_y = 26;
mcu_y = 16; player_y = 16;
mcu_score = 0; player_score = 0;
game_over = false;
} else {
u8g2.drawFrame(0, 0, 128, 54);
u8g2.drawBox(ball_x, ball_y, 2, 2);
u8g2.drawVLine(MCU_X, mcu_y, PADDLE_HEIGHT);
u8g2.drawVLine(PLAYER_X, player_y, PADDLE_HEIGHT);
u8g2.setCursor(2, 63);
u8g2.print(mcu_score);
u8g2.setCursor(120, 63);
u8g2.print(player_score);
u8g2.sendBuffer();
}
}Loading
ssd1306
ssd1306