Why does my Sketch act different, when a variable is changed?

Hi,

I hope my question isn t too newbie…
I am learning Arduino IDE Language, now i made a little Setch myself. First it seemd to work properly, but when the Loop
comes to digotal read of the button (Taster) it runs just thru. I tried to take the container-Variable down to 254 and everything works just fine. I wonder what the problem is to avoid this problem in the future…

bitwise_counting.ino (1.22 KB)

Your code is small enough to post:

// bitwise counting
byte container = 1;      //byte-Zähler
byte box;                 //byte-Aufnahme
byte mask = 1;     //Vergleichsmaske
int i;                   //Durchlaufzähler
int z;                   //Durchlaufzähler
const int led[] = { 
  3, 4, 5, 6, 8, 9, 10, 11 };  // Zuweisen der Led-Pin-Nummer
const int tast = 2;                            // Zuweisen Taster-Pin
int Taster = 0;                                   // Taster indikator setzen


void setup() {

  for (i = 0; i < 8; i++ )                // output Pins zuordnen
  {
    pinMode(led[i], OUTPUT);
  }
  pinMode (tast, INPUT);                    // taster Pin zuordnen

}

void loop() {

  Taster = digitalRead(tast);               //start?
  if (Taster != LOW) {
    int Taster = 0;
    for (container = 1; container <= 255; container ++) { //Zähler bis 255 starten
      mask = 00000001;
      box = container;
      for (i = 0; i < 8; i++ ){                           //bit Seqenz Zähler
        if (box & mask){
          digitalWrite(led[i], HIGH);                     // LED an
        }
        box >>= 1;

      }
      delay(500);
      for (i = 0; i < 8; i++ ){                           //Alle LEDs aus
        digitalWrite(led[i], LOW);
      }
    }

  }  
}
  Taster = digitalRead(tast);               //start?
  if (Taster != LOW) {
    int Taster = 0;

Having a global variable, Taster, and a local variable, Taster, really isn’t a good idea. There are plenty of names available. Use a different name for the local variable.

    for (container = 1; container <= 255; container ++) { //Zähler bis 255 starten

When container is 255, and is incremented, what will the new value be? Is that new value going to be less than or equal 255?

How will this loop ever end?

Thanks PaulS,
for your quick response.
Sorry itś my first post so i didnt think of copy and paste for the Sketch :-))

The var. container in my code is good,when it is set to 254 why is that so?

greets Zerstep

The var. container in my code is good,when it is set to 254 why is that so?

Do you mean that:

    for (container = 1; container <= 254; container ++) {

does what you want, but that

    for (container = 1; container <= 255; container ++) {

doesn’t?

What does 255 + 1 equal, when the data type is byte?

Hey PaulS,

I think I got your point, type byte is in range of 0 to 255 so it gets back to zero right? How comes that the for Loop goes over
255 when the limit is set to 255?
Is there another way to code this loop to get 255?

greets Zerstep

int container = 1;

Job done.

But you really should learn why this works because it will be a constant problem for you. See byte - Arduino Reference

Use:
int container;

    for (container = 1; container <= 255; container ++) {

You have less than OR equals 255. So when container is 255 is still satisfies that and runs the for loop again. Then it increments and container is 0 so it will still satisfy and runs again and starts counting up again.

You can either use <=254 or <255. But <=255 isn’t going to work out if container is a byte.

Thanks Folks,

It seems that newbs are always understanding slowly

greets Zerstep

You are an expert now. :wink: