-2
int wallposy11[] = {6,5,5,4,4,3,3,2,2,1,1};
float wallposx11[] = {3.5,1.5,3.5,1.5,3.5,1.5,4.5,3.5,1.5,4.5};
float wallposy12[] = {5.5,5.5,5.5,4.5,4.5,4.5,3.5,3.5,2.5,2.5,2.5,2.5,1.5,1.5};
int wallposx12[] = {2,5,6,3,4,5,2,5,2,3,4,5,2,5};

void moveU(){
  for (x=0; x<19; x=x+1){
    if (float (sy + 0.5)== float (wallposy12[x]) && sx == wallposx12[x]){
      primeU = true;
      Serial.println("prime up");
    }
    else {
      primeU = false;
      Serial.println("not primed");
    }
    if ((float (sy - 0.5) == float (wallposy12[x]) && primeU == true) == 1);{
      //strike();
      Serial.println("------------");
      Serial.println(float (sy - 0.5) == float (wallposy12[x]) && primeU == true);
    }
  }
  sy = (sy + 1);
}

Im having trouble with the 3rd for() loop running even though the condition is false.

Context: im trying to make a version of the maze module from the game "Keep talking and nobody explodes", using an 8x8 dot matrix for the maze, and a joystick module for the movement with an arduino. The snippet of code im having trouble with is supposed to check if the variable sx is equal to any number within the wallposy12[] array.

This is only my second post so please let me know if something is unclear or poorly explained.

My whole code:

#include "LedControl.h"
#include "binary.h"

/*
 DIN connects to pin 12
 CLK connects to pin 11
 CS connects to pin 10 
 setLef(addr, row, col, state)

     _ _ _
    |     |
col |     |
    |_ _ _|
      row
     (wires)
*/
LedControl lc=LedControl(12,11,10,1);

int sx = 1; //x pos of the dot you control, aka start,
int sy = 1; //y pos of start dot
int mazet = 0; //maze type, 1-9, check bombmanual.com page 15.
int wallnum = 25; //unused, how many walls are in each maze
int x = 0; //used in for loop to check every number in an array, (the wallpos one) and see if var x matches.
int d1x = 0; //identifying dot 1 pos x
int d1y = 0; //identifying dot 1 posy
int d2x = 0; //dot 2 pos x
int d2y = 0; //dot 2 pos y
bool primeR = false;
bool primeL = false;
bool primeU = false;
bool primeD = false; 

int wallposy11[] = {0};
float wallposx11[] = {0};
float wallposy12[] = {0};
int wallposx12[] = {0};

int wallposy21[] = {0};
float wallposx21[] = {0};
float wallposy22[] = {0};
int wallposx22[] = {0};

int wallposy31[] = {0};
float wallposx31[] = {0};
float wallposy32[] = {0};
int wallposx32[] = {0};

int wallposy41[] = {0};
float wallposx41[] = {0};
float wallposy42[] = {0};
int wallposx42[] = {0};

int wallposy51[] = {0};
float wallposx51[] = {0};
float wallposy52[] = {0};
int wallposx52[] = {0};

int wallposy61[] = {0};
float wallposx61[] = {0};
float wallposy62[] = {0};
int wallposx62[] = {0};

int wallposy71[] = {0};
float wallposx71[] = {0};
float wallposy72[] = {0};
int wallposx72[] = {0};

int wallposy81[] = {0};
float wallposx81[] = {0};
float wallposy82[] = {0};
int wallposx82[] = {0};

int wallposy91[] = {0};
float wallposx91[] = {0};
float wallposy92[] = {0};
int wallposx92[] = {0};

/* wallpos(classifier for x coord or y coord)(which maze, 1 - 9)(1 for vertical walls, 2 for horizontal lines)[] = {}
for example: 
            wallposx11 is for the x value of the horizontal lines in the 1st maze.
            wallposy72 is for the y value of the vertical lines in the 7th maze.
*/

void preset1(){ //setting up wall values and id dot values for the first maze preset
  int wallposy11[] = {6,5,5,4,4,3,3,2,2,1,1};
  float wallposx11[] = {3.5,1.5,3.5,1.5,3.5,1.5,4.5,3.5,1.5,4.5};
  float wallposy12[] = {5.5,5.5,5.5,4.5,4.5,4.5,3.5,3.5,2.5,2.5,2.5,2.5,1.5,1.5};
  int wallposx12[] = {2,5,6,3,4,5,2,5,2,3,4,5,2,5};
  d1x = 1;
  d1y = 5;
  d2x = 5;
  d2y = 4;
}

