Problem Mit Millis

danke erstmal für eure Hilfe. Leider funktioniert das Beispiel auch nicht wie gewollt.

Ich hab Dich gefragt. Mir was zusammengereimt und jetzt kannst Du nicht mal sagen was falsch ist. :man_shrugging:

sorry, das problem ist nachwievor das gleiche, also das der zähler nicht immer bei 0 anfängt zu zählen

Dann zeige doch mal den debug

programmAuswahl: 1
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
programmAuswahl: 2
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
programmAuswahl: 3
programmAuswahl: 0
programmAuswahl: 1
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
programmAuswahl: 2
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
programmAuswahl: 3
programmAuswahl: 0
programmAuswahl: 1
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
programmAuswahl: 2
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
programmAuswahl: 3
programmAuswahl: 0

Und wo ist jetzt ein Fehler?

Ich ahne, dass er nach dem umschalten die 0 nicht im SerMon sieht, weil der Counter erst dann ausgegeben wird, wenn intervall abgelaufen ist....

Doch fängt er.
Nur die Ausgabe nicht. Hatte ich aber dazu geschrieben.
Und wenn das jetzt auf dem LED auch läuft, dann bau ich DIr die 0 noch ein

Vielen Dank nochmal für eure Hilfe , aber ich bleibe doch lieber bei der ursprünglichen Methode. Will euch nicht weiter wegen stressen. So schlimm ist das Problem für mich jetzt nicht .

Nochmal: Wo ist dein Problem? Und warum willst du den Code von @my_xy_projekt nicht verwenden?

Wenn ich mich gestresst fühle, dann mach ich den Netbook zu.

Und hier jetzt mit 0er Counterausgabe

#include "LedControl.h"
constexpr uint8_t BUTTON_PIN {2};

LedControl lc = LedControl(12, 11, 10, 1);

constexpr bool isPressed {LOW};
uint8_t lastProgramm;
uint8_t lastCounter = 255;

bool buttonState;
bool lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
unsigned long previous_millis = 0;
byte counter = 0;
byte program = 0;

byte squares[5][8] =
{
  {0b11111111, 0b10000001, 0b10000001, 0b10000001, 0b10000001, 0b10000001, 0b10000001, 0b11111111},
  {0000000000, 0b01111110, 0b01000010, 0b01000010, 0b01000010, 0b01000010, 0b01111110, 0b00000000},
  {0000000000, 0b00000000, 0b00111100, 0b00100100, 0b00100100, 0b00111100, 0b00000000, 0b00000000},
  {0000000000, 0b00000000, 0b00000000, 0b00011000, 0b00011000, 0b00000000, 0b00000000, 0b00000000},
  {0000000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000},
};

void testing(uint16_t interval, byte arr[0][8])
{
  if (millis() - previous_millis >= interval)
  {
    previous_millis += interval;
    counter++;

    if (counter >= 5)
    { counter = 0; }
  }
}

void getButton()
{
  bool buttonState = digitalRead(BUTTON_PIN);

  if (buttonState == isPressed)
  {
    if (lastButtonState != isPressed)
    {
      lastButtonState = isPressed;
      program++;
    }
  }
  else if (millis() - lastDebounceTime > debounceDelay)
  {
    lastButtonState = !isPressed;
  }
}

void setup()
{
  lc.shutdown(0, false);
  lc.setIntensity(0, 1);
  lc.clearDisplay(0);
  pinMode(BUTTON_PIN, INPUT);
  Serial.begin(9600);
}
void loop()
{
  getButton();
  setProgramm();
  displayCount();
}

void setProgramm()
{
  if (lastProgramm != program)
  {
    counter = 0;
    lc.clearDisplay(0);
    lastProgramm = program;
    Serial.print(F("programmAuswahl: "));
    Serial.println(program);
    previous_millis = 0;
  }

  switch (program)
  {
    case 0:
      break;

    case 1:
      testing(1000, squares);
      break;

    case 2:
      testing(500, squares);
      break;

    default:
      program = 0;
      break;
  }
}

void displayCount()
{
  if (lastCounter != counter)
  {
    Serial.print(F("Counter: "));
    Serial.println(counter);

    for (byte row = 1; row < 9; row++)
    {
      lc.setRow(0, row - 1, arr[counter][row - 1]);
    }

    lastCounter = counter;
  }
programmAuswahl: 1
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
programmAuswahl: 2
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
programmAuswahl: 3
programmAuswahl: 0
programmAuswahl: 1
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
programmAuswahl: 2
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
programmAuswahl: 3
Counter: 0
programmAuswahl: 0
programmAuswahl: 1
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
programmAuswahl: 2
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
programmAuswahl: 3
programmAuswahl: 0
programmAuswahl: 1
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
programmAuswahl: 2
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
programmAuswahl: 3
Counter: 0
programmAuswahl: 0
programmAuswahl: 1
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
programmAuswahl: 2
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
programmAuswahl: 3
Counter: 0
programmAuswahl: 0
programmAuswahl: 1
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0

Ändere mal bitte Zeile 81.
NEU:

previous_millis = millis();
(Bin mal kurz weg)

Counter: 4
Counter: 0
Counter: 1
Counter: 2
□□□□□□□□□□□□□□□□□□□□□□□□□□□□Counter: 0
programmAuswahl: 1
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
programmAuswahl: 2
Counter: 1
Counter: 2
Counter: 3
programmAuswahl: 3
Counter: 0
programmAuswahl: 0
programmAuswahl: 1
Counter: 1
Counter: 2
programmAuswahl: 2
Counter: 0
Counter: 1
Counter: 2
programmAuswahl: 3
Counter: 0
programmAuswahl: 0
programmAuswahl: 1
Counter: 1
Counter: 2
programmAuswahl: 2
Counter: 0
Counter: 1
Counter: 2
Counter: 3
programmAuswahl: 3
Counter: 0
programmAuswahl: 0
programmAuswahl: 1
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 0
Counter: 1
Counter: 2

leider immer noch das selbe Problem. Danke nochmal für eure Hilfe . Ich bleibe dann doch bei alten Methode

Warum gibst du so schnell auf:

neuer Versuch:

#include "LedControl.h"
constexpr uint8_t BUTTON_PIN {2};
constexpr bool isPressed {LOW};
constexpr uint32_t debounceDelay {50};

LedControl lc = LedControl(12, 11, 10, 1);

uint8_t program = 0;
uint8_t lastProgramm;

uint8_t counter = 0;
uint8_t lastCounter = 255;

bool buttonState;
bool lastButtonState = LOW;

unsigned long lastDebounceTime = 0;
unsigned long previous_millis = 0;

uint8_t squares[5][8] =
{
  {0b11111111, 0b10000001, 0b10000001, 0b10000001, 0b10000001, 0b10000001, 0b10000001, 0b11111111},
  {0000000000, 0b01111110, 0b01000010, 0b01000010, 0b01000010, 0b01000010, 0b01111110, 0b00000000},
  {0000000000, 0b00000000, 0b00111100, 0b00100100, 0b00100100, 0b00111100, 0b00000000, 0b00000000},
  {0000000000, 0b00000000, 0b00000000, 0b00011000, 0b00011000, 0b00000000, 0b00000000, 0b00000000},
  {0000000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000},
};

void testing(uint32_t interval, uint8_t arr[0][8]) {
  if (millis() - previous_millis >= interval) {
    previous_millis += interval;
    counter++;
    if (counter >= 5) counter = 0;
  }
}

void getButton() {
  bool buttonState = digitalRead(BUTTON_PIN);

  if (buttonState == isPressed) {
    if (lastButtonState != isPressed) {
      lastButtonState = isPressed;
      program++;
    }
  } else if (millis() - lastDebounceTime > debounceDelay) {
    lastButtonState = !isPressed;
  }
}

void setup() {
  lc.shutdown(0, false);
  lc.setIntensity(0, 1);
  lc.clearDisplay(0);
  pinMode(BUTTON_PIN, INPUT);
  Serial.begin(115200);
}

void loop() {
  getButton();
  setProgramm();
  displayCount();
}

void setProgramm() {
  if (lastProgramm != program) {
    counter = 0;
    lastCounter = 255;
    lc.clearDisplay(0);
    lastProgramm = program;
    Serial.print(F("programmAuswahl: "));
    Serial.println(program);
    previous_millis = millis();
  }

  switch (program) {
    case 0:
      break;
    case 1:
      testing(1000, squares);
      break;
    case 2:
      testing(500, squares);
      break;
    default:
      program = 0;
      break;
  }
}

void displayCount() {
  if (lastCounter != counter) {
    Serial.print(F("Counter: "));
    Serial.println(counter);

    for (uint8_t row = 1; row < 9; row++) {
      lc.setRow(0, row - 1, arr[counter][row - 1]);
    }
    lastCounter = counter;
  }
}

Ich musste noch Zeile 67 einfügen

habe dein Beispiel ausprobiert und es scheint jetzt zu funktionieren, also counter startet bei jedem tastendruck bei 0. Vielen Dank!

Gemeinsam sind wir stark :grin: