Code not running as expected

I made this small test code that take two counters that counts from 0 until they reach two different numbers.
each count should run independently and print is count.
My code is not acting as expected.
Anyone can spot my issue?

int num1= 500;
int num2 = 100;

long pMillis = 0;
unsigned int interval = 20;
int count = 0;
int count2 = 0;
void setup() {
  
Serial.begin(9600);

}

void loop()
{
  if(millis() - pMillis >= interval)
  {
    pMillis = millis();
    if (count <num1)
    {
      Serial.print("Count1: ");
      Serial.print(count);
      Serial.print(" ");
      count+=1;
    }
    else count = 0;
  }
  if(count2 < num2)
  {
    Serial.print("Count2: ");
    Serial.println(num2);
    count2+=1;
  }
  else count2 = 0;
}

I assume that the two counters should both increment when the interval has passed, but only one of them is updated when this occurs, the other is incremented on each pass through loop()

unsigned long pMillis;

that complies?

that complies. Humm. I did not know that.

counting only one number works just fine:

int num1= 200;
int num2 = 100;

unsigned long pMillis = 0;
unsigned int interval = 50;
int count = 0;
int count2 = 0;
void setup() {
  
Serial.begin(9600);

}

void loop()
{
  if(millis() - pMillis >= interval)
  {
    pMillis = millis();
    if (count <num1)
    {
      Serial.print("Count1: ");
      Serial.print(count);
      Serial.println(" ");
      count+=1;
    }
    else count = 0;
  }
}

Please explain exactly how the code is supposed to work and what the output should be

the code have 2 numbers and to counters.
each counter is starting from zero and should count until his correspnding number then reset to zero and start to count again.

count1 will count until arriving the value of num1 and count2 will count until arriving the value of num2

so if num1 = 200 and num2 = 100
count1 and count2 should be in syncc -> running from 0 to 100 together that count2 should be start from 0 while count1 is continue running until 200 and only then back to 0

this is what I've got so far:

int num1= 200;
int num2 = 100;

unsigned long pMillis = 0;
unsigned int interval = 50;
int count1 = 0;
int count2 = 0;
void setup() {
  
Serial.begin(9600);

}

void loop()
{
  if(millis() - pMillis >= interval)
  {
    pMillis = millis();
    if (count1 <num1)
    {
      Serial.print("Count1: ");
      Serial.print(count1);
      Serial.println(" ");
      count1+=1;
      if (count2 <num2)
    {
      Serial.print("Count2: ");
      Serial.print(count2);
      Serial.println(" ");
      count2+=1;
    }
    }
    
  }
}
  

ok I think I've got it:

int num1= 200;
int num2 = 100;

unsigned long pMillis = 0;
unsigned int interval = 50;
int count1 = 0;
int count2 = 0;
void setup() {
  
Serial.begin(9600);

}

void loop()
{
  if(millis() - pMillis >= interval)
  {
    pMillis = millis();
    if (count1 <num1)
    {
      Serial.print("Count1: ");
      Serial.print(count1);
      Serial.println(" ");
      count1+=1;
      if (count2 <num2)
    {
      Serial.print("Count2: ");
      Serial.print(count2);
      Serial.println(" ");
      count2+=1;
    }
    else count2 = 0;
    }
    else count1 = 0;
  }
}
  

That sketch will not compile because you forgot to change the count variable to count1 in every place in the code

With that corrected the code compiles but count 2 only increments if count1 is less than num1. Is that what you want ?

no. I solve it! now both counter are running and counting independetly from each other :slight_smile:

That sounds. Please post your new code

Slightly less beginner-friendly though, but simple enough when you wrap your head around it.

uint16_t num1= 500;
uint16_t num2 = 100;

uint32_t pMillis = 0;
uint8_t interval = 20;
uint16_t count = 0;
uint16_t count2 = 0;

void setup() { 
  Serial.begin(9600); 
}

void loop() {
  if((millis() - pMillis) >= interval) {
    pMillis = millis();
    Serial.print("Count1: ");
    Serial.print((count += 1) %= num1);
    Serial.print(" Count2: ");
    Serial.println((count2 += 1) %= num2);
  }
}

That is by far more elegant then my code! Thanks

You'll probably like this too

  unsigned int xx;
  
  if (xx == 777)
      ;
  else
      ;

Complies, compiles and does nothing.

I had to see if.

a7

Surely no surprise considering that

  byte x = 123;
  if (x > 10)
    Serial.println("higher");
  else
    Serial.println("lower");

compiles and works as expected, although I would always put the dependant code in { } anyway

I almost never do, and yes, occasionally I am shown that it might not be a bad idea. In the future. :expressionless:

a7