void preset2(){ //configuring wall and id dot values for the second maze
  int wallposy21[] = {6,5,5,4,4,3,3,3,2,2,2,2,1,1};
  float wallposx21[] = {3.5,2.5,4.5,1.5,3.5,2.5,4.5,5.5,1.5,2.5,3.5,5.5,1.5,3.5};
  float wallposy22[] = {5.5,5.5,5.5,4.5,4.5,4.5,3.5,3.5,2.5,2.5,1.5};
  int wallposx22[] = {1,3,6,2,4,5,3,5,2,4,5};
  d1x = 2;
  d1y = 3;
  d2x = 5;
  d2y = 5;
}

void preset3(){ //configuring wall and id dot values for the third maze
  int wallposy31[] = {6,6,5,5,5,5,4,4,4,3,3,3,3,3,2,2,2,2,1};
  float wallposx31[] = {3.5,4.5,1.5,2.5,3.5,5.5,2.5,3.5,5.5,1.5,2.5,3.5,4.5,5.5,1.5,3.5,4.5,5.5,4.5};
  float wallposy32[] = {5.5,4.5,4.5,4.5,2.5,2.5};
  int wallposx32[] = {2,1,4,5,2,3};
  d1x = 4;
  d1y = 3;
  d2x = 6;
  d2y = 3;
}

void preset4(){ //configuring wall and id dot values for the fourth maze
  int wallposy41[] = {6,5,5,4,4,4,3,2,1,1};
  float wallposx41[] = {2.5,1.5,2.5,1.5,3.5,5.5,1.5,5.5,3.5,5.5};
  float wallposy42[] = {5.5,5.5,5.5,4.5,4.5,3.5,3.5,3.5,2.5,2.5,2.5,2.5,1.5,1.5,1.5};
  int wallposx42[] = {3,4,5,4,5,2,3,5,2,3,4,5,2,3,4};
  d1x = 1;
  d1y = 3;
  d2x = 1;
  d2y = 6;
}

void preset5(){ //configuring wall and id dot values for the fifth maze
  int wallposy51[] = {5,4,4,3,3,3,2,2,1};
  float wallposx51[] = {5.5,2.5,4.5,1.5,4.5,5.5,1.5,5.5,1.5};
  float wallposy52[] = {5.5,5.5,5.5,5.5,4.5,4.5,4.5,4.5,3.5,3.5,2.5,2.5,2.5,1.5,1.5,1.5};
  int wallposx52[] = {1,2,3,4,2,3,5,6,3,4,2,3,5,3,4,5};
  d1x = 4;
  d1y = 1;
  d2x = 5;
  d2y = 4;
}

void preset6(){ //configuring wall and id dot values for the sixth maze
  int wallposy61[] = {6,6,5,5,5,5,4,4,4,3,3,3,2,2,2,1,};
  float wallposx61[] = {1.5,3.5,1.5,2.5,3.5,5.5,2.5,3.5,4.5,2.5,4.5,5.5,2.5,3.5,4.5,4.5};
  float wallposy62[] = {5.5,4.5,3.5,3.5,3.5,2.5,1.5,1.5,1.5};
  int wallposx62[] = {4,5,2,3,6,1,2,3,5};
  d1x = 3;
  d1y = 1;
  d2x = 5;
  d2y = 6;
}

void preset7(){ //configuring wall and id dot values for the seventh maze
  int wallposy71[] = {};
  float wallposx71[] = {4.5,1.5,3.5,5.5,2.5,4.5,2.5,5.5,1.5,2.5,5.5};
  float wallposy72[] = {5.5,5.5,4.5,4.5,4.5,3.5,3.5,3.5,2.5,2.5,1.5,1.5,1.5};
  int wallposx72[] = {2,3,3,4,5,1,2,4,4,5,2,3,4};  
  d1x = 2;
  d1y = 1;
  d2x = 2;
  d2y = 6;
}

void preset8(){ //configuring wall and id dot values for the eighth maze
  int wallposy81[] = {6,6,5,5,4,4,3,3,};
  float wallposx81[] = {1.5,4.5,3.5,5.5,1.5,5.5,1.5,3.5};
  float wallposy82[] = {5.5,4.5,4.5,4.5,4.5,3.5,3.5,2.5,2.5,2.5,2.5,1.5,1.5,1.5,1.5};
  int wallposx82[] = {3,2,3,4,5,3,4,2,4,5,6,3,4,5,6};
  d1x = 3;
  d1y = 3;
  d2x = 4;
  d2y = 6;
}

