LCD not displaying after 1st Loop

So I'm new to Arduino programming and I'm currently running into the issue where my display wont print what I've programmed it to after the first loop. After I use the reset button to reset the screen and lights the hardware just doesn't work the second time.
My setup is the attached file.

Thanks in advance!

Hardware Setup: arduino hosted at ImgBB — ImgBB

#include <LiquidCrystal.h>
#include <Adafruit_NeoPixel.h>

// Define Orbital Switches
#define ORBIT0 A0
#define ORBIT1 A1
#define ORBIT2 A2
#define ORBIT3 A3
#define ORBIT4 A4
#define ORBIT5 A5

// Define Spectrum Lights
#define RED_STRIP 2
#define CYAN_STRIP 3
#define BLUE_STRIP 4
#define VIOLET_STRIP 5

#define NUM_PIXELS 8

Adafruit_NeoPixel RED = Adafruit_NeoPixel(NUM_PIXELS, RED_STRIP, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel CYAN = Adafruit_NeoPixel(NUM_PIXELS, CYAN_STRIP, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel BLUE = Adafruit_NeoPixel(NUM_PIXELS, BLUE_STRIP, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel VIOLET = Adafruit_NeoPixel(NUM_PIXELS, VIOLET_STRIP, NEO_GRB + NEO_KHZ800);

// Define LCD
#define RS 7
#define En 8
#define DB4 9
#define DB5 10
#define DB6 11
#define DB7 12

LiquidCrystal LCD(RS, En, DB4, DB5, DB6, DB7);

// Defining Power & Reset Button
#define POWER_SWITCH
#define RESET_SWITCH 6

// Declaring Variables
int delayval = 100;
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
int initialOrbital, finalOrbital;
double lambda, E;


void setup()
{
  Serial.begin(9600);
  pinMode(RED_STRIP, OUTPUT);
  pinMode(CYAN_STRIP, OUTPUT);
  pinMode(BLUE_STRIP, OUTPUT);
  pinMode(VIOLET_STRIP, OUTPUT);

  pinMode(ORBIT0, INPUT);
  pinMode(ORBIT1, INPUT);
  pinMode(ORBIT2, INPUT);
  pinMode(ORBIT3, INPUT);
  pinMode(ORBIT4, INPUT);
  pinMode(ORBIT5, INPUT);

  pinMode(RESET_SWITCH, INPUT);

  RED.begin();
  CYAN.begin();
  BLUE.begin();
  VIOLET.begin();

  LCD.begin(20, 4);
}

void loop()
{
  startProgram();
  easterEgg();
}

void startProgram()
{
  LCD.setCursor(0, 0);
  LCD.print("Set Initial Orbital");
  initialOrbital = getOrbital();
  waitForAllClear();
  LCD.setCursor(0, 0);
  LCD.println("Set Final Orbital");
  finalOrbital = getOrbital();

  while (initialOrbital == finalOrbital)
  {
    LCD.setCursor(0, 3);
    LCD.println("Invalid Input!");
    waitForAllClear();
    finalOrbital = getOrbital();
  }

  displayOrbitals(initialOrbital, finalOrbital);
  delay(5000);
  LCD.clear();

  if (finalOrbital == 2 && initialOrbital > finalOrbital)
    displayLight(initialOrbital);

  lambda = calcwavelength(initialOrbital, finalOrbital);
  E = calcEnergy(lambda);

  displayAnalysis(initialOrbital, finalOrbital, lambda, E);

  reset();
  LCD.clear();
}

int getOrbital()
{
  int currentOrbital = -1;

  while (currentOrbital < 0)
    currentOrbital = checkButtonPressed();

  return currentOrbital;
}

int checkButtonPressed()
{
  delay(2000);
  while (digitalRead(ORBIT0) == HIGH)
    return 1;

  while (digitalRead(ORBIT1) == HIGH)
    return 2;

  while (digitalRead(ORBIT2) == HIGH)
    return 3;

  while (digitalRead(ORBIT3) == HIGH)
    return 4;

  while (digitalRead(ORBIT4) == HIGH)
    return 5;

  while (digitalRead(ORBIT5) == HIGH)
    return 6;

  return (-1);
}

void waitForAllClear()
{
  LCD.setCursor(0, 1);
  LCD.print("Move the Electron");
  while (digitalRead(ORBIT0) == HIGH || digitalRead(ORBIT1) == HIGH || digitalRead(ORBIT2) == HIGH ||
         digitalRead(ORBIT3) == HIGH || digitalRead(ORBIT4) == HIGH || digitalRead(ORBIT5) == HIGH)
    delay(1000);

  LCD.clear();
  LCD.setCursor(0, 2);
}

void displayOrbitals(int initialOrbital, int finalOrbital)
{
  delay(1000);
  LCD.clear();
  LCD.setCursor(0, 0);
  LCD.print("Orbital 1: " + String(initialOrbital));
  LCD.setCursor(0, 1);
  LCD.print("Orbital 2: " + String(finalOrbital));
}

double calcwavelength(int initialOrbital, int finalOrbital)
{
  double R = 10973731.6;
  double invni = initialOrbital * initialOrbital;
  double invnf = finalOrbital * finalOrbital;
  double nf = 1 / (invnf);
  double ni = 1 / (invni);
  double nDelta = nf - ni;
  double invLambda = R * nDelta;
  double lambda = 1e9 / invLambda;

  return lambda;
}

double calcEnergy(double lambda)
{
  double h = 6.62607004e-34;
  double c = 299792458;
  lambda = lambda * 1e-9;
  double E = (h * c) / lambda;
  E = 1e18 * E;

  return E;
}

void displayAnalysis(int initialOrbital, int finalOrbital, double lambda, double E)
{
  if (initialOrbital > finalOrbital)
  {
    LCD.clear();
    LCD.setCursor(0, 0);

    LCD.print("Energy Emitted: ");
    LCD.setCursor(0, 1);
    LCD.print(E);
    LCD.setCursor(6, 1);
    LCD.print(" aJ");
    LCD.setCursor(0, 2);
    LCD.println("Wavelength Emitted: ");
    LCD.setCursor(0, 3);
    LCD.print(lambda);
    LCD.setCursor(6, 3);
    LCD.print(" nm");
  }
  else if (initialOrbital < finalOrbital)
  {
    LCD.clear();
    LCD.setCursor(0, 0);

    LCD.print("Energy Absorbed: ");
    LCD.setCursor(0, 1);
    LCD.print(E);
    LCD.setCursor(6, 1);
    LCD.print(" aJ");
    LCD.setCursor(0, 2);
    LCD.println("Wavelength Absorbed: ");
    LCD.setCursor(0, 3);
    LCD.print(lambda);
    LCD.setCursor(6, 3);
    LCD.print(" nm");
  }
}

void displayLight(int initialOrbital)
{
  uint32_t red_high = RED.Color(255, 0, 0);
  uint32_t cyan_high = CYAN.Color(0, 255, 255);
  uint32_t blue_high = BLUE.Color(13, 31, 224);
  uint32_t violet_high = VIOLET.Color(128, 17, 219);

  if (initialOrbital == 3)
  {
    digitalWrite(RED_STRIP, HIGH);
    for (int i = 0; i < NUM_PIXELS; i++)
    {
      RED.setPixelColor(i, red_high);
      RED.show();
    }
  }
  else if (initialOrbital == 5)
  {
    digitalWrite(BLUE_STRIP, HIGH);
    for (int i = 0; i < NUM_PIXELS; i++)
    {
      BLUE.setPixelColor(i, blue_high);
      BLUE.show();
    }
  }
  else if (initialOrbital == 4)
  {
    digitalWrite(CYAN_STRIP, HIGH);
    for (int i = 0; i < NUM_PIXELS; i++)
    {
      CYAN.setPixelColor(i, cyan_high);
      CYAN.show();
    }
  }
  else if (initialOrbital == 6)
  {
    digitalWrite(VIOLET_STRIP, HIGH);
    for (int i = 0; i < NUM_PIXELS; i++)
    {
      VIOLET.setPixelColor(i, violet_high);
      VIOLET.show();
    }
  }
}

void reset()
{
  boolean flag = checkReset();

  while (flag)
    flag = checkReset();
  
  // Resetting the lights & LCD
  LCD.clear();

  uint32_t low = RED.Color(0, 0, 0);

  for (int i = 0; i < NUM_PIXELS; i++)
  {
    BLUE.setPixelColor(i, low);
    RED.setPixelColor(i, low);
    CYAN.setPixelColor(i, low);
    VIOLET.setPixelColor(i, low);

    RED.show();
    CYAN.show();
    BLUE.show();
    VIOLET.show();
  }
}

boolean checkReset()
{
  while (digitalRead(RESET_SWITCH) == HIGH)
    return false;

  return true;
}

void easterEgg()
{

  boolean check = checkEasterEgg();
  while (check)
    check = checkEasterEgg();

  while (digitalRead(ORBIT0) == HIGH && digitalRead(ORBIT1) == HIGH && digitalRead(ORBIT2) == HIGH &&
         digitalRead(ORBIT3) == HIGH && digitalRead(ORBIT4) == HIGH && digitalRead(ORBIT5) == HIGH)
    setColor();

  for (int i = 0; i < NUM_PIXELS; i++)
  {
    RED.setPixelColor(i, RED.Color(redColor, greenColor, blueColor));
    CYAN.setPixelColor(i, CYAN.Color(redColor, greenColor, blueColor));
    BLUE.setPixelColor(i, BLUE.Color(redColor, greenColor, blueColor));
    VIOLET.setPixelColor(i, VIOLET.Color(redColor, greenColor, blueColor));

    RED.show();
    CYAN.show();
    BLUE.show();
    VIOLET.show();

    delay(delayval);

    if (i == NUM_PIXELS)
    {
      i = 0;
      setColor();
    }
  }
  LCD.clear();

  LCD.setCursor(7, 0);
  LCD.print("DESIGN");

  LCD.setCursor(8, 0);
  LCD.print("TEAM");

  LCD.setCursor(10, 2);
  LCD.print("1");

  LCD.setCursor(7, 3);
  LCD.print("BITCHES");

  reset();
}

boolean checkEasterEgg()
{
  while (digitalRead(ORBIT0) == HIGH && digitalRead(ORBIT1) == HIGH && digitalRead(ORBIT2) == HIGH &&
         digitalRead(ORBIT3) == HIGH && digitalRead(ORBIT4) == HIGH && digitalRead(ORBIT5) == HIGH)
    return false;

  return true;
}

void setColor()
{
  redColor = random(0, 255);
  greenColor = random(0, 255);
  blueColor = random(0, 255);
  Serial.print("red: ");
  Serial.println(redColor);
  Serial.print("green: ");
  Serial.println(greenColor);
  Serial.print("blue: ");
  Serial.println(blueColor);
}

You will have a better chance at getting responses if you include your code etc. in your post. The forum instructions explain how to do this.

Don

If you just type up a super simple program that changes the display each loop, like incrementing a variable each loop, does it work? This would eliminate the hardware side of things.

If you move lcd.Begin to the loop does it fix it? I have not recently used one of those LCDs, but it sounds like something necessary to write is not getting called at the appropriate time.

Possibly the program is getting stuck somewhere else and not actually looping? Are you sure it is?

Three things to consider, so my $.03

"If you move lcd.Begin to the loop does it fix it?"

That will never fix anything. It may only possibly and crudely cover up some other inherent problem (and only if written correctly).

$0.02 out of $0.03 isn't too bad.

Don

It may only possibly and crudely cover up some other inherent problem (and only if written correctly). .

Not a great idea for good code writing, but part of the diagnosis process. If it worked that would tell you something.

$.03 ftw :wink:

exaustgas:
Not a great idea for good code writing, but part of the diagnosis process. If it worked that would tell you something.

$.03 ftw :wink:

It wouldn't tell me anyting.

Huzaifa2000,
Try this... Try that..., Trial and error of just moving code around or changing code and seeing what happens is not a good way to debug things.
To figure out what the code is doing, in cases like this where there isn't much code, just go look at the code and really study it. Try to go through it and envision or even make notes as to what it is doing. It isn't that difficult to do.

It looks like there may be a h/w and or s/w issue related to the "reset" switch, currently on pin 6 and the reset() function logic.
RESET_SWITCH is declared as an input so you must ensure that the pin is driven both high and low as necessary.

Then take a look at the logic in your reset() function to make sure it matches the signal levels you are using for when the button is pressed vs not pressed and does what you intend.

If the switch/button is just a normally open button with no other external circuitry you should use INPUT_PULLUP mode vs INPUT so that when the button is not pushed that the pin reads HIGH.

The reset() function is called from startProgram() and startProgram() is called each time loop() runs.
reset() will hang until checkReset() returns false.
checkReset() returns false if RESET_SWITCH is HIGH otherwise it returns true.
The while() in checkReset() is the same as using a simple if statement.

So effectively, reset() hangs until the RESET_SWITCH pin reads HIGH, then it clears the LCD display and turn off the pixels.
Once reset() returns back to startProgram(), startProgram() clears the LCD.

So every time loop() is called/run, startProgram() does some stuff, calls reset() which hangs until the RESET_SWITCH pin reads HIGH. Then the LCD is cleared, the pixels are turned off, and then the LCD is cleared again.

I don't know what you were trying to achieve but that is probably not the behavior you intended.

--- bill

bperrybap:
. . .
I don't know what you were trying to achieve but that is probably not the behavior you intended.

--- bill

Too bad there aren't any provisions for him to add 'comments' to spell out what he is trying to achieve.

Don