Проблема с таймером на millis()

Написал код. В loop() можно увидеть таймер на millis() с переменной timing.
По какой-то неявной для меня причине, после 10-го срабатывания данного таймера, таймер перестаёт работать, что приводит к постоянному вызову функции bip().

void setup() {            
  Serial.begin(9600);
  pinMode(8, INPUT_PULLUP);  //На вход
  pinMode(10, INPUT_PULLUP);  //На вход
  pinMode(4, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(7, OUTPUT);  
}

void flick(int period){
  digitalWrite(7, 1);
  delayMicroseconds(period*200);
  delayMicroseconds(period*200);
  delayMicroseconds(period*200);
  delayMicroseconds(period*200);
  delayMicroseconds(period*200);
  digitalWrite(7, 0);
  delayMicroseconds(period*200);
  delayMicroseconds(period*200);
  delayMicroseconds(period*200);
  delayMicroseconds(period*200);
  delayMicroseconds(period*200);
}

void bip(int period){ 
  int Time = millis();
  digitalWrite(2, 1);
  while(millis() - Time < 100) flick(period);
  digitalWrite(2, 0);
}

void loop() {
  float test_t[56]={45.5, 41.7, 38.5, 35.7, 33.3, 31.2, 29.4, 27.8, 26.3, 25.0, 23.8, 22.7, 21.7,  //Создаём массив периодов для монохромных мельканий в режиме "тест" 
             20.8, 20.0, 19.2, 18.5, 17.9, 17.2, 16.7, 16.1, 15.6, 15.2, 14.7, 14.3, 13.9, 
             13.5, 13.2, 12.8, 12.5, 12.2, 11.9, 11.6, 11.4, 11.1, 10.9, 10.6, 10.4, 10.2, 
             10.0, 9.8, 9.6, 9.3, 9.2, 9.0, 8.8, 8.7, 8.5, 8.4, 8.2, 8.1, 8.0, 7.8, 7.7, 7.6, 7.5};

  int Button = 1, button_press = 0, lvl = 0, CFFF, CFDF, key = 1, timing = 0, freq;

  while(true){
    Button = digitalRead(8);

    if(!Button) button_press = 1;
    if(Button && button_press && !lvl){
      lvl = 2; freq = 65; 
      button_press = 0;
      digitalWrite(2, 1);
      delay(100);
      digitalWrite(2, 0); 
    }
    if(lvl == 2){
      if(!timing) timing = millis();
      if (millis() - timing < 3000) {
        flick(test_t[freq-10]);
        if(button_press && Button){
          CFDF = freq; lvl = 3; 
          int timeD = millis() - timing;
          digitalWrite(2, 1);
          delay(1000);
          digitalWrite(2, 0);
        }                   
      }else{
        --freq; timing = millis(); 
        bip(test_t[freq-10]);  
      }
    }
  }
}

int Time = millis();

Почему это является ошибкой?

:slight_smile: Откуда я знаю! Просто пишу всегда long.Хотя это тоже неправильно.

Тут то же спорный момент для int

Для данного примера достаточно:

unsigned int Time = millis();

Потому что при переполнении числа оно становится отрицательным. А минус на минус - даёт плюс ))

@Fedor_Yar07 можете объяснить (хоть самому себе) нахрена в функции loop используется цикл

Вы понимаете, как работает loop? Что она сама по себе вызывается из такого же цикла?

Так если не использовать цикл, локальные переменные будут постоянно обновляться и приходить в изначальное состояние

Кто или что мешает объявить их статическими?

Зачем они? Зачем этот ход конём?

Моя дырявая память

А для этого?

Все, что работает с millis() должно быть unsigned long. Нюансы возможны, но нужно хорошо понимать, что и зачем

2 лайка

Просто это конеходство натолкнуло на мысль - если unsigned int переменным присваивать millis(), а потом сравнивать их переполненные разности, то должно быть эквивалентно разностям в unsigned long?

Если переменной unsigned int присваивать значение millis(), как это делает ТС, то переполнение возможно уже при присвоении, а что там будет получаться в итоге - хз, абсолютная неопределенность

Тоже самое что и при unsigned long. Тут главное до нужного значения досчитать (то что правее знака сравнения).

Пока разность не превышает unsigned int - эквивалентно, потом нет

Взял реальную Ардуину и проверил по классике из иде:

/* Blink without Delay

 Turns on and off a light emitting diode (LED) connected to a digital
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.

 The circuit:
 * Use the onboard LED.
 * Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO 
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino model, check
  the Technical Specs of your board  at https://www.arduino.cc/en/Main/Products

 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 modified 11 Nov 2013
 by Scott Fitzgerald
 modified 9 Jan 2017
 by Arturo Guadalupi


 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to set a pin number :
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
 int previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const int interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  int currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Работает с int. Однако если в 55 строке написать long мигание дохнет после 17 вспышки. Так что ТС ошибся со своими странностями. Надо было пробовать (int) millis() в условии.

А теперь проверьте ту же классику при разных значениях interval.

Попробовал на 100 и 10000.