Мигать светодиодом без задержки - PullRequest
0 голосов
/ 03 декабря 2018

Итак, я создаю код, чтобы я мог считывать и сохранять значения температуры не менее 8 часов и сохранять их в Edurom Arduino.Я также хочу, чтобы встроенный светодиод мигал раз в секунду, а датчик температуры - раз в минуту.Я написал следующее, но у меня остался светодиод, который горит целую минуту, затем выключается еще минуту и ​​так далее.Я хочу, чтобы он постоянно мигал.Я знаю, что это из-за моей задержки (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 голосов
/ 04 декабря 2018

Я думаю, что решение, которое вы ищете, включает использование прерываний по таймеру.Это выполнит подпрограмму обработки прерывания (может также быть мигающим светодиодом) независимо от того, что происходит в остальной части вашей функции цикла.Это может помочь вам лучше видеть: Arduino прерывает таймер

...