0

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.

1
  • If someone has an idea about tags I should add or remove tells me out! I'm new on stack overflow and I want to see my problem fixed as soon as possible.
    – Spyro 999
    Commented Oct 16, 2018 at 2:01

1 Answer 1

1

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.

6
  • What do you mean by timer interrupts?
    – Spyro 999
    Commented Oct 16, 2018 at 2:20
  • I removed the while loop but there are still issues with the app selection system..
    – Spyro 999
    Commented Oct 16, 2018 at 2:27
  • Why you have a for (;;) ?
    – Undertalk
    Commented Oct 16, 2018 at 2:31
  • when I saw that the loop() was not working I wrote for (;;) { [...] } to make an infinite loop. Do I need to remove It?
    – Spyro 999
    Commented Oct 16, 2018 at 2:37
  • Yes. You don't need to do yourself an infinite loop, as the arduino framework will be calling loop inside its own infinite loop. You just program "one turn" of the loop inside loop()
    – Undertalk
    Commented Oct 16, 2018 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.