millis() ใน arduino
/in Arduino /by อ.เอ้ดแบบที่ 1
unsigned long period = 1000; //ระยะเวลาที่ต้องการรอ
unsigned long last_time = 0; //ประกาศตัวแปรเป็น global เพื่อเก็บค่าไว้ไม่ให้ reset จากการวนloop
void setup() {
Serial.begin(115200);
}
void loop() {
if( millis() - last_time > period) {
last_time = millis(); //เซฟเวลาปัจจุบันไว้เพื่อรอจนกว่า millis() จะมากกว่าตัวมันเท่า period
Serial.println("Hello arduinona!");
}
}
ขอบคุณ: https://www.modulemore.com/article/3/%E0%B8%A5%E0%B8%94%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%83%E0%B8%8A%E0%B9%89-delay-%E0%B8%AB%E0%B8%B1%E0%B8%99%E0%B8%A1%E0%B8%B2%E0%B9%83%E0%B8%8A%E0%B9%89-millis-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B8%94%E0%B8%B5%E0%B8%81%E0%B8%A7%E0%B9%88%E0%B8%B2
แนวที่ 2
int period = 1000;
unsigned long time_now = 0;
void setup() {
Serial.begin(115200);
}
void loop() {
time_now = millis();
Serial.println(“Hello”);
while(millis() < time_now + period){
//wait approx. [period] ms
}
}
NodeMCU ESP8266 TM1637 NTP DHT11
/in Arduino /by อ.เอ้ด- NODEMCU ESP8266
- 4 Digit Seven Segment TM1637
- DHT11
- SevenSegmentTM1637.h
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include “SevenSegmentTM1637.h”
#include “SevenSegmentExtended.h”
#include <DHT.h>
#define CLK D2 // Define the connections pins:
#define DIO D3
#define DHTPIN D5 // Pin sensor is connected to
char cdmy[11];
// Set DHT type, uncomment whatever type you’re using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
float temp_c ;
float humi ;
char tempf[4];
float tempr;
char humif[4];
//SevenSegmentTM1637 display(CLK, DIO); // Create display object of type TM1637Display:
SevenSegmentExtended display(CLK, DIO);
DHT dht = DHT(DHTPIN, DHTTYPE);
const char *ssid = “xxxxx”;
const char *password = “xxxxxxx”;
//const long utcOffsetInSeconds = 19802;
const long utcOffsetInSeconds = 25200;
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, “1.th.pool.ntp.org”, utcOffsetInSeconds);
void setup(){
display.begin(); // initializes the display
display.setBacklight(100); // set the brightness to 100 %
Serial.begin(9600);
Serial.println();
Serial.println(“DHTxx test!”);
dht.begin();
// Begin serial communication at a baud rate of 9600:
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print (“.”);
}
timeClient.begin();
}
void loop() {
// Read the temperature as Celsius and Fahrenheit:
temp_c = dht.readTemperature();
humi = dht.readHumidity();
// print both to serial console
Serial.println();
Serial.print(temp_c);
Serial.print(“<-T H->”);
Serial.println(humi);
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime(); //get date
int currentHour = timeClient.getHours();
int currentMinute = timeClient.getMinutes();
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
sprintf(cdmy,” %02d-%02d-%02d “,ptm->tm_mday,ptm->tm_mon+1,ptm->tm_year+2443);
for (int i = 0; i < 2; i++) {
display.clear(); // clear the display
display.print(cdmy); // print right->left day month year
delay(100);
}
// print time hour min
//for (int i = 0; i < 2; i++) {
display.clear(); // clear the display
// print hour min for
display.printTime(currentHour,currentMinute, true); //print hour.min
//display.blink(); // blink
delay(1000);
// }
display.setColonOn(1); // open dot . led version dot if use time led : show led
display.printNumber(static_cast<int>(temp_c * 100),false,false,false);
display.printRaw(88,3); // display c at location 3
display.blink();
delay(2000);
display.printRaw(128,1);
display.printNumber(static_cast<int>(humi * 100),false,false,false);
display.printRaw(116,3); // display h at location 3
display.blink();
delay (3000);
display.setColonOn(false); // close colon
display.clear();
}xxx
TM1637.h Groove
/in Arduino /by อ.เอ้ด#include “TM1637.h”
TM1637 tm1637(CLK, DIO);
tm1637.set();
tm1637.init();
tm1637.set(BRIGHTEST);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
tm1637.clearDisplay(); // clear the display
tm1637.displayStr();
tm1637.displayStr(‘ABCD’);
// Without specifying decimal pointt it displays int
tm1637.displayNum(num);
// Colon light up as decimal point
tm1637.displayNum(num, 2);
delay(2000);
// Note that adding 3 digits (-123450) makes number out of int range
tm1637.displayNum(num, 3);
delay(2000);
// Display negative numbers without sign
tm1637.displayNum(num, 0, false);
delay(2000);
/ LOVE, O=0
tm1637.displayStr(“L0UE”);
delay(2000);
// loop
tm1637.displayStr(“HELL0-1234567890”, 1000);
delay(2000);
// ON
tm1637.displayStr(“0N”);
delay(2000);
// OFF
tm1637.displayStr(“0FF”);
delay(2000);
/tm1637.display(0, ListDisp[0]);
tm1637.display(1, ListDisp[1]);
tm1637.display(2, ListDisp[2]);
tm1637.display(3, ListDisp[3]);
Display1.display(0, 1); // แสดงเลข 1 ที่หลัก 0
delay(1000); // *** หลัก (0,1,2,3) นับจากซ้ายตามลำดับ
Display1.display(1, 2); // แสดงเลข 2 ที่หลัก 1
delay(1000);
Display1.display(2, 5); // แสดงเลข 5 ที่หลัก 2
delay(1000);
Display1.display(3, 8); // แสดงเลข 8 ที่หลัก 3
delay(1000);
Display1.display(0, CHAR_BLANK); // แสดง ‘ ‘ (ช่องว่าง) ที่หลัก 0
delay(1000);
Display1.display(1, CHAR_BLANK); // แสดง ‘ ‘ (ช่องว่าง) ที่หลัก 1
delay(1000);
Display1.display(2, CHAR_BLANK); // แสดง ‘ ‘ (ช่องว่าง)ที่หลัก 2
delay(1000);
Display1.display(3, CHAR_BLANK); // แสดง ‘ ‘ (ช่องว่าง) ที่หลัก 3
delay(1000);
Display1.point(POINT_ON); // เปิดแสดงจุด “:” ระหว่างหลัก 1 กับ หลัก 2
Display1.clearDisplay(); // ต้องตาม Function นี้ จุดถึงจะแสดง
delay(1000);
tm1637.point(POINT_OFF); // ปิดแสดงจุด “:” ระหว่างหลัก 1 กับ หลัก 2
tm1637.clearDisplay(); // ต้องตาม Function นี้ จุดถึงจะปิดไม่แสดง
delay(1000);
Display1.point(POINT_ON);
Display1.clearDisplay();
Display1.display(0, 1);
Display1.display(1, 2);
Display1.display(2, 3);
Display1.display(3, 4);
delay(1000);
Arduino C TM structure
/in Arduino /by อ.เอ้ดstruct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
sprintf(cdmy,” %02d-%02d-%02d “,ptm->tm_mday,ptm->tm_mon+1,ptm->tm_year+2443);
sprintf(chm,”%02d%02d”,ptm->tm_hour,ptm->tm_min);
Member | Type | Meaning | Range |
---|---|---|---|
tm_sec | int | seconds after the minute | 0-61* |
tm_min | int | minutes after the hour | 0-59 |
tm_hour | int | hours since midnight | 0-23 |
tm_mday | int | day of the month | 1-31 |
tm_mon | int | months since January | 0-11 |
tm_year | int | years since 1900 |
|
tm_wday | int | days since Sunday | 0-6 |
tm_yday | int | days since January 1 | 0-365 |
tm_isdst | int | Daylight Saving Time flag |
ESP8266 NODEMCU TM1637 NTP
/in Arduino /by อ.เอ้ด#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <TM1637Display.h> //
#define CLK D2 // Define the connections pins:
#define DIO D3
TM1637Display display = TM1637Display(CLK, DIO); // Create display object of type TM1637Display:
const char *ssid = “XXXX”;
const char *password = “xxxxx”;
//const long utcOffsetInSeconds = 19802;
const long utcOffsetInSeconds = 25200;
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, “1.th.pool.ntp.org”, utcOffsetInSeconds);
void setup(){
Serial.begin(9600);
Serial.println();
// Begin serial communication at a baud rate of 9600:
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( “.” );
}
timeClient.begin();
}
void loop() {
int A,B;
display.setBrightness(7); // Set the brightness:
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime(); //get date
Serial.print(“Epoch Time: “);
Serial.println(epochTime);
int currentHour = timeClient.getHours();
Serial.print(“Hour: “);
Serial.println(currentHour);
int currentMinute = timeClient.getMinutes();
Serial.print(“Minutes: “);
Serial.println(currentMinute);
//Get a time structure
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
Serial.print(“Month day: “);
Serial.print(monthDay);
display.showNumberDec(monthDay, false, 2, 1);
delay(3000);
display.clear();
int currentMonth = ptm->tm_mon+1;
Serial.print(” Month: “);
Serial.print(currentMonth);
display.showNumberDec(currentMonth, true, 2, 1);
delay(3000);
display.clear();
//int currentYear = ptm->tm_year+1900;
int currentYear = ptm->tm_year+2443;
Serial.print(” Year: “);
Serial.println(currentYear);
display.showNumberDec(currentYear, false, 4, 0);
delay(3000);
display.clear();
A = timeClient.getHours() * 100 + timeClient.getMinutes();
B = timeClient.getSeconds();
if((B % 2) == 0)
{
display.showNumberDecEx(A, 0b01000000 , false, 4, 0);
}
else
{
display.showNumberDecEx(A, 0b00000000 , false, 4, 0);
}
delay (3000);
display.clear();
}
.
NODEMCU 4DIGIT TM1637 DHT11
/in Arduino /by อ.เอ้ด#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <TM1637Display.h> //
#include <DHT.h>
#define CLK D2 // Define the connections pins:
#define DIO D3
#define DHTPIN D5 // Pin sensor is connected to
// Create degree Celsius symbol:
const uint8_t celsius[] = {
// SEG_A | SEG_B | SEG_F | SEG_G, // Circle
SEG_G | SEG_E | SEG_D // c
};
const uint8_t hum[] = {
// SEG_A | SEG_B | SEG_F | SEG_G, // Circle
SEG_F | SEG_E | SEG_G | SEG_C // h
};
// Set DHT type, uncomment whatever type you’re using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
float temp_c = 0.0;
float humi = 0.0;
TM1637Display display = TM1637Display(CLK, DIO); // Create display object of type TM1637Display:
DHT dht = DHT(DHTPIN, DHTTYPE);
const char *ssid = “xxxx”;
const char *password = “yyyyyy”;
//const long utcOffsetInSeconds = 19802;
const long utcOffsetInSeconds = 25200;
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, “1.th.pool.ntp.org”, utcOffsetInSeconds);
void setup(){
Serial.begin(9600);
Serial.println();
Serial.println(“DHTxx test!”);
dht.begin();
// Begin serial communication at a baud rate of 9600:
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( “.” );
}
timeClient.begin();
}
void loop() {
int A,B;
// Read the temperature as Celsius and Fahrenheit:
temp_c = dht.readTemperature();
humi = dht.readHumidity();
// print both to serial console
Serial.printf(“%2.2fC %2.2f%%\n”, temp_c, humi);
timeClient.update();
display.setBrightness(7); // Set the brightness:
A = timeClient.getHours() * 100 + timeClient.getMinutes();
B = timeClient.getSeconds();
if((B % 2) == 0)
{
display.showNumberDecEx(A, 0b01000000 , false, 4, 0);
}
else
{
display.showNumberDecEx(A, 0b00000000 , false, 4, 0);
}
delay (3000);
display.clear();
display.showNumberDecEx(static_cast<int>(temp_c * 100), 0x40);
display.setSegments(celsius, 1, 3);
delay (30000);
display.clear();
display.showNumberDecEx(static_cast<int>(humi * 100), 0x40);
display.setSegments(hum, 1, 3);
delay (30000);
display.clear();
}
ElectroDragon ESP8266 Flash
/in Arduino /by อ.เอ้ดESP8266 ESP01 BME280 BMp280
/in Arduino /by อ.เอ้ดuse ESP8266 ESP01 Black PCB 1Mb ESPEasy Firmware 1M
Interesting links
Here are some interesting links for you! Enjoy your stay :)Categories
Archive
- October 2024
- January 2024
- September 2023
- August 2023
- June 2023
- May 2023
- January 2023
- December 2022
- September 2022
- July 2022
- June 2022
- May 2022
- April 2022
- March 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- August 2021
- July 2021
- June 2021
- May 2021
- March 2021
- February 2021
- January 2021
- December 2020
- November 2020
- October 2020
- September 2020
- August 2020
- July 2020
- June 2020
- May 2020
- April 2020
- March 2020
- January 2020
- December 2019
- November 2019
- October 2019
- September 2019
- July 2019
- June 2019
- September 2018
- August 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- September 2017
- August 2017
- July 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- March 2016
- October 2015
- September 2015
- August 2015
- March 2015
- March 2014
- October 2011