Search code examples
loopsarduinoarduino-unoarduino-idelcd

Arduino the void loop() function isn't looping


I'm new to Arduino and I wrote the beginning a code that is supposed to play games read stories and more on an LCD display.

Here's my code

#include <LiquidCrystal.h>

// Arduino pins number
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
const int LCD_RS = 7;
const int LCD_Enable = 8;
const int LCD_D4 = 9;
const int LCD_D5 = 10;
const int LCD_D6 = 11;
const int LCD_D7 = 12;
LiquidCrystal lcd(LCD_RS, LCD_Enable, LCD_D4, LCD_D5, LCD_D6, LCD_D7);

// Basic vars
int none = 0;
String Apps[2] = {"App selector","Credits"};
int CurrentApp = 0;
int Yaxis = 1;
int Xaxis = 1;
int HiCh = 0;
int button;
int JXaxis;
int JYaxis;


void Credits() {  //          CREDITS
  Serial.print("- Credits app loading \n");
  lcd.clear();
  lcd.setCursor(3,0);
  lcd.print("Credits app");
  lcd.setCursor(0,1);
  Serial.print("- Credits app loaded \n");

}

void setup() {  //          SETUP
  Serial.begin(9600);
  Serial.print("[2J");
  Serial.print("  Serial Monitor opened \n \n");
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("App selector");
  Serial.print("- App selector menu \n");
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  lcd.setCursor(0,1);
  lcd.print(Apps[0]);
}


void SelectApp() {  //          SELECTAPP
  switch (HiCh) {
    case (1):
      CurrentApp = 1;
      Credits();
      break;
    default:
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Error");
      Serial.print("- App loading error \n");
  }
}


void loop() {  //          LOOP
  while (none == 0) {
    button = digitalRead(SW_pin);
    int JYaxis = analogRead(Y_pin) / 128;
    int JXaxis = analogRead(X_pin) / 128;
    if (CurrentApp == 0) {
      for (;;) {
        if (button == 0) {
          SelectApp();
        }
        if (JYaxis <= 1) {
          if (HiCh != 0) {
            HiCh = HiCh - 1;
            delay(300);
          }
        }
        if (JYaxis >= 7) {
          HiCh = HiCh + 1;
          delay(300);
        }
      }
    }
  }
}

I am only using one joystick as the controller and I have an Arduino UNO R3 board

I know a lot of other people have written about this and a lot of people have fixed the issue too but I cannot find the problem in my code...

I'm sure it's an error during the execution of the script that blocks the rest but I can't find where it is.

Thank you in advance! If you need any specifications ask them to me and I'll try to answer them.


Solution

  • That code has a couple of issues.

    1. In the loop() function you wouldn't normally make an infinite loop, you just put one run of your loop. That is, remove the while.

    2. On the other hand, using delay() is not a great idea, as the processing loop will stop there and continue after specified time. The behavior you're trying to achieve is better implemented using timer interrupts.