本帖最后由 allentfy 于 2022-8-8 17:26 编辑
请论坛里的大神指导,有不好的地方,欢迎评论,给我学习的机会。 环境搭建: 编译配置:
原理图: 功能要求: 1. 上电用定时器使LED1闪烁。 2. LED2呼吸灯。 3. 板载RGB快闪再关闭。 4. 按键SE4控制数码管切换显示。 5. 手机连接WIFI点亮板载LED开关。 6. 串口打印电位器值,ADC,电位器调整显示不一样的值。
先上个端口定义: /********************led输出端口****************************************************/ pinMode(LED_BUILTIN,OUTPUT); //将LED引脚设置为输出模式 pinMode(4, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); pinMode(21, OUTPUT); pinMode(25, OUTPUT); pinMode(14, OUTPUT); pinMode(0, OUTPUT); pinMode(16, OUTPUT); pinMode(17, OUTPUT); pinMode(2, OUTPUT); pinMode(26, OUTPUT); /********************按********************************************************/ pinMode(15, INPUT_PULLUP); pinMode(35, INPUT_PULLUP); /********************wifi***********************************************************/
1. LED1闪烁 pinMode(4, OUTPUT); /********************定时器************************************************************/ hw_timer_t* timer =NULL; int flag = 0; void IRAM_ATTRonTimer(){ flag ^=1; digitalWrite(4,flag); //把4脚LED点亮 } Serial.begin(115200); Serial.println(); Serial.println("Configuring access point...");
timer =timerBegin(0,80,true); //初始,待定定时器几 timerAttachInterrupt(timer,&onTimer,true); //中断 timerAlarmWrite(timer,1000000,true); //1S定时 timerAlarmEnable(timer); //使能
2.呼吸灯 /********************呼吸灯************************************************************/ int freq = 2000; // 频率 int channel = 0; // 通道 int resolution = 8; // 分辨率 const int led_pwm = 12; /********************呼吸灯***********************************************************/ ledcSetup(channel, freq,resolution); // 设置通道 ledcAttachPin(led_pwm, channel); // 将通道与对应的引脚连接 /**********************呼吸灯**********************************************************/ // 逐渐变亮 for (int dutyCycle = 0;dutyCycle <= 255; dutyCycle = dutyCycle + 5) { ledcWrite(channel,dutyCycle); // 输出PWM delay(10); } // 逐渐变暗 for (int dutyCycle = 255;dutyCycle >= 0; dutyCycle = dutyCycle - 5) { ledcWrite(channel,dutyCycle); // 输出PWM delay(10); } 3.RGB /********************RGB宏定义************************************************************/ #define NUM_LEDS 1 #define DATA_PIN 5 //#define CLOCK_PIN 13 CRGB leds[NUM_LEDS]; /********************RGB************************************************************/ FastLED.addLeds<NEOPIXEL,DATA_PIN>(leds, NUM_LEDS); //灯珠显示红色 leds[0] = CRGB::Red; FastLED.show(); delay(200); // 灯珠显示绿色 leds[0] = CRGB::Green; FastLED.show(); delay(200); // 灯珠显示蓝色 leds[0] = CRGB::Blue; FastLED.show(); delay(200); leds[0] = 0; FastLED.show();
4.按键切换数码管 /********************按键定义************************************************************/ const int buttonPin = 15; int buttonState = 0; // variable for reading the pushbutton status char i=0; /**********************按键数码管显示**************************************************/ buttonState =digitalRead(buttonPin); if(buttonState == LOW) { delay(5); if(buttonState == LOW) { i++; } } if(i==1){//显示9 digitalWrite(13,LOW); //com0 digitalWrite(21,LOW); //com1 digitalWrite(25,LOW); //A digitalWrite(14,HIGH); //B digitalWrite(0,HIGH); //C digitalWrite(16,HIGH); //D digitalWrite(17,HIGH); //E digitalWrite(2,HIGH); //F digitalWrite(26,HIGH); //G }else if(i==2){//显示8 digitalWrite(13,LOW); //com0 digitalWrite(21,LOW); //com1 digitalWrite(25,HIGH); //A digitalWrite(14,HIGH); //B digitalWrite(0,HIGH); //C digitalWrite(16,HIGH); //D digitalWrite(17,HIGH); //E digitalWrite(2,HIGH); //F digitalWrite(26,HIGH); //G }else if(i==3){//显示7 digitalWrite(13,LOW); //com0 digitalWrite(21,LOW); //com1 digitalWrite(25,LOW); //A digitalWrite(14,LOW); //B digitalWrite(0,HIGH); //C digitalWrite(16,HIGH); //D digitalWrite(17,HIGH); //E digitalWrite(2,HIGH); //F digitalWrite(26,LOW); //G }else if(i==4){//关显示 digitalWrite(13,LOW); //com0 digitalWrite(21, LOW); //com1 digitalWrite(25,LOW); //A digitalWrite(14,LOW); //B digitalWrite(0,LOW); //C digitalWrite(16,LOW); //D digitalWrite(17,LOW); //E digitalWrite(2,LOW); //F digitalWrite(26,LOW); //G }else{ i=0; } 5.连接WIFI控制LED开关(官方原码) // 设置你的wifi与密码 const char *ssid = "meiyao_esp32"; const char *password = "88888888"; WiFiServer server(80);
// 配置wifi以及获取IP地址. WiFi.softAP(ssid, password); IPAddress myIP =WiFi.softAPIP(); Serial.print("AP IPaddress: "); Serial.println(myIP); server.begin(); Serial.println("Server started");
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get aclient, Serial.println("NewClient."); // print amessage out the serial port String currentLine =""; // make aString to hold incoming data from the client while (client.connected()){ // loop while the client'sconnected if (client.available()){ // if there's bytes to readfrom the client, char c =client.read(); // read abyte, then Serial.write(c); // print it out the serialmonitor if (c == '\n') { // if the byte is a newlinecharacter
// if the currentline is blank, you got two newline characters in a row. // that's the end ofthe client HTTP request, so send a response: if(currentLine.length() == 0) { // HTTP headersalways start with a response code (e.g. HTTP/1.1 200 OK) // and acontent-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println();
// the content ofthe HTTP response follows the header: client.print("Click <a href=\"/H\">here</a>to turn ON the LED.<br>"); client.print("Click <a href=\"/L\">here</a>to turn OFF the LED.<br>");
// The HTTPresponse ends with another blank line: client.println(); // break out ofthe while loop: break; } else { // if you got a newline, then clearcurrentLine: currentLine =""; } } else if (c != '\r'){ // if you got anything else but acarriage return character, currentLine +=c; // add it to the end of thecurrentLine }
// Check to see if theclient request was "GET /H" or "GET /L": if (currentLine.endsWith("GET /H")){ digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on } if(currentLine.endsWith("GET /L")) { digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off } } } // close the connection: client.stop(); Serial.println("Client Disconnected."); } 6.串口打印 /********************串口定义************************************************************/ int value;
/**********************串口打印**********************************************************/ value = analogRead(35); Serial.println(value);
调整电位器显示如下: 整体板子的效果图
|