|
| 1 | +#include <Wire.h> |
| 2 | +#include <LiquidCrystal_I2C.h> |
| 3 | + |
| 4 | +#define SDA 13 |
| 5 | +#define SCL 14 |
| 6 | + |
| 7 | +LiquidCrystal_I2C lcd(0x27, 16, 2); |
| 8 | + |
| 9 | +// Custom characters |
| 10 | +byte fullBlock[8] = { |
| 11 | + 0b11111, |
| 12 | + 0b11111, |
| 13 | + 0b11111, |
| 14 | + 0b11111, |
| 15 | + 0b11111, |
| 16 | + 0b11111, |
| 17 | + 0b11111, |
| 18 | + 0b11111 |
| 19 | +}; |
| 20 | + |
| 21 | +byte topHalf[8] = { |
| 22 | + 0b11111, |
| 23 | + 0b11111, |
| 24 | + 0b11111, |
| 25 | + 0b11111, |
| 26 | + 0b00000, |
| 27 | + 0b00000, |
| 28 | + 0b00000, |
| 29 | + 0b00000 |
| 30 | +}; |
| 31 | + |
| 32 | +byte bottomHalf[8] = { |
| 33 | + 0b00000, |
| 34 | + 0b00000, |
| 35 | + 0b00000, |
| 36 | + 0b00000, |
| 37 | + 0b11111, |
| 38 | + 0b11111, |
| 39 | + 0b11111, |
| 40 | + 0b11111 |
| 41 | +}; |
| 42 | + |
| 43 | +byte emptyBlock[8] = { |
| 44 | + 0b00000, |
| 45 | + 0b00000, |
| 46 | + 0b00000, |
| 47 | + 0b00000, |
| 48 | + 0b00000, |
| 49 | + 0b00000, |
| 50 | + 0b00000, |
| 51 | + 0b00000 |
| 52 | +}; |
| 53 | + |
| 54 | +int blockX = 0; // 0–15 |
| 55 | +int blockY = 0; // 0–3 (virtual rows) |
| 56 | + |
| 57 | +void setup() { |
| 58 | + Wire.begin(SDA, SCL); |
| 59 | + lcd.init(); |
| 60 | + lcd.backlight(); |
| 61 | + |
| 62 | + // Load custom characters (indexes 0–3) |
| 63 | + lcd.createChar(0, fullBlock); |
| 64 | + lcd.createChar(1, topHalf); |
| 65 | + lcd.createChar(2, bottomHalf); |
| 66 | + lcd.createChar(3, emptyBlock); |
| 67 | + |
| 68 | + Serial.begin(9600); |
| 69 | + Serial.println("Use w/a/s/d to move block"); |
| 70 | + |
| 71 | + drawBlock(); |
| 72 | +} |
| 73 | + |
| 74 | +void loop() { |
| 75 | + if (Serial.available()) { |
| 76 | + char cmd = Serial.read(); |
| 77 | + |
| 78 | + // Clear current block |
| 79 | + drawAt(blockX, blockY, 3); // draw empty |
| 80 | + |
| 81 | + // Update position |
| 82 | + if (cmd == 'a' && blockX > 0) blockX--; |
| 83 | + if (cmd == 'd' && blockX < 15) blockX++; |
| 84 | + if (cmd == 'w' && blockY > 0) blockY--; |
| 85 | + if (cmd == 's' && blockY < 3) blockY++; |
| 86 | + |
| 87 | + drawBlock(); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Draw a block at current position |
| 92 | +void drawBlock() { |
| 93 | + drawAt(blockX, blockY, getCharForRow(blockY)); |
| 94 | +} |
| 95 | + |
| 96 | +// Determine char to draw for virtual row |
| 97 | +byte getCharForRow(int row) { |
| 98 | + if (row % 2 == 0) return 1; // top half |
| 99 | + else return 2; // bottom half |
| 100 | +} |
| 101 | + |
| 102 | +// Place a custom char at virtual row |
| 103 | +void drawAt(int x, int vRow, byte charIndex) { |
| 104 | + int lcdRow = vRow / 2; |
| 105 | + lcd.setCursor(x, lcdRow); |
| 106 | + lcd.write(charIndex); |
| 107 | +} |
0 commit comments