SOLVED:How to get int z to add 1 only when int y == 6

I posted a different question on this same program. My problem this time is how do I add to z only when y =6. It counts 1 full pallet but stops after that, which is what the code says. How do I get it to count every after that?

int x;
int y;
int z;
int t = 200;


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
x = 0;
while (x < 5) {
  x = x + 1;
  Serial.println(x);
  delay(t);
}
Serial.println("Box is full");


if(x == 5 ) {
  y = y + 1;
Serial.print(y);
Serial.println(" Boxes");
Serial.println();
delay(t);
}
if (y == 6) {
  z = z + 1;
  Serial.print(z);
  Serial.println(" Full pallets");
  Serial.println();
  delay(t);
}
}

That is the correct way. Is something not working the way you want?

After you added one to z, did you want to set y back to 0 so it would count up to 6 again? If you don't set y back to 0 it will count 7, 8, 9.... and never get back to 6.

1 Like

I didn't quite understand your difficulty, but see if this modification I made to your code meets your need:

int x;
int y;
int z;
int t = 200;


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
x = 0;
while (x < 5) {
  x = x + 1;
  Serial.println(x);
  delay(t);
}
Serial.println("Box is full");


if(x == 5 ) {
  y = y + 1;
Serial.print(y);
Serial.println(" Boxes");
Serial.println();
delay(t);
}
if (y%6 == 0) {                   // < < < < ---------------------------------------------
  z = z + 1;
  Serial.print(z);
  Serial.println(" Full pallets");
  Serial.println();
  delay(t);
}
}
1 Like

where do you reset y ?

I need int z to continue counting. What do I need to change to accomplish that? I suppose int y would have to reset after it reaches 6, however I would like to keep the overall count of int y.

how do I reset it? I tried using break but I must of been using it wrong.

maybe you want to increment z every 6th value of y

if ( ! ( y % 6))

Thank you! Does using % say anything after 6 is a remainder and to return it to 0?

Hi,
using %¨6, it means that each time the remainder of dividing Y by 6 is equal to 0, it increments z.
6, 12, 18, 24 .........

That's exactly what I wanted to do but didn't know how. I appreciate you.

Hi,
if your problem was solved, do a kindness to everyone on the forum, especially to those who helped you. Write [Solved] before your topic title, so if someone searches and finds your topic, they'll know what the solution looks like.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.