FADER.ino 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2022 by M5Stack
  4. * Equipped with M5Core sample source code
  5. * 配套 M5Core 示例源代码
  6. * Visit for more information: https://docs.m5stack.com/en/unit/fader
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/fader
  8. *
  9. * Describe: UNIT FADER. 滑动电位器/推子
  10. * Date: 2021/8/20
  11. *******************************************************************************
  12. Connect UNIT FADER to port B and push the FADER slider to adjust the input
  13. value and light brightness 将UNIT FADER连接到B端口,
  14. 推动FADER滑杆即可实现调整输入数值大小与灯光亮度
  15. */
  16. #include "FastLED.h"
  17. #include "M5Stack.h"
  18. // How many leds in your strip?
  19. #define NUM_LEDS 14
  20. #define INPUT_PINS 36
  21. #define DATA_PIN 26
  22. // Define the array of leds
  23. CRGB leds[NUM_LEDS];
  24. uint8_t beginHue = 0;
  25. uint8_t deltaHue = 30;
  26. uint8_t brightness = 100;
  27. uint16_t rawADC = 0;
  28. void setup() {
  29. M5.begin();
  30. M5.Lcd.setTextDatum(MC_DATUM);
  31. M5.Lcd.drawString("FADER UNIT TEST", 160, 60, 4);
  32. FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  33. delay(1000);
  34. pinMode(36, INPUT);
  35. fill_rainbow(leds, NUM_LEDS, beginHue, deltaHue);
  36. }
  37. void loop() {
  38. rawADC = analogRead(INPUT_PINS); // Read ADC value 读取ADC数值
  39. brightness = map(rawADC, 0, 4095, 0,
  40. 255); // The mapping ADC value is the brightness value
  41. // range 映射ADC值为亮度值范围
  42. FastLED.setBrightness(brightness); // Adjust the brightness of the FADER
  43. // LED 调整FADER LED灯亮度
  44. FastLED.show();
  45. Serial.printf("%d\r\n", rawADC);
  46. M5.Lcd.fillRect(0, 120, 320, 100, BLACK);
  47. M5.Lcd.drawString("value: " + String(rawADC), 160, 160, 4);
  48. delay(100);
  49. }