Doubt in working with multiple

Asked

Viewed 124 times

2

I’m developing a Sketch and I need to use multiples of one, but I guess I’m not knowing how to work right with them. I have a loop FOR that goes from 1 to 72 and I need to count and go showing the messages on the screen. The problem is that there is message that is repeating itself due to more than 1 number being multiple of 3, how can I fix it ?

OBS: By the loop go up to 72 is normal after displaying the last message, start all over again, then I will have 2 separate message blocks.

I need to do this:

1 ) A cada 1  byte  exibe no terminal ( BLOCO ! )
2 ) A cada 21 bytes exibe no terminal ( BLOCO 2 )
3 ) A cada 29 bytes exibe no terminal ( BLOCO 3 )
4 ) A cada 35 bytes exibe no terminal ( BLOCO 4 )
5 ) A cada 36 bytes exibe no terminal ( BLOCO 5 )

CODE:

  for(i = 1; i < 72; i ++)
  {

    if ((i % 1 == 0) && ((i) % 1 == 0))
    {
      Serial.println("BLOCO 1");
    }
    else if ((i % 21 == 0) && ((i) % 3 == 0))
    {
      Serial.println("BLOCO 2");
    }
    else if ((i % 29 == 0) && ((i) % 3 == 0))
    {
      Serial.println("BLOCO 3");
    }
    else if ((i % 35 == 0) && ((i) % 5 == 0))
    {
      Serial.println("BLOCO 4");
    }
    else if ((i % 36 == 0) && ((i) % 3 == 0))
    {
      Serial.println("BLOCO 5");
    }
}
  • Welcome to Stackoverflowpt! Could you explain your question better? I can try to help you, but I still don’t understand what you want.

  • Hello Avelino. Thank you for the Welcome. Nos if’s were the multiples and when TRUE displays the message.

1 answer

1


If I understand your question correctly, you need to show messages in a set of blocks of bytes. Therefore, as follows, 71 exact messages will be printed:

    for (int i = 1; i < 72; i++) {

        if (i % 21 == 0) {
            Serial.println("BLOCO 2");
            continue;
        }
        if (i % 29 == 0) {
            Serial.println("BLOCO 3");
            continue;
        }
        if (i % 35 == 0) {
            Serial.println("BLOCO 4");
            continue;
        }
        if (i % 36 == 0) {
            Serial.println("BLOCO 5");
            continue;
        }

        Serial.println("BLOCO 1");
    }
  • Thank you Avelino.

  • @abduzeedo worked?

  • I’m testing... already put the result.

  • 1

    Worked..........

Browser other questions tagged

You are not signed in. Login or sign up in order to post.