Advertisement
mayorBanana

Untitled

Apr 13th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 6
  4. #define WIDTH 8
  5. #define HEIGHT 8
  6. #define NUM_LEDS (WIDTH * HEIGHT)
  7. CRGB leds[NUM_LEDS];
  8.  
  9.  
  10. int index(int x, int y) {
  11. if (y % 2 == 0)
  12. return y * WIDTH + WIDTH - x - 1;
  13. else
  14. return y * WIDTH + x;
  15. }
  16.  
  17.  
  18. class keyboard_t {
  19. private:
  20. int row_pins[4];
  21. int col_pins[4];
  22.  
  23. public:
  24. keyboard_t(int rows[4], int cols[4]) {
  25. for (int i = 0; i < 4; i++) {
  26. int row_pin = rows[i];
  27. row_pins[i] = row_pin;
  28. pinMode(row_pin, INPUT_PULLUP);
  29. }
  30.  
  31. for (int i = 0; i < 4; i++) {
  32. int col_pin = cols[i];
  33. col_pins[i] = col_pin;
  34. pinMode(col_pin, INPUT);
  35. }
  36. }
  37.  
  38. bool isPressed(int x, int y) {
  39. int col_pin_no = this->col_pins[x];
  40. pinMode(col_pin_no, OUTPUT);
  41. digitalWrite(col_pin_no, LOW);
  42.  
  43. int row_pin_no = this->row_pins[y];
  44. int return_value = digitalRead(row_pin_no);
  45.  
  46. pinMode(col_pin_no, INPUT);
  47. if (return_value != 0)
  48. return 0;
  49. else
  50. return 1;
  51. }
  52. };
  53.  
  54. int row_pins[] = { 2, 3, 4, 5 };
  55. int col_pins[] = { A0, A1, A2, A3 };
  56. keyboard_t kbd(row_pins, col_pins);
  57.  
  58.  
  59. void setup() {
  60. Serial.begin(115200);
  61. FastLED.addLeds<NEOPIXEL, 6>(leds, NUM_LEDS);
  62. }
  63.  
  64. #define SNAKE_SIZE 5
  65. int pixel_x[SNAKE_SIZE] = { 0 };
  66. int pixel_y[SNAKE_SIZE] = { 0 };
  67. int pixel_vx = 0;
  68. int pixel_vy = 0;
  69.  
  70. int deadline = 0;
  71.  
  72. // https://pastebin.com/Djeaivk4
  73. void loop() {
  74. if (kbd.isPressed(1, 0)) {
  75. pixel_vx = 0;
  76. pixel_vy = 1;
  77. } else if (kbd.isPressed(0, 1)) {
  78. pixel_vx = -1;
  79. pixel_vy = 0;
  80. } else if (kbd.isPressed(2, 1)) {
  81. pixel_vx = +1;
  82. pixel_vy = 0;
  83. } else if (kbd.isPressed(1, 2)) {
  84. pixel_vx = 0;
  85. pixel_vy = -1;
  86. }
  87.  
  88. int now = millis();
  89. if (now >= deadline) {
  90. for (int i = 0; i < NUM_LEDS; i++)
  91. leds[i].setRGB(0, 0, 0);
  92.  
  93. for (int i = SNAKE_SIZE - 1; i > 0; i--) {
  94. pixel_x[i] = pixel_x[i - 1];
  95. pixel_y[i] = pixel_y[i - 1];
  96. }
  97. pixel_x[0] = pixel_x[1] + pixel_vx;
  98. pixel_y[0] = pixel_y[1] + pixel_vy;
  99. if (pixel_x[0] < 0)
  100. pixel_x[0] = WIDTH - 1;
  101. if (pixel_x[0] >= WIDTH)
  102. pixel_x[0] = 0;
  103. if (pixel_y[0] < 0)
  104. pixel_y[0] = HEIGHT - 1;
  105. if (pixel_y[0] >= HEIGHT)
  106. pixel_y[0] = 0;
  107.  
  108. for (int i = 0; i < 5; i++)
  109. if (i == 0) {
  110. leds[index(pixel_x[i], pixel_y[i])].setRGB(255, 0, 0);
  111. } else if(i == 4) {
  112. leds[index(pixel_x[i], pixel_y[i])].setRGB(255, 255, 0);
  113. }
  114. else{
  115. leds[index(pixel_x[i], pixel_y[i])].setRGB(0, 255, 0);
  116. }
  117. FastLED.show();
  118.  
  119. deadline = now + 500;
  120. }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement