Counter (count down) in for loop and conditional (trigger) issue

Hi all,

I have a small issue I hope.

I have a for loop which in it there's also a conditional which happens only once (kind of trigger) and I have a counter which when it reaches 0 I need it to trigger another conditional. I need the counter to start going down when there's a trigger, the problem is that the counter only goes down by 1 every time it's triggered. I tried moving the counter-- around in the code but with no luck, can some one help me out please, thanks!

for (int i = 0; i < cols; i++) {     
      for (int j = 0; j < rows; j++) {

        int index = i + j * cols;
        //println(j);
        grid[index].display();

        //CODE FOR SENSORS (hopefully)
        println(counter);


        if (grid[index].on == false && sensor && grid[index].on == false && sensorIndex == grid[index].cellIndex) {

          grid[index].on = true;
          counter--;

          if (counter <=0) {
            if (index > cols) grid[index-cols].on = true;
            if (index < grid.length - cols) grid[index+cols].on = true;
            if (index < grid.length-1 && i != 5) grid[index+1].on = true;
            if (index > 0 && i != 0 ) grid[index-1].on = true;
          }

          //println(index);

          sensor = false;
        } else if (grid[index].on == true && sensor && grid[index].on == true && sensorIndex == grid[index].cellIndex) {

          grid[index].on = false;
          if (index > cols) grid[index-cols].on = false;
          if (index < grid.length - cols) grid[index+cols].on = false;
          if (index < grid.length-1) grid[index+1].on = false;
          if (index > 0) grid[index-1].on = false;

          sensor = false;
        }
      }
    }
Tagged:

Answers

Sign In or Register to comment.