当前位置:网站首页>Arduino- use millis() to do two (or more) things at the same time
Arduino- use millis() to do two (or more) things at the same time
2022-07-24 09:46:00 【Jiang tengxia】
The code is as follows
#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
}
}
}
边栏推荐
- Opencv learning Day5
- Development history of the first commercial humanoid biped robot in China
- Vector control of permanent magnet synchronous motor (I) -- mathematical model
- Friends come to interview a unicorn company in Beijing at leisure. The interview question is priced at 25K
- 07 Jason module
- Li Kou 300 longest increasing subsequence dynamic programming
- C#/VB. Net: convert word or EXCEL documents to text
- Learn more about the synchronized lock upgrade process [concurrent programming]
- Huawei wireless device security policy configuration command
- ASI-20220222-Implicit PendingIntent
猜你喜欢

Linux deployment mysql8.0

Li Kou 300 longest increasing subsequence dynamic programming

Embedded development: Tools - optimizing firmware using DRT
![Cyclicbarrier and countdownlatch [concurrent programming]](/img/38/3305a0cdb6de40e1370cc93c8e5014.png)
Cyclicbarrier and countdownlatch [concurrent programming]
![[don't bother with reinforcement learning] video notes (I) 2. Summary of reinforcement learning methods](/img/7e/cb5bf37de4482facda723f3de7deb6.jpg)
[don't bother with reinforcement learning] video notes (I) 2. Summary of reinforcement learning methods

Vscode failed to use SSH Remote Connection (and a collection of other problems)
![[don't bother to strengthen learning] video notes (II) 1. What is Q-learning?](/img/4f/809adc96e30fad03a113acc3df4b61.png)
[don't bother to strengthen learning] video notes (II) 1. What is Q-learning?

Spark Learning: implement compact table command

MySQL基础篇(一)-- SQL基础

Android Version Description security privacy 13
随机推荐
OPENCV学习DAY5
JS bind simulation
Will your NFT disappear? Dfinity provides the best solution for NFT storage
[C language] implementation of three versions of address book small project (including source code)
配置系统环境变量的时候误删了Path怎么办?
PHP debugging tool - socketlog installation and usage
Spark Learning: how to choose different association forms and mechanisms?
JS, the return statement used in the for loop should be placed in the function to terminate the loop, similar to the invalid return problem in break & foreach
What is the cloud native mid platform business architecture?
Huawei wireless device security policy configuration command
Little dolphin "transformed" into a new intelligent scheduling engine, which can be explained in simple terms in the practical development and application of DDS
Basic knowledge of PHP - complete collection of PHP functions
Hands on deep learning (VII) -- bounding box and anchor box
详解LinkedList
[example] v-contextmenu right click menu component
[don't bother to strengthen learning] video notes (IV) 2. Dqn realizes maze walking
Simple parsing JSON strings with regular expressions
[don't bother with reinforcement learning] video notes (I) 3. Why use reinforcement learning?
Es document CRUD
Raspberry Pie:: no space left on device