void setup() { // put your setup code here, to run once: } void loop() { // put your main code here, to run repeatedly: } |
void setup() { // put your setup code here, to run once: pinMode(LED_BUILTIN,OUTPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(LED_BUILTIN,HIGH); delay(100); digitalWrite(LED_BUILTIN,LOW); delay(100); } |
uint8_t smile[8][12] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; uint8_t wink[8][12] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; uint8_t cry[8][12] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; |
#include "Arduino_LED_Matrix.h" ArduinoLEDMatrix matrix; void setup() { Serial.begin(115200); matrix.begin(); } uint8_t smile[8][12] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; uint8_t wink[8][12] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; uint8_t cry[8][12] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; void loop() { matrix.renderBitmap(smile, 8, 12); delay(1000); matrix.renderBitmap(wink, 8, 12); delay(1000); matrix.renderBitmap(cry, 8, 12); delay(1000); } |
|
#ifndef _Waves_h_ #define _Waves_h_ static int waveformsTable[120] = { 0x22, 0x44, 0x66, 0x88, 0xaa, 0xcc, 0xee, 0x110, 0x132, 0x154, 0x176, 0x198, 0x1ba, 0x1dc, 0x1fe, 0x220, 0x242, 0x264, 0x286, 0x2a8, 0x2ca, 0x2ec, 0x30e, 0x330, 0x352, 0x374, 0x396, 0x3b8, 0x3da, 0x3fc, 0x41e, 0x440, 0x462, 0x484, 0x4a6, 0x4c8, 0x4ea, 0x50c, 0x52e, 0x550, 0x572, 0x594, 0x5b6, 0x5d8, 0x5fa, 0x61c, 0x63e, 0x660, 0x682, 0x6a4, 0x6c6, 0x6e8, 0x70a, 0x72c, 0x74e, 0x770, 0x792, 0x7b4, 0x7d6, 0x7f8, 0x81a, 0x83c, 0x85e, 0x880, 0x8a2, 0x8c4, 0x8e6, 0x908, 0x92a, 0x94c, 0x96e, 0x990, 0x9b2, 0x9d4, 0x9f6, 0xa18, 0xa3a, 0xa5c, 0xa7e, 0xaa0, 0xac2, 0xae4, 0xb06, 0xb28, 0xb4a, 0xb6c, 0xb8e, 0xbb0, 0xbd2, 0xbf4, 0xc16, 0xc38, 0xc5a, 0xc7c, 0xc9e, 0xcc0, 0xce2, 0xd04, 0xd26, 0xd48, 0xd6a, 0xd8c, 0xdae, 0xdd0, 0xdf2, 0xe14, 0xe36, 0xe58, 0xe7a, 0xe9c, 0xebe, 0xee0, 0xf02, 0xf24, 0xf46, 0xf68, 0xf8a, 0xfac, 0xfce, 0xff0 }; static int wave_sin[256] = { 0x80,0x83,0x85,0x88,0x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x2A,0x2C,0x2D,0x2F,0x30,0x32,0x34,0x35, 0x37,0x39,0x3B,0x3D,0x3F,0x41,0x43,0x45, 0x47,0x49,0x4B,0x4D,0x4F,0x52,0x54,0x56, 0x59,0x5B,0x5D,0x60,0x62,0x65,0x67,0x69, 0x6C,0x6E,0x71,0x73,0x76,0x78,0x7B,0x7D }; #endif |
#include "Waves.h" int i = 0; void setup() { analogWriteResolution(8); // set the analog output resolution to 12 bit (4096 levels) } void loop() { analogWrite(DAC, wave_sin); // write the selected waveform on DAC0 i++; if (i == 256) // Reset the counter to repeat the wave i = 0; delayMicroseconds(10); // Hold the sample value for the sample time } |
#include "Waves.h" #include <OPAMP.h> int i = 0; void setup() { OPAMP.begin(OPAMP_SPEED_HIGHSPEED); analogWriteResolution(8); // set the analog output resolution to 12 bit (4096 levels) } void loop() { analogWrite(DAC, wave_sin/2); // write the selected waveform on DAC0 i++; if (i == 256) // Reset the counter to repeat the wave i = 0; delayMicroseconds(10); // Hold the sample value for the sample time } |
#include "Waves.h" #include <OPAMP.h> int i = 0; void setup() { OPAMP.begin(OPAMP_SPEED_HIGHSPEED); analogWriteResolution(8); // set the analog output resolution to 12 bit (4096 levels) analogReadResolution(8); //change to 14-bit resolution Serial.begin(9600); } void loop() { analogWrite(DAC, wave_sin/2); // write the selected waveform on DAC0 i++; if (i == 256) // Reset the counter to repeat the wave i = 0; int reading = analogRead(A4); // returns a value between 0-16383 delayMicroseconds(10); // Hold the sample value for the sample time Serial.print(reading); Serial.print("\n"); } |
topic:homeassistant/sensor/sensorBedroomH/config |
{ "device_class":"humidity", "name":"Humidity", "state_topic":"homeassistant/sensor/sensorBedroom/state", "unit_of_measurement":"%", "value_template":"{{ value_json.humidity}}", "unique_id":"hum01ae", "device":{ "identifiers":[ "bedroom01ae" ], "name":"Bedroom" } } |
#include <WiFi.h> #include <PubSubClient.h> #include <ArduinoJson.h> // 替换为你的网络信息 const char* ssid = "WIFI名称"; const char* password = "WIFI密码"; // 替换为你的Mosquitto broker信息 const char* mqtt_server = "IPV4地址"; const int mqtt_port = 1883; const char* mqtt_user = "HA用户名"; const char* mqtt_password = "HA密码"; // 初始化WiFi和MQTT客户端 WiFiClient espClient; PubSubClient client(espClient); // 传感器数据发布主题 const char* topic = "homeassistant/sensor/sensorBedroom/state"; void setup() { Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, mqtt_port); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); // 假设你有一个温度传感器,获取温度数据 float temperature = readTemperature(); StaticJsonDocument<200> jsonDoc; jsonDoc["humidity"] = temperature; char jsonBuffer[512]; serializeJson(jsonDoc, jsonBuffer); client.publish(topic, jsonBuffer); // 每隔5秒发送一次数据 delay(5000); } void setup_wifi() { delay(10); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("Connected!"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); if (client.connect("ArduinoClient", mqtt_user, mqtt_password)) { Serial.println("connected"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } float readTemperature() { // 在这里读取实际传感器的数据 // 示例中返回一个固定值 return 21.5; } |
#include <Wire.h> #include "Adafruit_SHT4x.h" Adafruit_SHT4x sht4 = Adafruit_SHT4x(); void setup() { // put your setup code here, to run once: Serial.begin(115200); Wire1.begin(); if(sht4.begin(&Wire1) == false) { Serial.println("SHT40 not detected. Please check wiring. Freezing."); while (1) ; } sht4.setPrecision(SHT4X_HIGH_PRECISION); sht4.setHeater(SHT4X_NO_HEATER); Serial.println("SHT40 acknowledged."); } void loop() { // put your main code here, to run repeatedly: sensors_event_t humidity, temp; sht4.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println("℃"); Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("%"); delay(1000); } |
#include <Wire.h> #include "Adafruit_SHT4x.h" #include <WiFi.h> #include <PubSubClient.h> #include <ArduinoJson.h> Adafruit_SHT4x sht4 = Adafruit_SHT4x(); // 替换为你的网络信息 const char* ssid = "WIFI名称"; const char* password = "WIFI密码"; // 替换为你的Mosquitto broker信息 const char* mqtt_server = "IPV4地址"; const int mqtt_port = 1883; const char* mqtt_user = "HA用户名"; const char* mqtt_password = "HA密码"; // 初始化WiFi和MQTT客户端 WiFiClient espClient; PubSubClient client(espClient); // 传感器数据发布主题 const char* topic = "homeassistant/sensor/sensorBedroom/state"; void setup() { // put your setup code here, to run once: Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, mqtt_port); Wire1.begin(); if(sht4.begin(&Wire1) == false) { Serial.println("SHT40 not detected. Please check wiring. Freezing."); while (1) ; } sht4.setPrecision(SHT4X_HIGH_PRECISION); sht4.setHeater(SHT4X_NO_HEATER); Serial.println("SHT40 acknowledged."); } void loop() { // put your main code here, to run repeatedly: delay(1000); if (!client.connected()) { reconnect(); } client.loop(); // 假设你有一个温度传感器,获取温度数据 float temperature = readTemperature(); float humidity = readHumidity(); StaticJsonDocument<200> jsonDoc; jsonDoc["humidity"] = humidity; jsonDoc["temperature"] = temperature; char jsonBuffer[512]; serializeJson(jsonDoc, jsonBuffer); client.publish(topic, jsonBuffer); // 每隔5秒发送一次数据 delay(5000); } void setup_wifi() { delay(10); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("Connected!"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); if (client.connect("ArduinoClient", mqtt_user, mqtt_password)) { Serial.println("connected"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); } } } float readTemperature() { // 在这里读取实际传感器的数据 // 示例中返回一个固定值 sensors_event_t humidity,temp; sht4.getEvent(&humidity,&temp);// populate temp and humidity objects with fresh data // Serial.print("Temperature: "); // Serial.print(temp.temperature); // Serial.println("℃"); // Serial.print("Humidity: "); // Serial.print(humidity.relative_humidity); // Serial.println("%"); return temp.temperature; } float readHumidity() { // 在这里读取实际传感器的数据 // 示例中返回一个固定值 sensors_event_t humidity,temp; sht4.getEvent(&humidity,&temp);// populate temp and humidity objects with fresh data // Serial.print("Temperature: "); // Serial.print(temp.temperature); // Serial.println("℃"); // Serial.print("Humidity: "); // Serial.print(humidity.relative_humidity); // Serial.println("%"); return humidity.relative_humidity; } |
引用: 秦天qintian0303 发表于 2024-10-2 11:37 wave_sin[256] 这种方式是不是曲线更加的平滑啊啊
数组位数越多,能形成的波形应该会越还原。DAC应该也是可以调整到12位精度的,这两个方式应该都可以让波形更加的平滑一些。