Advertisement
vitareinforce

code bikinan anya

Jul 12th, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #include <Adafruit_GFX.h>
  2. #include <Adafruit_NeoMatrix.h>
  3. #include <Adafruit_NeoPixel.h>
  4. #include <SoftwareSerial.h>
  5.  
  6. #define PIN 6 // Define the pin connected to the LED matrix
  7.  
  8. Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(32, 64, PIN,
  9. NEO_MATRIX_TOP + NEO_MATRIX_LEFT +
  10. NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG,
  11. #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000L)
  12. NEO_GRB + NEO_KHZ800
  13. #else
  14. NEO_GRB + NEO_KHZ800
  15. #endif
  16. );
  17.  
  18. SoftwareSerial serial(2, 3); // Define the software serial pins
  19.  
  20. void setup() {
  21. matrix.begin();
  22. matrix.setTextWrap(false);
  23. matrix.setBrightness(50); // Set the brightness of the LED matrix
  24. matrix.setTextColor(matrix.Color(255, 255, 255)); // Set the text color
  25.  
  26. serial.begin(9600); // Set the baud rate of the serial communication
  27. }
  28.  
  29. void loop() {
  30. if (serial.available()) {
  31. // Read incoming data from the computer
  32. String data = serial.readStringUntil('\n');
  33.  
  34. // Split the received data into individual pixel values
  35. String pixelValues[2048]; // Assuming maximum of 32x64=2048 pixels
  36. int numPixels = splitString(data, ',', pixelValues);
  37.  
  38. if (numPixels == 2048) {
  39. matrix.clear();
  40.  
  41. // Display the pixels on the LED matrix
  42. for (int i = 0; i < numPixels; i++) {
  43. int x = i % 32;
  44. int y = i / 32;
  45.  
  46. // Convert the pixel value to an integer
  47. int pixelValue = pixelValues[i].toInt();
  48.  
  49. // Set the color of the LED based on the pixel value
  50. if (pixelValue == 1) {
  51. matrix.drawPixel(x, y, matrix.Color(255, 255, 255));
  52. } else {
  53. matrix.drawPixel(x, y, matrix.Color(0, 0, 0));
  54. }
  55. }
  56.  
  57. matrix.show(); // Display the updated LED matrix
  58. }
  59. }
  60. }
  61.  
  62. // Function to split a string into an array based on a delimiter
  63. int splitString(String input, char delimiter, String output[]) {
  64. int counter = 0;
  65. String temp = "";
  66.  
  67. for (int i = 0; i < input.length(); i++) {
  68. if (input.charAt(i) == delimiter) {
  69. output[counter] = temp;
  70. counter++;
  71. temp = "";
  72. } else {
  73. temp += input.charAt(i);
  74. }
  75. }
  76.  
  77. return counter;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement