Advertisement
timor2542

VEC2023 NATIONAL LAB Q1

May 27th, 2024
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.66 KB | Source Code | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #include <TFT_eSPI.h>  // Hardware-specific library
  3.  
  4. TFT_eSPI tft;  // Invoke custom library
  5.  
  6. #define PIN BCM27
  7. #define NUMPIXELS 12
  8.  
  9. #define SW7 BCM4
  10. #define SW6 BCM5
  11.  
  12. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  13.  
  14. bool toEdit = false;
  15. int menu = 0;
  16. int R_val, G_val, B_val, bright_val, led_num_val;
  17. int R_bar_val, G_bar_val, B_bar_val;
  18.  
  19. void setup() {
  20.  
  21.   pinMode(SW7, INPUT_PULLUP);
  22.   pinMode(SW6, INPUT_PULLUP);
  23.  
  24.   pixels.begin();  // This initializes the NeoPixel library.
  25.   pixels.clear();
  26.   pixels.show();
  27.  
  28.   tft.begin();
  29.   tft.setRotation(3);
  30.   tft.setTextSize(3);
  31.   tft.fillScreen(TFT_BLACK);
  32.  
  33.   tft.drawString("LED AMOUNT:", 10, 10);
  34.   tft.drawString("BRIGHTNESS:", 10, 60);
  35.   tft.drawString("R:", 10, 110);
  36.   tft.drawRect(48, 108, 154, 29, TFT_RED);
  37.   tft.drawString("G:", 10, 160);
  38.   tft.drawRect(48, 158, 154, 29, TFT_GREEN);
  39.   tft.drawString("B:", 10, 210);
  40.   tft.drawRect(48, 208, 154, 29, TFT_BLUE);
  41.   analogReadResolution(12);
  42. }
  43.  
  44. void loop() {
  45.   if ((digitalRead(SW7) == LOW) && toEdit == false) {
  46.     while (digitalRead(SW7) == LOW) { delay(200); }
  47.     menu++;
  48.     if (menu > 4) {
  49.       menu = 0;
  50.     }
  51.     tft.fillRect(290, 0, 40, 240, TFT_BLACK);
  52.   }
  53.   if (digitalRead(SW6) == LOW) {
  54.     while (digitalRead(SW6) == LOW) { delay(200); }
  55.     toEdit = !toEdit;
  56.     tft.fillRect(290, 0, 40, 240, TFT_BLACK);
  57.   }
  58.   switch (toEdit) {
  59.     case true:
  60.       {
  61.  
  62.         tft.fillTriangle(300, 20 + (50 * menu), 319, 10 + (50 * menu), 319, 30 + (50 * menu), TFT_RED);
  63.         switch (menu) {
  64.           case 0:
  65.             {
  66.               led_num_val = map(analogRead(1), 0, 4095, 0, 12);
  67.               break;
  68.             }
  69.           case 1:
  70.             {
  71.               bright_val = map(analogRead(1), 0, 4095, 0, 255);
  72.               break;
  73.             }
  74.           case 2:
  75.             {
  76.               R_val = map(analogRead(1), 0, 4095, 0, 255);
  77.               R_bar_val = map(analogRead(1), 0, 4095, 0, 150);
  78.               break;
  79.             }
  80.           case 3:
  81.             {
  82.               G_val = map(analogRead(1), 0, 4095, 0, 255);
  83.               G_bar_val = map(analogRead(1), 0, 4095, 0, 150);
  84.               break;
  85.             }
  86.           case 4:
  87.             {
  88.               B_val = map(analogRead(1), 0, 4095, 0, 255);
  89.               B_bar_val = map(analogRead(1), 0, 4095, 0, 150);
  90.               break;
  91.             }
  92.         }
  93.         break;
  94.       }
  95.     case false:
  96.       {
  97.         tft.fillTriangle(300, 20 + (50 * menu), 319, 10 + (50 * menu), 319, 30 + (50 * menu), TFT_WHITE);
  98.         break;
  99.       }
  100.   }
  101.   tft.drawString(String(led_num_val) + "  ", 210, 10);
  102.  
  103.   tft.drawString(String(bright_val) + "  ", 210, 60);
  104.  
  105.   tft.fillRect(50, 110, R_bar_val, 25, TFT_RED);
  106.   tft.fillRect(50 + R_bar_val, 110, 150 - R_bar_val, 25, TFT_BLACK);
  107.   tft.drawString(String(R_val) + "  ", 210, 110);
  108.  
  109.   tft.fillRect(50, 160, G_bar_val, 25, TFT_GREEN);
  110.   tft.fillRect(50 + G_bar_val, 160, 150 - G_bar_val, 25, TFT_BLACK);
  111.   tft.drawString(String(G_val) + "  ", 210, 160);
  112.  
  113.   tft.fillRect(50, 210, B_bar_val, 25, TFT_BLUE);
  114.   tft.fillRect(50 + B_bar_val, 210, 150 - B_bar_val, 25, TFT_BLACK);
  115.   tft.drawString(String(B_val) + "  ", 210, 210);
  116.  
  117.   pixels.setBrightness(bright_val);
  118.   for (int j = 1; j <= led_num_val; j++) {
  119.     pixels.setPixelColor(j - 1, pixels.Color(R_val, G_val, B_val));
  120.     pixels.show();  // This sends the updated pixel color to the hardware.
  121.   }
  122.   for (int j = NUMPIXELS; j > led_num_val; j--) {
  123.     pixels.setPixelColor(j - 1, pixels.Color(0, 0, 0));
  124.     pixels.show();  // This sends the updated pixel color to the hardware.
  125.   }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement