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?
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()
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;
}
}
}
}