Arduino 我是否可以在按钮或模式中的某个位置向该代码添加毫秒?

Arduino 我是否可以在按钮或模式中的某个位置向该代码添加毫秒?,arduino,arduino-uno,arduino-c++,Arduino,Arduino Uno,Arduino C++,我需要在我的代码中添加millis,但我不知道在哪里可以添加millis,或者实际需要在哪里添加millis。因为我只使用了延迟到现在,您能否帮助我并告诉我在这段代码中可以在哪里实现millis。如果可以在按钮模式或开关盒中或之后添加millis,因为这会如果模式正确更改会更好,因为现在它们在正常或某些时候按下按钮时会发生更改。您需要按两次按钮才能切换到下一个模式。我将提供所有帮助 const int BUTTON_SWITCH = 8; const int BUTTON_ALARM

我需要在我的代码中添加millis,但我不知道在哪里可以添加millis,或者实际需要在哪里添加millis。因为我只使用了延迟到现在,您能否帮助我并告诉我在这段代码中可以在哪里实现millis。如果可以在按钮模式或开关盒中或之后添加millis,因为这会如果模式正确更改会更好,因为现在它们在正常或某些时候按下按钮时会发生更改。您需要按两次按钮才能切换到下一个模式。我将提供所有帮助

 const int BUTTON_SWITCH = 8;
    const int BUTTON_ALARM = 9;
    const int KNOB = A0;
    const int TEMP = A1;
    const int tempRES = 10000; // the resistance of the NTC at 25'C is 10k ohm
    const int NTC_MATERIAL_CONSTANT = 3950;
    const int RED_LED = 4;
    const int BUZZER = 3;
    int index = 1;
    double value;
    int state = 0;
    unsigned long time_now = 0;
    int period = 1000;
    char incomingOption;
    #include "Display.h"

    void setup() {
      Serial.begin(9600);
      pinMode(BUTTON_SWITCH, INPUT_PULLUP);
      pinMode(BUTTON_ALARM, INPUT_PULLUP);
      pinMode(RED_LED, OUTPUT);
    }

    float get_temperature()
    {
      float temperature, resistance;
      int value;
      value = analogRead(TEMP);
      resistance   = (float)value * tempRES / (1024 - value); // Calculate resistance
      /* Calculate the temperature according to the following formula. */
      temperature  = 1 / (log(resistance / tempRES) / NTC_MATERIAL_CONSTANT + 1 / 298.15) - 273.15;
      return temperature;
    }
    void loop() {
      if (digitalRead(BUTTON_SWITCH) == LOW) {     // button for switching the 3 modes
        index = index + 1;
      }
      if (digitalRead(BUTTON_ALARM) == LOW) {      // button for the alarm
        displayAlarm();
      }
       if (Serial.available()) {
            // Read entire buffer up to newline character
            // Since on the C# side, serialPort1.WriteLine appends a newline character
            String respond = Serial.readStringUntil('\n');
            if (respond == "RESET") {
                digitalWrite(RED_LED, LOW);
                digitalWrite(BUZZER, LOW);
            }
        }
      if (index > 3) {       // when the code is on the last mode press the button to turn back to the first mode
        index = 1;
      }

      get_temperature();
      float celcius;
      celcius = get_temperature();

      if(celcius<16 || celcius>27){        // if the temperature becomes less than 16 degrees or goes higher than 27 degrees turn on the alarm
        displayAlarm();
      }


      switch (index) {
        case 1: displayTime(); break;          // switch between the 3 different modes
        case 2: displayTemp(); break;
        case 3: displayAngle(); break;
      }

    }


    void displayTime() {
      float timer = Serial.parseFloat();   // take the current time from the c# application and display it on the arduino board
      Display.show(timer);
    }

    void displayTemp() {

      float celcius;
      celcius = get_temperature();      // mode for displaying the current temperature
      Display.show(celcius);
    }


    void displayAngle() {
      int value = analogRead(KNOB);  // read and save analog value from the potentionmeter 
      value = map(value, 0, 1023, 0, 30); // Map value 0-1023 to 0-30
      Display.show(value);
    }

    void displayAlarm() {
      Serial.println("Alarm");
      digitalWrite(RED_LED, HIGH);
      tone(BUZZER, 1500, 700);

    }
