指示灯闪烁,无延迟

时间:2018-12-03 02:57:43

标签: arduino-uno

所以我正在创建代码,以便可以读取和存储温度值 至少保存8小时,然后将其存储在Arduino EEPROM中。我还希望内置LED每秒闪烁一次,而温度传感器每分钟记录一次。我写了以下内容,但我只让LED保持一整分钟,然后再关闭一分钟,依此类推。我希望它不断闪烁。我知道是因为我的延迟(6000),但我不知道如何解决。任何帮助表示赞赏,谢谢!

    #include <EEPROM.h> //Librería para controlar la EEPROM de la Arduino
    float tempC; //Initialize variable for storing Temperature
    int tempPin = 0; //Conected at A0
    int addr = 0; //Cantidad de espacios (bytes) iniciales
    int count = 0; //counter needed to stop at a certain point before overflowing the EEPROM memory
    int Voltages[501]; // Array with space for 501 data Points.A little over 8 hours. Holding integer values of Voltages.
    float Temps[501]; //Holds float values for Temperatures
    const int SWITCH = 8; //set switch to port 8
    int ledState = LOW; // ledState used to set the LED
    unsigned long previousMillis = 0;        // will store last time LED was updated
    const long interval = 1000;           // blink once per second

  void setup(){
  Serial.begin(115200);
  pinMode(LED_BUILTIN,OUTPUT); 
  pinMode(SWITCH,INPUT_PULLUP);}

  void loop(){
  int i;
  if (digitalRead(SWITCH)==HIGH & count<=501){

  for (int i=0;i<EEPROM.length();i++){
    EEPROM.write(i,0);} // Clears the EEPROM so that only new recorded values are shown. In case of terminating before 8h

  Serial.println("-------Recording-------");
    unsigned long currentMillis = millis(); // current time
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis; // save the last time the LED blinked
      if (ledState == LOW) {// if the LED is off turn it on and vice-versa:
        ledState = HIGH;} else {
        ledState = LOW;}
        digitalWrite(LED_BUILTIN, ledState);}

  for (i=0; i<501; i++){

    count = count+1;
    int v = analogRead(tempPin); // reads voltage 
    Voltages[i] = v; 
    Serial.println(i);
    Serial.println(Voltages[i]); 
    delay(60000); 
    EEPROM.put(addr, Voltages[i]); //stores voltages in EEPROM
    addr= addr +2;}}

1 个答案:

答案 0 :(得分:4)

我认为您正在寻找的解决方案涉及使用计时器中断。这将执行中断服务程序(可能还会闪烁),而与循环功能的其余部分无关。这可能会给您带来更好的了解:Arduino timer interrupts