本帖最后由 pizg 于 2012-12-6 12:26 编辑
樓主提供的代碼, 可以讓pin12 HIGH 1秒 LOW 1秒, pin13 HIGH 5秒 LOW 5秒.
我寫了下列代碼不必include pt.h, 同樣可以達到上述條件:
static int counter1=0,counter2=0;
static int protothread1() //线程1,控制灯1
{
if (counter1 <= 1)
digitalWrite(12,HIGH);
else
digitalWrite(12,LOW);
if (counter1 == 2) counter1 = 0;
}
static int protothread2()
{
if (counter2 <= 5)
digitalWrite(13,HIGH);
else
digitalWrite(13,LOW);
if (counter2 == 10) counter2 = 0;
}
void setup()
{
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}
void loop ()
{
counter1++;
counter2++;
protothread1(); //执行线程1
protothread2(); //执行线程2
delay(1000);
}
在loop裏儘量不要設置delay,
PT應該有更好的用法吧?
|