【问题标题】:LED wont turn on using localtime [ARDUINO]LED 不会使用本地时间打开 [ARDUINO]
【发布时间】:2019-04-29 03:17:39
【问题描述】:
#include <stdio.h>      
#include <time.h>

time_t now;
struct tm *now_tm;
int h,m,s;

void setup(){
    pinMode(D6,OUTPUT);
}

void loop ()
{
    now = time(NULL);
    now_tm = localtime(&now);
    h = now_tm->tm_hour;
    m = now_tm->tm_min;
    s = now_tm->tm_sec;

    if(h == 17 && m == 0 && s==0)
    {
        digitalWrite(D6,HIGH);
        delay(100000);
        digitalWrite(D6,LOW);
    }
}

我不知道为什么我的引脚 D6 上的 LED 在 17:00:00 时不亮

我尝试用其他IF 条件检查它,它工作正常

我在视觉上计算小时、分钟和秒,它们显示了正确的值

【问题讨论】:

  • Arduino 怎么知道现在几点了?在 if 中放一个Serial.println("Debug message");,这样你就可以看到你是否真的进入了条件块(很可能你不是)
  • 什么是 D6? Uno、Nano、Mega 没有 D6 常数
  • 您尝试了哪些其他条件有效?
  • 你用的是什么库?什么实时时钟?

标签: c arduino led localtime


【解决方案1】:

为了让您的代码正常工作,您需要有某种类型的外部时钟(arduino 不知道现在几点),例如DS3231。你可以在这里找到关于它的教程:https://howtomechatronics.com/tutorials/arduino/arduino-ds3231-real-time-clock-tutorial/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多