Advertisement
GefingerPoken

ESP32 Development Platform

May 19th, 2024
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.44 KB | Source Code | 0 0
  1. #include <Wire.h>                // https://arduino.cc/reference/en/language/functions/communication/wire
  2. #include <LiquidCrystal_I2C.h>   // https://arduinolibraries.info/libraries/liquid-crystal-i2-c
  3. #include <ESP32RotaryEncoder.h>  // https://arduinolibraries.info/libraries/esp32-rotary-encoder https://tinyurl.com/rotenc
  4. #include <TM1638plus.h>          // https://arduinolibraries.info/libraries/tm1638plus
  5. #include <LedControl.h>          // https://arduino.cc/reference/en/libraries/ledcontrol
  6. #include <FastLED.h>             // https://fastled.io/
  7. #include <WiFi.h>                // https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/src/WiFi.h
  8.  
  9. #define SCK 16   // SPI clock
  10. #define DTA 17   // SPI data
  11. #define CS_tm 4  // TM1638 chip select
  12. #define CS_m7 5  // MAX7219 chip select
  13.  
  14. #define CLK 14  // Rotary Encoder pin A
  15. #define DT 27   // Rotary Encoder pin B
  16. #define SW 26   // Rotary Encoder switch
  17.  
  18. #define DIN 2            // WS2812b strip data
  19. #define CHIPSET WS2812B  // led type
  20. #define COLOR_ORDER GRB  // Green Red Blue
  21. #define NUM_LEDS 32      // strip length
  22. int brightness = 45;     // initial brightness setting
  23.  
  24. LiquidCrystal_I2C lcd(0x27, 16, 2);     // I2C LCD (newer versions use the adress 0x3F)
  25. RotaryEncoder rot(CLK, DT, SW);         // KY-040 rotary encoder module with on-board pull-up resistors
  26. TM1638plus tm(CS_tm, SCK, DTA, false);  // TM1638 module (bool high_freq: true if high freq CPU > ~100 MHZ)
  27. LedControl ledm(DTA, SCK, CS_m7, 0);    // Max7219 LED matrix (address 0)
  28. CRGB leds[NUM_LEDS];                    // WS2812B LED strip
  29.  
  30. char line0[17];  // arrays for 16 character LCD screen rows
  31. char line1[17];  // (extra character for the null terminator)
  32.  
  33. int selection = 50;  // initial value for rotary encoder value
  34. const int lowerBoundary = 0;
  35. const int upperBoundary = 99;
  36. const bool wrap = false;
  37.  
  38. const char* ssid = "ssid";
  39. const char* password = "password";
  40.  
  41. void setup() {
  42.  
  43.   lcd.init();       // initialize the LCD module
  44.   lcd.backlight();  // turn on the backlight
  45.   lcd.clear();      // clear the screen
  46.  
  47.   tm.displayBegin();  // initialize the TM1638 module
  48.   tm.reset();
  49.   tm.setLED(0, 1);  // turn on 1st LED
  50.  
  51.   ledm.shutdown(0, false);  // initialize the Max7219 module
  52.   ledm.setIntensity(0, 0);  // address, brightness (0-15)
  53.   ledm.clearDisplay(0);     // turn off all LEDs
  54.  
  55.   rot.setEncoderType(EncoderType::HAS_PULLUP);
  56.   // rot.setEncoderType(EncoderType::SW_FLOAT);
  57.   rot.setBoundaries(lowerBoundary, upperBoundary, wrap);
  58.   rot.onTurned(&knobCallback);
  59.   rot.onPressed(&buttonCallback);
  60.   rot.begin();  // configure inputs and the attach interrupts
  61.  
  62.   lcd.setCursor(0, 0);  // position cursor to column 0, line 0
  63.   lcd.print("BlinkenLights");
  64.   lcd.setCursor(0, 1);  // position cursor to column 0, line 1
  65.   lcd.print("  DEV PLATFORM");
  66.  
  67.   FastLED.addLeds<CHIPSET, DIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  68.   FastLED.setBrightness(brightness);
  69.  
  70.   randomSeed(31415926);
  71.  
  72.   connectWiFi();
  73.  
  74.   lcd.clear();
  75. }
  76.  
  77. void loop() {
  78.  
  79.   tm.displayIntNum(millis(), true, TMAlignTextRight);  // millisecond counter
  80.  
  81.   ledm.setLed(0, random(8), random(8), true);   // turn on a random LED
  82.   ledm.setLed(0, random(8), random(8), false);  // turn off a ramdon LED
  83.  
  84.   sinelon();       // color dot sweeping back and forth, with fading trails
  85.   FastLED.show();  // (from FastLED example "DemoReel100")
  86. }
  87.  
  88. void knobCallback(long value) {
  89.   lcd.setCursor(0, 1);  // position cursor to column 0, line 1
  90.   lcd.print("Selecting: ");
  91.   selection = value;
  92.   lcd.print(value);
  93.   lcd.print("  ");
  94. }
  95.  
  96. void buttonCallback(unsigned long duration) {
  97.   lcd.setCursor(0, 0);  // position cursor to column 0, line 0
  98.   lcd.print("Selection: ");
  99.   lcd.print(selection);
  100.   lcd.print(" ");
  101. }
  102.  
  103. void sinelon() {
  104.   fadeToBlackBy(leds, NUM_LEDS, 4);          // amount to fade by (lower value = longer trail)
  105.   int pos = beatsin16(17, 0, NUM_LEDS - 1);  // beats per minute, lowest, highest
  106.   leds[pos] += CHSV(96, 255, brightness);    // hue, saturation, brightness
  107. }
  108.  
  109. void connectWiFi() {
  110.   delay(3000);
  111.   lcd.clear();
  112.   lcd.setCursor(0, 0);
  113.   lcd.print("connecting WiFi");
  114.   lcd.setCursor(3, 1);
  115.   WiFi.begin(ssid, password);
  116.  
  117.   while (WiFi.status() != WL_CONNECTED) {
  118.     delay(500);
  119.     lcd.print(".");
  120.   }
  121.  
  122.   lcd.clear();
  123.   lcd.setCursor(0, 0);
  124.   lcd.print("WiFi connected:");
  125.   lcd.setCursor(1, 1);
  126.   lcd.print(WiFi.localIP());
  127.   delay(3000);
  128. }
Tags: Arduino ESP32
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement