Image

创意实验室 → 逐一关闭 LED

出版日期: 04.09.2024

逐一关闭 LED


由于 LED 灯非常亮,因此相机无法很好地传达这种效果。事实上,一切看起来都不错。

Arduino草图:

const byte  PinLeds [] = { A0, A1, A2, A3, A4, A5 };

int         Nleds = sizeof(PinLeds);
int         idxLed;

const char *CharList [] = { "|", "/", "-", "\\"};
int         Nchar       = sizeof(CharList)/sizeof(char*);
int         idxChar;

void fLeds ()
{
    for (int n = 0; n < Nleds; n++)
        digitalWrite (PinLeds [n], HIGH);

    digitalWrite (PinLeds [idxLed], LOW);

    if (Nleds <= ++idxLed)
        idxLed = 0;
}

void fChars ()
{
    Serial.print   (CharList [idxChar]);
    Serial.print   ('\r');

    if (Nchar <= ++idxChar)
        idxChar = 0;
}

struct Job {
    const unsigned long MsecPeriod;
    void (*func) (void);
    const char         *desc;

    unsigned long msec;
};

Job jobs [] = {
    {  500, fLeds,  "leds"  },
    {  200, fChars, "chars" },
};

const int Njobs = sizeof(jobs)/sizeof(Job);

void loop ()
{
    unsigned long msec = millis ();

    for (int j = 0; j < Njobs; j++)  {
        if (msec - jobs [j].msec >= jobs [j].MsecPeriod)  {
            jobs [j].msec += jobs [j].MsecPeriod;

            jobs [j].func ();
        }
    }
}

void setup()
{
    Serial.begin(9600);

    for (int n = 0; n < Nleds; n++)
        pinMode (PinLeds [n], OUTPUT);
}

该代码以可调的速度依次打开所有 LED 并关闭每个 LED,并在串行监视器中显示可显示的字符。

该示例清楚地显示了设备的状态,并且开关 LED 表示设备未冻结。





暂时没有评论