#include #include #include "Ultrasonic.h" #include #include "FastLED.h" //wifi cofi const char *ssid = "x.factory"; // WIFI account const char *password = "make0314"; // WIFI password #define USE_MULTOCRE 0 #define NUM_LEDS 100 // set the num of led #define LED_DT 9 // LED pin #define LED_TYPE WS2812 // LED Light Strip Model #define COLOR_ORDER GRB uint8_t max_bright = 128; // LED max bright CRGB leds[NUM_LEDS]; //CRGB ColorName方法定义颜色 CRGB myRGBcolor(50,50,50); // myRGBcolor(rValue,gValue,bValue) // rValue: 0 - 255 // gValue: 0 - 255 // bValue: 0 - 255 Ultrasonic ultrasonic(4); const int dirPin = 1; // dir pin const int stepPin = 2; // step pin int num = 0; const int STEPS_PER_REV = 1; const float stepAngle = 0.9; // Step angle of stepper motor const int microstepping = 1; // 1=full step long stepCount_S = 0; // num of step fow slow long stepCount_F = 0; // num of step fow fast long RangeInCentimeters = 1000; int remember_min; //rec min int remember_sec; // rec sec int remember_hour; // rec hour // Time info #define NTP1 "ntp1.aliyun.com" #define NTP2 "ntp2.aliyun.com" #define NTP3 "ntp3.aliyun.com" String minute; String second; String hour; void xTaskOne(void *xTask1){ int count = 0; int rangeMeasurements[3]; // Storage of three measurements int sum = 0; // For totalizing measured values while (1) { for (int i = 0; i < 3; i++) { // 进行三次测量 rangeMeasurements[i] = ultrasonic.MeasureInCentimeters(); sum += rangeMeasurements[i]; // 累加测量值 delay(250); // 两次测量之间保持间隔 } // Calculation of the average value int averageRange = sum / 3; RangeInCentimeters = averageRange; // Serial.print("Average distance is: "); // Serial.println(averageRange); sum = 0; delay(500); } // 当任务完成时,删除自身 vTaskDelete(NULL); } void xTaskTwo(void *xTask2){ int count = 0; while (1) { // increase brightness for (double i = 20;i<150;i+=0.5){ FastLED.setBrightness(i); myRGBcolor.r = 50; myRGBcolor.b = 50; myRGBcolor.g = 50; fill_solid(leds, NUM_LEDS, myRGBcolor); FastLED.show(); delay(10); } // Reduced brightness for (double i = 150;i>20;i-=0.5){ FastLED.setBrightness(i); myRGBcolor.r = 50; myRGBcolor.b = 50; myRGBcolor.g = 50; fill_solid(leds, NUM_LEDS, myRGBcolor); FastLED.show(); delay(10); } } vTaskDelete(NULL); } void setup() { // put your setup code here, to run once: Serial.begin(115200); delay(500); pinMode(stepPin,OUTPUT); pinMode(dirPin,OUTPUT); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); LEDS.addLeds(leds, NUM_LEDS); FastLED.setBrightness(max_bright); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("WiFi connected!"); configTime(8 * 3600, 0, NTP1, NTP2, NTP3); #if !USE_MULTCORE xTaskCreate( xTaskOne,/* Task function. */ "TaskOne",/* String with name of task. */ 4096,/* Stack size in bytes.*/ NULL,/* parameter passed as input of the task */ 1,/* priority of the task.(configMAx PRIORITIES - 1 being the highest, and @ being the lowest.) */ NULL);/* Task handle.*/ xTaskCreate( xTaskTwo,/* Task function.*/ "TaskTwo",/* String with name of task. */ 4096,/* Stack size in bytes.*/ NULL,/* parameter passed as input of the task */ 2,/* priority of the task.(configMax PRIORITIES - 1 being the highest, and being the lowest.) */ NULL); /* Task handle.*/ #else xTaskCreatepinnedToCore(xTaskOne,"TaskOne",4096,NULL,1,NULL,0); xTaskCreatepinnedToCore(xTaskTwo,"TaskTwo",4896,NULL,2,NULL,1); #endif } void loop() { // put your main code here, to run repeatedly: // setClock(); // int num_sec = second.toInt(); // Serial.print("second is, "); // Serial.println(num_sec); int current_angle; int getCurrentAngle; if(RangeInCentimeters > 300){ setClock(); int num_hour = hour.toInt(); if(num_hour>12){ num_hour = num_hour - 12; } if(num_hour != remember_hour){ remember_hour = num_hour; int target_angle = num_hour * 30; Serial.print("hour is, "); Serial.println(num_hour); getCurrentAngle = getCurrentAngle_S(); current_angle = getCurrentAngle % 360; // Serial.print("current angle is:"); // Serial.println(current_angle); if(target_angle > current_angle){ for(int i = 0; i < (target_angle - current_angle)/0.9; i++){ onestep(); // Serial.print(i); delay(10); } }else{ for(int i = 0; i < (target_angle + 360 - current_angle)/0.9; i++){ onestep(); } } getCurrentAngle = getCurrentAngle_S(); current_angle = getCurrentAngle % 360; Serial.print("current angle is "); Serial.println(current_angle); } } else if(RangeInCentimeters < 300 && RangeInCentimeters > 50){ //分 remember_hour = 60; setClock(); int num_min = minute.toInt(); if(num_min != remember_min){ remember_min = num_min; int target_angle = num_min * 6; Serial.print("min is, "); Serial.println(num_min); getCurrentAngle = getCurrentAngle_S(); current_angle = getCurrentAngle % 360; if(target_angle > current_angle){ for(int i = 0; i < (target_angle - current_angle)/0.9; i++){ onestep(); // Serial.print(i); delay(10); } }else{ for(int i = 0; i < (target_angle + 360 - current_angle)/0.9; i++){ onestep(); } } } // slow(); // Serial.println("MIN"); // Serial.print("当前角度:"); // Serial.print(getCurrentAngle_S()); // Serial.println(" 度"); // getCurrentAngle = getCurrentAngle_S(); // current_angle = getCurrentAngle % 360; // Serial.println("--------down-----"); // Serial.println(current_angle); // Serial.println("--------up------"); } else{ // sec remember_min = 70; remember_hour = 60; setClock(); int num_sec = second.toInt(); if(num_sec != remember_sec){ remember_sec = num_sec; int target_angle = num_sec * 6; Serial.print("sec is, "); Serial.println(num_sec); getCurrentAngle = getCurrentAngle_S(); current_angle = getCurrentAngle % 360; // Serial.print("当前角度:"); // Serial.print(current_angle); if(target_angle > current_angle){ for(int i = 0; i < (target_angle - current_angle)/0.9; i++){ onestep(); // Serial.print(i); delay(10); } }else{ for(int i = 0; i < (target_angle + 360 - current_angle)/0.9; i++){ onestep(); } } getCurrentAngle = getCurrentAngle_S(); current_angle = getCurrentAngle % 360; Serial.print("current angle is "); Serial.println(current_angle); } // Serial.println("SEC"); // Serial.println(" 度"); // getCurrentAngle = getCurrentAngle_S(); // current_angle = getCurrentAngle % 360; // Serial.println("--------down-----"); // Serial.println(current_angle); // Serial.println("--------up------"); } } void onestep(){ digitalWrite(dirPin,HIGH); digitalWrite(stepPin,HIGH); delayMicroseconds(1000); digitalWrite(stepPin,LOW); delayMicroseconds(1000); stepCount_S++; // update the num of step // // 等待一秒 // delay(1000); } void fast(){ digitalWrite(dirPin,HIGH); for(int x = 0; x < STEPS_PER_REV ; x++) { digitalWrite(stepPin,HIGH); delayMicroseconds(1000); digitalWrite(stepPin,LOW); delayMicroseconds(1000); stepCount_F++; } } // void makeOneStep() { // digitalWrite(stepPin, HIGH); // delayMicroseconds(1000); // 步进脉冲的持续时间,用微秒计 // digitalWrite(stepPin, LOW); // delayMicroseconds(100); // 步进脉冲的暂停时间,用微秒计 // stepCount++; // 每步都要更新步数 // } float getCurrentAngle_F() { return stepCount_F * (stepAngle / microstepping); } float getCurrentAngle_S() { return stepCount_S * (stepAngle / microstepping); } //time_t now; //实例化时间 void setClock() { struct tm timeInfo; //声明一个结构体 if (!getLocalTime(&timeInfo)) { //一定要加这个条件判断,否则内存溢出 Serial.println("Failed to obtain time"); return; } //Serial.print(asctime(&timeInfo)); //默认打印格式:Mon Oct 25 11:13:29 2021 // String date = WDAY_NAMES[timeInfo.tm_wday]; // Serial.println(date.c_str()); // sprintf_P(buff1, PSTR("%04d-%02d-%02d %s"), timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday, WDAY_NAMES[timeInfo.tm_wday].c_str()); // String shuju = String(timeInfo.tm_year + 1900); //年 // shuju += "-"; // // shuju += timeInfo.tm_mon + 1; //月 // // shuju += "-"; // shuju += timeInfo.tm_mday; //日 // shuju += " "; // shuju += timeInfo.tm_hour; //时 // shuju += ":"; // shuju += timeInfo.tm_min; // shuju += ":"; // shuju += timeInfo.tm_sec; // shuju += " "; // // shuju += WDAY_NAMES[timeInfo.tm_wday].c_str(); //星期 // Serial.println(shuju.c_str()); minute = timeInfo.tm_min; second = timeInfo.tm_sec; hour = timeInfo.tm_hour; }