void preset9(){ //configuring wall and id dot values for the ninth maze
  int wallposy91[] = {6,5,5,5,5,4,4,3,3,3,2,2,2,2,1,1};
  float wallposx91[] = {1.5,1.5,2.5,4.5,5.5,3.5,5.5,1.5,2.5,4.5,1.5,2.5,3.5,5.5,2.5,4.5};
  float wallposy92[] = {5.5,5.5,4.5,3.5,3.5,3.5,2.5,2.5};
  int wallposx92[] = {3,4,4,2,3,5,4,5};
  d1x = 1;
  d1y = 2;
  d2x = 3;
  d2y = 5;
}
 
void draw(){ //sets up boundaries, start pos and end pos on the dot matrix
  lc.setRow(0,0,B11111111); //sets row 0 to on. (the B is binary value for each light, 1 being on and 0 is off. B11111111 is all on, B01111111 is all but first on, etc)
  lc.setRow(0,7,B11111111);
  lc.setColumn(0,7,B11111111);
  lc.setColumn(0,0,B11111111);
  lc.setLed(0,sx,sy,1); //sets the start dot to on
  lc.setLed(0,6,6,1);
  delay(1000);
  
  lc.setLed(0,6,6,0); //flashing endpoint animation
  delay(1000);

  lc.setLed(0,6,6,1);
  delay(1000);
}

void strike(){ //sets the whole screen to red when player hits wall.
  lc.setRow(0,0,B11111111);
  lc.setRow(0,1,B11111111);
  lc.setRow(0,2,B11111111);
  lc.setRow(0,3,B11111111);    
  lc.setRow(0,4,B11111111);
  lc.setRow(0,5,B11111111);
  lc.setRow(0,6,B11111111);
  lc.setRow(0,7,B11111111);
}

void moveU(){
  for (x=0; x<19; x=x+1){
    if (float (sy + 0.5)== float (wallposy12[x]) && sx == wallposx12[x]){
      primeU = true;
      Serial.println("prime up");
    }
    else {
      primeU = false;
      Serial.println("not primed");
    }
    if ((float (sy - 0.5) == float (wallposy12[x]) && primeU == true) == 1);{
      //strike();
      Serial.println("------------");
      Serial.println(float (sy - 0.5) == float (wallposy12[x]) && primeU == true);
    }
  }
  sy = (sy + 1);
}

void moveDown(){ //moves dot down
  for (x=0; x<19; x=x+1){ 

    if (float (sy - 0.5)==float (wallposx12[x])){
      if (wallposy12[x] == sy){
          strike(); //expand this line for explanation on this whole for() loop.
            /* whats happening here is the for loop will set x to 0, check if its lower than 19, and if it is, it will run the code under it. I set it to 19 because thats the amount of numbers in the wallposy12[]
            array. When the x value is plugged into both array vars, lets say that:
            wallposx12[] = {1,2,3,4,5,6,7,8}
            and
            wallposy12[] = {8,7,6,5,4,3,2,1}
              (notice that one is wallpos Y and the other is wallpos X)
            When the same number is plugged into both, you get the number in the same position, so:
            wallposx12[3] = 3 (because 4 is the 4th listed value in the array.)
            wallposy12[3] = 6 
            if we instead put x in the bracket instead of 4:
            x = 3
            wallposx12[x] = 3 (since x = 4, then putting that into the [] would return the 4th value)
            wallposy12[x] = 6
            so for (x=0; x<19; x=x+1) means when x is equal to zero, run this loop as long as x stays under 19 and when its done, add 1 to x.
            so when the code runs through a second time, x would be equal to 1. if it was already at 3 and added to 4 we would get:
            wallposx12[x] = 4
            wallposy12[x] = 5*/
      }
    }
  }
  sy = (sy - 1);
}

void moveR(){ //moves dot right
  switch(mazet){
    case 1: {
      for (x=0; x<19; x=x+1){
        if (float (sx + 0.5)==float (wallposx11[x])){
          if (wallposy11[x] == sy){
              strike();
          }
        }
      }
    }
      case 2: {
      for (x=0; x<19; x=x+1){
        if (float (sx + 0.5)==float (wallposx21[x])){
          if (wallposy21[x] == sy){
              strike();
          }
        }

      }
    }

  }

  sx = (sx + 1);
}

void moveL(){ //moves dot left
  for (x=0; x<19; x=x+1){
    if (float (sx - 0.5)==float (wallposx11[x])){
      if (wallposy11[x] == sy){
          strike();
      }
    }
  }
  sx = (sx - 1);
}


void scrn(){ //pressing joystick shows dots
  lc.clearDisplay(0);
  while (analogRead(A2) < 20) {
    lc.setLed(0,6,6,0);
    lc.setLed(0,sx,sy,0);
    lc.setLed(0,d1x,d1y,1);
    lc.setLed(0,d2x,d2y,1);
  }
}

void setup() {
 // preset3();
  Serial.begin(9600);
  mazet = random(1,9);
  Serial.print(mazet);
  if (lock == true){
    switch(mazet){
      case 1: { //if mazet = 3, then case = 3
        preset1();
        Serial.println("preset 1");
      }
      case 2:{
        preset2();
        Serial.println("preset 2");
      }
      case 3:{
        preset3();
        Serial.println("preset 3");
      }
      case 4:{
        preset4();
        Serial.println("preset 4");
      }
      case 5:{
        preset5();
        Serial.println("preset 5");
      }
      case 6:{
        preset6();
        Serial.println("preset 6");
      }
      case 7:{
        preset7();
        Serial.println("preset 7");
      }
      case 8:{
        preset8();
        Serial.println("preset 8");
      }
      case 9:{
        preset9();
        Serial.println("preset 9");
      }
    }
    lock = false;
  }
  Serial.println(mazet);
  lc.shutdown(0,false);
  // Set brightness to a medium value
  lc.setIntensity(0,1);
  // Clear the display
  lc.clearDisplay(0);  
  pinMode(7,INPUT);
  draw();
}



void loop(){
/*
  Serial.println(sx);
  Serial.println(sy);
  Serial.println(analogRead(A2)); */

  mazet = random(1,9);
  if (analogRead(A0)>1020) { //Read analog input for y and move up
    lc.setLed(0,sx,sy,0);
    moveU();
    lc.setLed(0,sx,sy,1);
    Serial.println(sx);
    Serial.println(sy);
    delay(200);
    }
  if (analogRead(A0)< 10) {//Read analog input for y and move down
    lc.setLed(0,sx,sy,0);
    moveDown();
    lc.setLed(0,sx,sy,1);
    Serial.println(sx);
    Serial.println(sy);
    delay(200);
    }
  

  if (analogRead(A1)>1020) {//Read analog input for x and move left
    lc.setLed(0,sx,sy,0);
    moveL();
    lc.setLed(0,sx,sy,1);
    Serial.println(sx);
    Serial.println(sy);
    delay(200);
    
  }
  
  if (analogRead(A1)< 10) {//Read analog input for x and move right
    lc.setLed(0,sx,sy,0);
    moveR();
    lc.setLed(0,sx,sy,1);
    Serial.println(sx);
    Serial.println(sy);
    delay(200);
    
  }
    if (analogRead(A2) < 20){
      scrn();
      lc.setLed(0,d1x,d1y,0);
      lc.setLed(0,d1x,d2y,0);
      lc.setLed(0,6,6,1);
      lc.setLed(0,sx,sy,1);
    }
  lc.setRow(0,0,B11111111); //draws borders.
  lc.setRow(0,7,B11111111);
  lc.setColumn(0,7,B11111111);
  lc.setColumn(0,0,B11111111);
  
}

I tried to have it print (float (sy - 0.5) == float (wallposy12[x]) && primeU == true)(the condition in the if statement) to see if it was returning 0 or 1, but it was returning 0 and still running the code under the if statement.

  • Never compare floats for equality, use static_cast, don't mix integer and float calculations. (Also do not use globals, investigate std::vector). And what value does `sy` have? Is it even initalized. But most importantly you should attach a debugger and go over your code line by line and check if all your assumptions are correct. – Pepijn Kramer Aug 29 '23 at 19:03
  • 1
    One surprising thing with floating point is that [0.1 + 0.2 != 0.3](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). It is *almost* equal, but not *exactly* equal. – BoP Aug 29 '23 at 19:06
  • 1
    @Grayniak The presented code does not make sense. For example what is the magic number 19 used in a for loop? – Vlad from Moscow Aug 29 '23 at 19:06
  • More on floating point math and inaccuracy in this episode of computerphile [floating point numbers](https://www.youtube.com/watch?v=PZRI1IfStY0). @BoP, video almost exactly starts with that statement ;) – Pepijn Kramer Aug 29 '23 at 19:20
  • 4
    `if ((float (sy - 0.5) == float (wallposy12[x]) && primeU == true) == 1);` -- You have an extraneous `;` after the `if` statement. Thus this: `{Serial.println("------------"); Serial.println(float (sy - 0.5) == float (wallposy12[x]) && primeU == true); }` will always execute. – PaulMcKenzie Aug 29 '23 at 19:34
  • 2
    `if (x);` the `;` is the end of the if-statement, and the true-body is an empty statement. If you had compiler warnings enabled, your compiler probably could have warned you about the bug. – Eljay Aug 29 '23 at 19:37

0 Answers0