Arduino-使用millis()同时做两件(或更多)事情

代码如下

#define ledPin 13 // LED connected to digital pin 13
#define buttonPin 4 // button on pin 4
int value = LOW; // previous value of the LED
int buttonState; // variable to store button state
int lastButtonState; // variable to store last button state
int blinking; // condition for blinking - timer is timing
long interval = 100; // blink interval - change to suit
long previousMillis = 0; // variable to store last time LED was updated
long startTime ; // start time for stop watch
long elapsedTime ; // elapsed time for stop watch
int fractional; // variable used to store fractional part of time
void setup()
{
 Serial.begin(9600);
 pinMode(ledPin, OUTPUT); // sets the digital pin as output
 pinMode(buttonPin, INPUT); // not really necessary, pins default to INPUT anyway
 digitalWrite(buttonPin, HIGH); // turn on pullup resistors. Wire button so that press shorts pin to 
ground.
}
void loop()
{
 // check for button press
 buttonState = digitalRead(buttonPin); // read the button state and store
 if (buttonState == LOW && lastButtonState == HIGH && blinking == false){ // check for a high to 
low transition
 // if true then found a new button press while clock is not running - start the clock
 startTime = millis(); // store the start time
 blinking = true; // turn on blinking while timing
 delay(5); // short delay to debounce switch
 lastButtonState = buttonState; // store buttonState in lastButtonState, to 
compare next time
 }
 else if (buttonState == LOW && lastButtonState == HIGH && blinking == true){ // check for a high to
low transition
 // if true then found a new button press while clock is running - stop the clock and report
 elapsedTime = millis() - startTime; // store elapsed time
 blinking = false; // turn off blinking, all done timing
 lastButtonState = buttonState; // store buttonState in lastButtonState, to 
compare next time
 // routine to report elapsed time - this breaks when delays are in single or double digits. Fix 
this as a coding exercise.
 Serial.print( (int)(elapsedTime / 1000L) ); // divide by 1000 to convert to seconds - then
cast to an int to print
 Serial.print("."); // print decimal point
 fractional = (int)(elapsedTime % 1000L); // use modulo operator to get fractional part 
of time
 Serial.println(fractional); // print fractional part of time
 }
 else{
 lastButtonState = buttonState; // store buttonState in lastButtonState, to 
compare next time
 }
 // blink routine - blink the LED while timing
 // check to see if it's time to blink the LED; that is, is the difference
 // between the current time and last time we blinked the LED bigger than
 // the interval at which we want to blink the LED.
 if ( (millis() - previousMillis > interval) ) {
 if (blinking == true){
 previousMillis = millis(); // remember the last time we blinked the LED
 // if the LED is off turn it on and vice-versa.
 if (value == LOW)
 value = HIGH;
 else
 value = LOW;
 digitalWrite(ledPin, value);
 }
 else{
 digitalWrite(ledPin, LOW); // turn off LED when not blinking
 }
 }
}

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Arduino ESP32-C3板(ESP32-C3 DevKitM)有多个硬件定时器,可以用来生成精确定时的信号,例如PWM信号、定时采样等。在这里,我们将介绍如何在Arduino ESP32-C3板上使用硬件定时器来生成PWM信号。 步骤1:引入头文件 首先,需要在Arduino IDE中引入ESP32-C3的头文件,其中包含了定时器相关的函数和常量。 #include <esp32-hal-timer.h> 步骤2:配置定时器 在Arduino ESP32-C3板上,有四个硬件定时器可供使用,分别为TIMER0、TIMER1、TIMER2和TIMER3。在使用定时器之前,需要先进行配置。以下是一个示例代码段,用于配置TIMER0。 void initTimer0() { // 配置TIMER0为PWM模式 timerAttach(TIMER0, 0, true); timerSetMode(TIMER0, TIMER_PWM_MODE, 1); timerSetFrequency(TIMER0, 1000); timerSetDuty(TIMER0, 0, 50); // 开始TIMER0 timerAlarmEnable(TIMER0); } 在上述代码中,我们首先使用timerAttach()函数将TIMER0与GPIO0引脚绑定,然后使用timerSetMode()函数将其设置为PWM模式。接下来,使用timerSetFrequency()函数设置PWM频率为1000Hz,然后使用timerSetDuty()函数设置PWM占空比为50%。最后,使用timerAlarmEnable()函数启动TIMER0。 步骤3:控制PWM输出 完成定时器的配置后,可以使用timerWrite()函数来控制PWM输出。以下是一个示例代码段,用于控制TIMER0输出PWM信号。 void loop() { for (int i = 0; i <= 100; i++) { timerWrite(TIMER0, i); delay(10); } } 在上述代码中,我们使用一个for循环来逐步增加PWM占空比,从0到100。每次循环使用timerWrite()函数来设置PWM占空比,然后使用delay()函数延时10毫秒。 总结 在Arduino ESP32-C3板上使用硬件定时器可以生成精确的PWM信号,用于控制各种设备和传感器。在使用定时器时,需要注意配置定时器的模式、频率和占空比。同时,也需要注意控制PWM输出的时序,以保证信号的稳定性和准确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农菌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值