While loop gets stuck and just shows void.setup

Hello I am an amateur to arduino programming and I would like to ask for help concerning why my code gets stuck at the void.setup phase and the while loop never progresses even if the variables change
any tips or advices would be greatly appreciated thank you!

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4

#include <IRremote.h>  
#define button_1 0xFF30CF
#define button_2 0xFF18E7
#define button_3 0xFF7A85
//Remote pin and fucntions
uint32_t Previous; 
int RECV_PIN = 6; 
IRrecv irrecv(RECV_PIN); 
decode_results results; 
/****** REMOTE *********/

Adafruit_SSD1306 display(OLED_RESET);

int redLed = 9;  

int bro = 1;

void setup() 
{
   irrecv.enableIRIn();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
   pinMode(redLed, OUTPUT); // Set redLED  Pin as ouput  
  display.clearDisplay();   
  display.display();
   display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0,0);
  display.println("Booting Up...");
  display.display();
  delay(1000);
   display.clearDisplay();
}
 
void loop() {
  int bro = 1;
  while(bro==1) {}
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,8);
  display.println("Waiting for answers...");
  delay(200);
  display.clearDisplay();
  
      
      
   if (irrecv.decode(&results)) {
    
     
      
 
      switch (results.value) {
        case button_1:         // key >>|
        bro = 2;
            digitalWrite(redLed, HIGH);
            delay(100);
            digitalWrite(redLed, LOW);
            display.setTextSize(1);
            display.setTextColor(WHITE);
            display.setCursor(0,8);
            display.println("Remote No.1 Picked 1");
            display.display();
            delay(5000);
            display.clearDisplay();
            break;
             
      
            
            case button_2:      // key <<
            bro = 3;
            digitalWrite(redLed, HIGH);
            delay(100);
            digitalWrite(redLed, LOW);
            display.setTextSize(1);
            display.setTextColor(WHITE);
            display.setCursor(0,8);
            display.println("Remote No.1 Picked 2");
            display.display();
            delay(5000);
            display.clearDisplay();
            break;
            
         
          
            case button_3:     //stop     
            bro = 4;
            digitalWrite(redLed, HIGH);
            delay(100);
            digitalWrite(redLed, LOW);
            display.setTextSize(1);
            display.setTextColor(WHITE);
            display.setCursor(0,8);
            display.println("Remote No.1 Picked 3");
            display.display();
            delay(5000);
            display.clearDisplay();
            break;


            default:
            bro = 5;
            display.setTextSize(1);
            display.setTextColor(WHITE);
            display.setCursor(0,8);
            Serial.print("Error 404");
            display.display();
            delay(1000);
            display.clearDisplay();
            break;
            
          
            }
display.display();
         }
 irrecv.resume();
   
  // }
   
}

Set bro to 1, and if it is 1 then sit and do nothing.

Sounds correct?

1 Like

Clue: why is that brace commented out? If you copied the code from somewhere else, take a look at the original, and examine its structure.

Note: This 'bro' is not the same variable as the global 'bro'. It is a local variable so each time through loop() it will be re-set to 1.

If you want to use the global variable, change the int bro = 1; inside loop() to bro = 1;. You will still be stuck in the while loop.

Try to not use while loops in your void loop. The void loop is like a while loop already. Allow you loop to run through and complete using if statements only

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