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中, 标准的程序入口main函数在内部被定义, 用户只需要关心以下两个函数: setup() 当Arduino板起动时setup()函数会被调用。用它来初始化变量,引脚模式,开始使用某个库,等等。该函数在Arduino板的每次上电和复位时只运行一次。 loop() 在创建setup函数,该函数初始化和设置初始值,loop()函数所事的正如其名,连续循环,允许你的程序改变状态和响应事件。可以用它来实时控制arduino板。 示例: int buttonPin = 3; void setup() { Serial.begin(9600); //初始化串口 pinMode(buttonPin, INPUT); //设置3号引脚为输入模式 } void loop() { if (digitalRead(buttonPin) == HIGH) serialWrite('H'); else serialWrite('L'); delay(1000); } 控制语句 if if,用于与比较运算符结合使用,测试是否已达到某些条件,例如一个输入数据在某个范围之外。使用格式如下: if (value > 50) { // 这里加入你的代码 } 该程序测试value是否大于50。如果是,程序将执行特定的动作。换句话说,如果圆括号中的语句为真,大括号中的语句就会执行。如果不是,程序将跳过这段代码。大括号可以被省略,如果这么,下一行(以分号结尾)将成为唯一的条件语句。 if (x > 120) digitalWrite(LEDpin, HIGH); if (x > 120) digitalWrite(LEDpin, HIGH); if (x > 120){ digitalWrite(LEDpin, HIGH); } if (x > 120){ digitalWrite(LEDpin1, HIGH); digitalWrite(LEDpin2, HIGH); } // 都是正确的 圆括号中要被计算的语句需要一个或多个操作符。 if...else 与基本的if语句相比,由于允许多个测试组合在一起,if/else可以使用多的控制流。例如,可以测试一个模拟量输入,如果输入值小于500,则采取一个动作,而如果输入值大于或等于500,则采取另一个动作。代码看起来像是这样: if (pinFiveInput < 500) { // 动作A } else { // 动作B } else中可以进行另一个if测试,这样多个相互独立的测试就可以同时进行。每一个测试一个接一个地执行直到遇到一个测试为真为止。当发现一个测试条件为真时,与其关联的代码块就会执行,然后程序将跳到完整的if/else结构的下一行。如果没有一个测试被验证为真。缺省的else语句块,如果存在的话,将被设为默认行为,并执行。 注意:一个else if语句块可能有或者没有终止else语句块,同理。每个else if分支允许有无限多个。
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、付费专栏及课程。

余额充值