时钟可能会折叠。
previousclock = millis() 在折叠之前可能会卡在一个高值上。您可以扩展测试以包括 (currentmilis
顺便说一句,它使用了ignas的源代码(OP源代码没有注册是不可读的,我不想注册)
编辑:我从 wakkerbot 复制了下面的片段,并对其进行了一些编辑。这只是为了
演示 wraparound 如何让你的 last_action 时间戳停留在 int 间隔的顶部(如果凹凸值不是 int_max 的除数)
您可能可以稍微简化上面/下面的逻辑,因为您只对内部/外部间隔测试感兴趣。 Stamp 的 typedef 当然应该适应millis() 的类型(unsigned long ?),并删除 fakemillis() 并将对它的引用替换为 millis()。
#include <stdio.h>
#define STAMP_INSIDE 0
#define STAMP_BELOW -1
#define STAMP_ABOVE 1
#define STAMP_BEYONDO -1
/* Intentionally very small, for fast wraparound
** Intentionally signed to stress test the logig.
*/
typedef signed char Stamp;
/* fake clock, returns incrementing value, but folds around
*/
Stamp fakemillis(void)
{
static Stamp ticker =0;
return ticker++;
}
/* Check if "test" is inside or above/below the interval {low,high}
** low and high may have been wrapped around zero (--> low > high)
** return
** 0 := "test" inside interval
** 1 := "test" below interval
** -1 := "test" above interval (but wrapped)
** The two impossible cases return -2.
*/
static int check_interval(Stamp low, Stamp high, Stamp test)
{
switch (4 *(high >= low)
+2 *(test >= low)
+1 *(test > high)
) {
case 0: return STAMP_INSIDE; /* inside (wrapped) */
case 1: /* outside (wrapped) */
return ((Stamp)(low - test) < (Stamp)(test - high)) ? STAMP_BELOW : STAMP_ABOVE;
case 2: break; /* impossible */
case 3: return STAMP_INSIDE; /* inside (wrapped) */
case 4: /* all below */
return ((Stamp)(low - test) < (Stamp)(test - high)) ? STAMP_BELOW : STAMP_ABOVE;
case 5: break; /* impossible */
case 6: return STAMP_INSIDE; /* inside normal case */
case 7: /* all above) */
return ((Stamp)(low - test) < (Stamp)(test - high)) ? STAMP_BELOW : STAMP_ABOVE;
}
return STAMP_BEYONDO;
}
/* Get new clock value, test if it is inside interval {*old, *old+width)
** iff inside: return STAMP_INSIDE;
** iff above (or below) return STAMP_ABOVE or STAMP_BELOW
** and UPDATE *old
*/
static int test_or_set(Stamp *old, Stamp width)
{
Stamp tick;
int diff;
tick = fakemillis();
diff = check_interval( *old, *old+width, tick);
if (!diff) return 0;
*old = tick;
return diff;
}
int main(void) {
Stamp goodlast=0;
Stamp tick=0;
Stamp badlast=0;
int goodtest;
int badtest;
unsigned uu;
for (uu = 0; uu < 260; uu++) {
tick= fakemillis();
if (tick > badlast+10) { badlast=tick; badtest=1; } else {badtest =0;}
goodtest = test_or_set ( &goodlast, 10);
printf("%x:Tick=%x bad=%x, badtest=%d good=%x goodtest=%d\n"
, uu, (unsigned) tick
, (unsigned) badlast, badtest
, (unsigned) goodlast, goodtest
);
}
return 0;
}
如果您在“普通”计算机上编译并运行上述程序,您会看到 badlast 和 badtest 卡住了。恕我直言,您的 arduino 也会发生这种情况。
更新:绝对溢出/翻转。 (GIYF)
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1200662708
Update2:不相关但不好的编码习惯:
#define CMD_SET_SPEED "S"
...
/* Internal configuration */
if(buffer.substring(0,1)==CMD_SET_SPEED) {
updateSpeed(buffer.substring(1));
}
你在这里比较两个字符串。 (这可能按 c++ 的意图处理,但在 C 中它是完全错误的。我还建议用一个巨大的 switch 语句替换重复的 if(...) {...} ,这至少可以避免调用substr() 函数重复。(还是内联?)
更新 20111211:这是一个 wrap-oblivious compare&set 函数,它需要一个 pointer 指向要比较和设置的值,以及预期间隔的宽度:
int test_and_set_if_beyond( unsigned long *pprev, unsigned long width )
{
unsigned long tick, low,high;
low = *pprev;
high = low+width;
tick = millis();
if (low < high) {
if (tick >= low && tick < high ) return 0; /* normal case */
}
else { /* interval is wrapped , clock could have wrapped */
if (tick >= low || tick < high) return 0;
}
*pprev = tick;
return 1;
}
该函数在loop()部分使用如下:
if (test_and_set_if_beyond ( &lightTimer, lightnessCheckPeriod)) {
int newLightness = analogRead(brightnessPin);
if(newLightness-lightness > LIGHT_TRESHOLD) {
say(RESPONSE_FLASH);
}
lightness = newLightness;
}
if (test_and_set_if_beyond ( &pingTimer, pingTimerPeriod)) {
say(RESPONSE_PING);
}
if (test_and_set_if_beyond ( &pingLEDTimer, pingTimerPeriod*2)) {
digitalWrite(failPin, HIGH);
}
feed();
最后:恕我直言,RESET 不起作用的原因是,并非所有全局变量都在 setup() 函数中初始化。另外:我认为您应该摆脱 String 的东西(运行时中有 GC 吗?)并改用普通字符缓冲区。