Advertisement
TopHatRaver

Test_Matrix

Sep 2nd, 2023
1,277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.10 KB | Source Code | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN  6
  4. #define ANALOGPIN A2
  5. #define COLOR_ORDER GRB
  6. #define CHIPSET     WS2811
  7.  
  8. #define BRIGHTNESS 64
  9. #define FRAMES_PER_SECOND  120
  10.  
  11. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  12.  
  13. #define MAX_DIMENSION ((kMatrixWidth > kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
  14.  
  15.  
  16.  
  17. // Params for width and height
  18. const uint8_t kMatrixWidth = 7;
  19. const uint8_t kMatrixHeight = 14;
  20.  
  21. // Param for different pixel layouts
  22. const bool    kMatrixSerpentineLayout = false;
  23. const bool    kMatrixVertical = true;
  24.  
  25. uint8_t gHue = 0;
  26. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  27.  
  28. uint16_t value;
  29.  
  30. uint16_t XY( uint8_t x, uint8_t y)
  31. {
  32.   uint16_t i;
  33.  
  34.   if( kMatrixSerpentineLayout == false) {
  35.     if (kMatrixVertical == false) {
  36.       i = (y * kMatrixWidth) + x;
  37.     } else {
  38.       i = kMatrixHeight * (kMatrixWidth - (x+1))+y;
  39.     }
  40.   }
  41.  
  42.   if( kMatrixSerpentineLayout == true) {
  43.     if (kMatrixVertical == false) {
  44.       if( y & 0x01) {
  45.         // Odd rows run backwards
  46.         uint8_t reverseX = (kMatrixWidth - 1) - x;
  47.         i = (y * kMatrixWidth) + reverseX;
  48.       } else {
  49.         // Even rows run forwards
  50.         i = (y * kMatrixWidth) + x;
  51.       }
  52.     } else { // vertical positioning
  53.       if ( x & 0x01) {
  54.         i = kMatrixHeight * (kMatrixWidth - (x+1))+y;
  55.       } else {
  56.         i = kMatrixHeight * (kMatrixWidth - x) - (y+1);
  57.       }
  58.     }
  59.   }
  60.  
  61.   return i;
  62. }
  63.  
  64. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  65. CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
  66. CRGB* const leds( leds_plus_safety_pixel + 1);
  67.  
  68. typedef void (*SimplePatternList[])();
  69.  
  70.  
  71. uint16_t XYsafe( uint8_t x, uint8_t y)
  72. {
  73.   if( x >= kMatrixWidth) return -1;
  74.   if( y >= kMatrixHeight) return -1;
  75.   return XY(x,y);
  76. }
  77.  
  78. SimplePatternList gPatterns = {
  79.  
  80.   test
  81.  
  82.   };
  83.  
  84. void loop()
  85. {
  86.     gPatterns[gCurrentPatternNumber]();
  87.  
  88.   // send the 'leds' array out to the actual LED strip
  89.   FastLED.show();  
  90.   // insert a delay to keep the framerate modest
  91.   FastLED.delay(1000/FRAMES_PER_SECOND);
  92.  
  93.   // do some periodic updates
  94.  
  95.   EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
  96.  
  97.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  98. }
  99.  
  100.  
  101. void nextPattern()
  102. {
  103.   // add one to the current pattern number, and wrap around at the end
  104.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  105. }
  106.  
  107.  
  108. void setup() {
  109.   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
  110.   FastLED.setBrightness( BRIGHTNESS );
  111.   Serial.begin(9600);
  112. }
  113.  
  114. void test() {
  115.   int V = read_pot();
  116.   for (int x = 0; x < kMatrixWidth; x++){
  117.     for (int y = 0; y < kMatrixHeight; y++){
  118.       leds[XY(x,y)] = CHSV(160,255, V);
  119.       FastLED.show();
  120.       delay(20);
  121.     }
  122.   }
  123.   fill_solid(leds, NUM_LEDS, CRGB::Black);
  124.   FastLED.show();
  125. }
  126.  
  127.  
  128. int read_pot() //LINEAR POT WORKS BEST
  129. {
  130.   value = analogRead(ANALOGPIN);
  131.   FastLED.setBrightness(value/4);
  132.   Serial.print("Pot Value "); Serial.println(value/4);
  133.   return(value/4);
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement