c_cpp 米里斯延时

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 米里斯延时相关的知识,希望对你有一定的参考价值。

void setup()

{
  pinMode(13,OUTPUT); // LED output
  pinMode(2,INPUT); // Button input
}

void loop()
{
  static unsigned char ledState = LOW;
  static unsigned char buttonState = LOW;
  static unsigned char lastButtonState = LOW;
  static unsigned long ledCameOn = 0;
  
  // If the LED has been on for at least 5 seconds then turn it off.
  if(ledState == HIGH)
  {
    if(millis()-ledCameOn > 5000)
    {
      digitalWrite(13,LOW);
      ledState = LOW;
    }
  }

  // If the button's state has changed, then turn the LED on IF it is not on already.
  buttonState = digitalRead(2);
  if(buttonState != lastButtonState)
  {
    lastButtonState = buttonState;
    if((buttonState == HIGH) && (ledState == LOW))
    {
      digitalWrite(13,HIGH);
      ledState = HIGH;
      ledCameOn = millis();
    }
  }
}
// https://www.baldengineer.com/millis-ind-on-off-times.html
int ledPin = 5;

// On and Off Times (as int, max=32secs)
const unsigned int onTime = 1000;
const unsigned int offTime = 100;
 
// Tracks the last time event fired
unsigned long previousMillis=0;
 
// Interval is how long we wait
int interval = onTime;
 
// Used to track if LED should be on or off
boolean ledState = true;
 
// Usual Setup Stuff
void setup() {
  pinMode(ledPin, OUTPUT);
}
 
void loop() {
  // Set Pin 13 to state of ledState each timethrough loop()
  // If ledState hasn't changed, neither will the pin
  digitalWrite(ledPin, ledState);
 
  // Grab snapshot of current time, this keeps all timing
  // consistent, regardless of how much code is inside the next if-statement
  unsigned long currentMillis = millis();
 
  // Compare to previous capture to see if enough time has passed
  if ((unsigned long)(currentMillis - previousMillis) >= interval) {
    // Change wait interval, based on current LED state
    if (ledState) {
      // LED is currently on, set time to stay off
      interval = offTime;
    } else {
      // LED is currently off, set time to stay on
      interval = onTime;
    }
    // Toggle the LED's state, Fancy, eh!?
    ledState = !(ledState);
 
    // Save the current time to compare "later"
    previousMillis = currentMillis;
  }
}
//int ledPin1 = 7;
int ledStatus1 = LOW;
long previousMillis1 = 0;
long interval1 = 30;

//int ledPin2 = 6;
int ledStatus2 = HIGH;
long previousMillis2 = 0;
long interval2 = 60;

int ledPins[] = {
  2, 3, 4, 5, 6, 7
};       // an array of pin numbers to which LEDs are attached
int pinCount = 6; 

void setup()
{
	for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }
}

void loop()
{
	if (millis() - previousMillis1 > interval1)
	{
		
		
		if(ledStatus1==LOW){
		    ledStatus1=HIGH;
		} else {
		    ledStatus1=LOW;
		}
		previousMillis1 = millis();
		digitalWrite(ledPins[0], ledStatus1);
		digitalWrite(ledPins[1], ledStatus1);
		digitalWrite(ledPins[2], ledStatus1);
		
		//Serial.println(ledStatus1);
	}

	if (millis() - previousMillis2 > interval2)
	{
		
		//Serial.println("hello");
		if(ledStatus2==LOW){
		    ledStatus2=HIGH;
		} else {
		    ledStatus2=LOW;
		}
		previousMillis2 = millis();
		digitalWrite(ledPins[3], ledStatus2);
		digitalWrite(ledPins[4], ledStatus2);
		digitalWrite(ledPins[5], ledStatus2);
		//Serial.println(ledStatus1);
	}


	
}
int ledPin1 = 7;
int ledStatus1 = LOW;
long previousMillis1 = 0;
long interval1 = 30;

int ledPin2 = 6;
int ledStatus2 = HIGH;
long previousMillis2 = 0;
long interval2 = 40;

void setup()
{
	pinMode(ledPin1, OUTPUT);
	pinMode(ledPin2, OUTPUT);
	//Serial.begin(9600);
}

void loop()
{
	if (millis() - previousMillis1 > interval1)
	{
		
		
		if(ledStatus1==LOW){
		    ledStatus1=HIGH;
		} else {
		    ledStatus1=LOW;
		}
		previousMillis1 = millis();
		digitalWrite(ledPin1, ledStatus1);
		//Serial.println(ledStatus1);
	}

	if (millis() - previousMillis2 > interval2)
	{
		
		Serial.println("hello");
		if(ledStatus2==LOW){
		    ledStatus2=HIGH;
		} else {
		    ledStatus2=LOW;
		}
		previousMillis2 = millis();
		digitalWrite(ledPin2, ledStatus2);
		//Serial.println(ledStatus1);
	}


	
}
#define INTERVAL_MESSAGE1 5000
#define INTERVAL_MESSAGE2 7000
#define INTERVAL_MESSAGE3 11000
#define INTERVAL_MESSAGE4 13000
 
unsigned long time_1 = 0;
unsigned long time_2 = 0;
unsigned long time_3 = 0;
unsigned long time_4 = 0;
 
void print_time(unsigned long time_millis);
 
void setup() {
    Serial.begin(115200);
}
 
void loop() {
    if(millis() > time_1 + INTERVAL_MESSAGE1){
        time_1 = millis();
        print_time(time_1);
        Serial.println("I'm message number one!");
    }
   
    if(millis() > time_2 + INTERVAL_MESSAGE2){
        time_2 = millis();
        print_time(time_2);
        Serial.println("Hello, I'm the second message.");
    }
   
    if(millis() > time_3 + INTERVAL_MESSAGE3){
        time_3 = millis();
        print_time(time_3);
        Serial.println("My name is Message the third.");
    }
   
    if(millis() > time_4 + INTERVAL_MESSAGE4){
        time_4 = millis();
        print_time(time_4);
        Serial.println("Message four is in the house!");
    }
}
 
void print_time(unsigned long time_millis){
    Serial.print("Time: ");
    Serial.print(time_millis/1000);
    Serial.print("s - ");
}
int period = 1000;
unsigned long time_now = 0;
 
void setup() {
    Serial.begin(115200);
}
 
void loop() {
    if(millis() > time_now + period){
        time_now = millis();
        Serial.println("Hello");
    }
   
    //Run other code
}

以上是关于c_cpp 米里斯延时的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 使用两个磁编码器使机器人直线驱动1米

物理学界四大神兽

c_cpp 有一个长度为n个(n <= 100)的数列,该数列定义为从2开始的递增有序偶数(公差为2的等差数列),现在要求你按照顺序每米个数求出一个平均值,如果最后不足m个,则以实际数量求平均值

克里斯·拉特纳的基本信息

朱利安·莫里斯 Julian Morris的资料.

毒瘤数据结构之瑟尼欧里斯树