Ariduino入门笔记——2. Arduino 默认函数(时间函数)

时间函数,是时序控制中非常重要的一环。比如说你要产生PWM信号用于控制舵机转向。那么就需要时间控制相关函数。它类似于C语言里的sleep函数和timeofday函数。对于Arduino来说也有专门的处理与时间相关的函数。

delay(ms)

让设备暂停执行X毫秒

int ledPin = 13;              // LED connected to digital pin 13

void setup() {
  pinMode(ledPin, OUTPUT);    // sets the digital pin as output
}

void loop() {
  digitalWrite(ledPin, HIGH); // sets the LED on
  delay(1000);                // waits for a second
  digitalWrite(ledPin, LOW);  // sets the LED off
  delay(1000);                // waits for a second
}

delayMicroseconds(us)

让设备暂停执行X微秒。另外,根据官网的介绍,当前实际能执行到的微妙最大值为16383微秒。因此如果有大于这个时间,最好使用delay函数。

int outPin = 8;               // digital pin 8

void setup() {
  pinMode(outPin, OUTPUT);    // sets the digital pin as output
}

void loop() {
  digitalWrite(outPin, HIGH); // sets the pin on
  delayMicroseconds(50);      // pauses for 50 microseconds
  digitalWrite(outPin, LOW);  // sets the pin off
  delayMicroseconds(50);      // pauses for 50 microseconds
}

micros()

返回自Arduino板启动后,至今的微秒数。大约每70分钟左右,会跳转从0开始。对于Arduino Portenta系列来说,返回的最小单位是1微秒,对于 16 MHz的Arduino设备来说,返回的最小单位都是4倍微妙时间,对于8MHz的Arduino设备来说,返回的是8倍微秒时间。

unsigned long time;

void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.print("Time: ");
  time = micros();

  Serial.println(time); //prints time since program started
  delay(1000);          // wait a second so as not to send massive amounts of data
}

millis()

返回自Arduino板启动后,至今的毫秒数。当运行时间超过50天以后,会跳转从0开始。

unsigned long myTime;

void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.print("Time: ");
  myTime = millis();

  Serial.println(myTime); // prints time since program started
  delay(1000);          // wait a second so as not to send massive amounts of data
}

时间换算

1 second (秒) = 1000 milliseconds (毫秒) = 1 0 6 10^6 106 microseconds (微秒) = 1 0 9 10^9 109 nanoseconds (纳秒)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值