const int BUTTON_开关=8;
常数int按钮\u报警=9;
常数int旋钮=A0;
常数int TEMP=A1;
常数int tempRES=10000;//NTC在25℃时的电阻为10k欧姆
材料常数=3950;
红色发光二极管常数=4;
常数int蜂鸣器=3;
int指数=1;
双重价值;
int state=0;
无符号长时间_now=0;
整数周期=1000;
字符输入选项;
#包括“Display.h”
无效设置(){
Serial.begin(9600);
pinMode(按钮开关、输入上拉);
pinMode(按钮报警、输入下拉);
引脚模式(红色发光二极管,输出);
}
浮子温度()
{
浮子温度、电阻;
int值;
数值=模拟读数(温度);
电阻=(浮点)值*温度/(1024-值);//计算电阻
/*根据以下公式计算温度*/
温度=1/(对数(电阻/温度)/NTC_材料常数+1/298.15)-273.15;
返回温度;
}
void循环(){
如果(digitalRead(按钮开关)==低){//用于切换3种模式的按钮
指数=指数+1;
}
如果(数字读取(按钮报警)=低){//报警按钮
显示报警();
}
if(Serial.available()){
//读取整个缓冲区直到换行符
//因为在C#端,serialPort1.WriteLine附加了一个换行符
String respond=Serial.readStringUntil('\n');
如果(响应=“重置”){
数码写入(红色发光二极管,低电平);
数字写入(蜂鸣器,低电平);
}
}
如果(索引>3){//当代码处于最后一种模式时,按下按钮返回到第一种模式
指数=1;
}
获取温度();
浮萍;
celcius=获取温度();
如果(celcius27){//如果温度低于16度或高于27度,则打开警报
显示报警();
}
开关(索引){
案例1:displayTime();break;//在3种不同模式之间切换
案例2:displayTemp();中断;
案例3:displayAngle();中断;
}
}
void displayTime(){
float timer=Serial.parseFloat();//从c#应用程序获取当前时间,并将其显示在arduino板上
显示。显示(计时器);
}
void displayTemp(){
浮萍;
celcius=get_temperature();//显示当前温度的模式
显示。显示(celcius);
}
void displayAngle(){
int value=analogRead(旋钮);//从电位计读取并保存模拟值
值=映射(值,0,1023,0,30);//映射值0-1023到0-30
显示。显示(值);
}
void displayAlarm(){
Serial.println(“报警”);
数码写入(红色发光二极管,高电平);
铃声(蜂鸣器,1500700);
}

您是否能按照要求准备MRE?您试图完成的目标一点也不清楚。您想如何处理
millis()
?哦,请把你的散文编排成可以理解的格式;源代码的格式真的很好,对于初学者来说,谢谢!这不是C btw,更改了您的标签。。。这是你的密码。您可以在任何地方使用
millis()
。我们怎么知道你想在哪里读时间戳?我不明白你的问题。我的问题是,我真的不知道我可以把毫秒放在哪里,因为我需要在我的代码中有毫秒,即使我的代码中的所有东西都正常工作。如果你能告诉我哪里可以在你认为有必要的地方添加毫秒,我真的不明白毫秒只是延迟。所以如果你能把米利斯在一个你认为会有帮助的地方,我会解释它。谢谢你的ASN版本。它不是关于什么特别的“放置米利斯的地方”。你写一个if语句,检查自上次发生某件事以来有多长时间了,然后你把它放在if语句中。只要代码的执行不断地到达那个点,检查if语句,那么在哪里编写它就无关紧要了。