- At first, we need to prepare the environment for ESP32 development board programming in arduino, and you can cheak this guide.
- secondly,Install the libraries and files which will be used
This is a library for our Monochrome OLEDs based on SSD1306 drivers.
#include <Adafruit_SSD1306.h>
This library allows you to communicate with I2C devices (the OLED is connected by I2C)
#include <Wire.h>
The Adafruit_GFX library provides a common syntax and set of graphics functions for all of our LCD and OLED displays and LED matrices.
#include <Adafruit_GFX.h>
Arduino library for controlling single-wire-based LED pixels and strip.
#include <Adafruit_NeoPixel.h>
- Difine your pins, this determines which pin of the board your device should be connected to.
#define LED 3 //the DI pin of ws2812 connect to GPIO3 #define voice 13 //the OUT pin of sound module connect to GPIO13 #define NUM_LEDS 16 //the maximum number of LEDs light up #define SCREEN_ADDRESS 0x3c // Address 0x3C for 128x32 #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define I2C_SDA 4 //the SDA pin connect to OLED #define I2C_SCL 5 //the SCLpin connect to OLED
- Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- Declare our NeoPixel strip object
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED, NEO_GRB + NEO_KHZ800);
- Initialise your devices so that they work properly
pinMode(voice, INPUT); //Set the "voice" pin as an input. strip.begin(); strip.setBrightness(60); strip.clear(); strip.show(); //Initialising the ws2812 state Serial.begin(115200); //Initialise I2C pins Wire.begin(I2C_SDA, I2C_SCL); // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for (;;); // Don't proceed, loop forever }
- Record the number of times the sound is detected by the sound sensor,set to record up to 120 times
if(digitalRead(voice) == 0){ count++; if (count>120){ count = 1; } }
- Adjust the number and color of ws2812 lights according to the count
void light() { //The ws2812 light implements a single-wire protocol, and the numbering starts at 0, not 1, so we're going to have to make some changes color = (count-1)/12; //12 counts for a round, each round has a different colour number = (count-1)%12; //Number of lights on in the round switch (color) { case 0: color_show( number, 255, 255, 255); //white break; case 1: color_show( number, 0, 0, 255); //blue break; case 2: color_show( number, 0, 255, 0); //green break; case 3: color_show( number, 0, 255, 255); //greenish blue break; case 4: color_show( number, 255, 0, 0); //red break; case 5: color_show( number, 255, 0, 255); //yellow break; case 6: color_show( number, 255, 255, 0); //purple break; case 7: color_show( number, 255, 127, 127); //light red break; case 8: color_show( number, 127, 255, 127); //light green break; case 9: color_show( number, 127, 127, 255); //light blue break; default: color_show( number, 0, 0, 0); //black return; } } //The ws2812 light implements a single-wire protocol,We need to define the colours for each light // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255 // pixels.setPixelColor(i, pixels.Color(0, 150, 0)); void color_show(int number, int red, int green, int blue){ for (int i = 0; i <NUM_LEDS - 4; i++){ if (i <= number){ strip.setPixelColor(i, red, green, blue); } else{ strip.setPixelColor(i, 0, 0, 0); } } }
- Lights on, and the OLED displays counting information
display.clearDisplay(); // Clear the display buffer display.setCursor(10, 10); //Where to start display display.setTextColor(WHITE); // Draw white text display.setTextSize(1); // Normal 1:1 pixel scale display.println("Makerfabs"); //what you want display in the OLED display.setCursor(10, 25); display.setTextColor(WHITE); display.setTextSize(1); display.println("MaESP ESP32 Dev Kit"); display.setCursor(10, 40); display.setTextColor(WHITE); display.setTextSize(1); display.println("count:" + String(count)); display.display(); // actually display all of the above strip.show(); // light on strip delay(200);
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.