0

LCD Print stuck in loop
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

LCD Print stuck in loop

by phrazelle on Sat Apr 14, 2018 12:31 pm

I am trying to get an LCD w/ I2C backpack connected to an Arduino Uno to check the state of four different pins and print something depending on if the pin is HIGH. If none of the pins are HIGH, I want it to print something else. This all works except for the last part. If none of the pins are HIGH, it goes into an endless loop of printing every option.

Here is the code:

Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <Adafruit_LiquidCrystal.h>

Adafruit_LiquidCrystal lcd(0);

const int ch_2 = A0;
const int ch_3 = A1;
const int ch_4 = A2;
const int ch_5 = A3;
int curr_source = 0;        // Used to store which source is currently displayed
 
void setup() {

  pinMode(ch_2, INPUT);
  pinMode(ch_3, INPUT);
  pinMode(ch_4, INPUT);
  pinMode(ch_5, INPUT);

  lcd.begin(16, 2);                  // Initialize LCD
  lcd.setCursor(0,0);                // Set starting point for LCD cursor
  lcd.print("|||| HELLO  ||||");     // Print default message
  delay (5000);                      // Wait to display current source
}

void loop() {
  check_source();
}

void check_source (void)
{
 if(digitalRead(ch_2) == HIGH)               // Is source #2 HIGH
 {
   if(curr_source != 2)                      // Is the curr_source variable something other than "2"?
   {
   curr_source = 2;                          // Change curr_source variable to "2"
   lcd.clear();                              // Clear LCD
   lcd.setCursor(0,1);                       // Set starting point for LCD cursor
   lcd.print("    SOURCE 2    ");            // Print source 2 to LCD
   }
 }
 else if(digitalRead(ch_3) == HIGH)         // If source #2 LED isn't HIGH, check to see if source #3 led is
 {
    if(curr_source != 3)                    // Is the curr_source variable something other than "3"?
   {
   curr_source = 3;                         // Change curr_source variable to "3"
   lcd.clear();                             // Clear LCD
   lcd.setCursor(0,1);                      // Set starting point for LCD cursor
   lcd.print("    SOURCE 3    ");           // Print source 3 to LCD
   }
 }
 else if(digitalRead(ch_4) == HIGH)         // If source #2 and #3 LEDs aren't HIGH, check to see if source #4 led is
 {
   if(curr_source != 4)                     // Is the curr_source variable something other than "4"?
   {
   curr_source = 4;                         // Change curr_source variable to "4"
   lcd.clear();                             // Clear LCD
   lcd.setCursor(0,1);                      // Set starting point for LCD cursor
   lcd.print("    SOURCE 4    ");           // Print source 4 to LCD
   }
 }
 else if(digitalRead(ch_5) == HIGH)         // If source #2, #3, and #4 LEDs aren't HIGH, check to see if source #5 led is
 {
   if(curr_source != 5)                     // Is the curr_source variable something other than "5"?
   {
   curr_source = 5;                         // Change curr_source variable to "5"
   lcd.clear();                             // Clear LCD
   lcd.setCursor(0,1);                      // Set starting point for LCD cursor
   lcd.print("    SOURCE 5    ");           // Print source 5 to LCD
   }
 }
 else
 {
   if(curr_source != 1)
 {
   curr_source = 1;                         // Change curr_source variable to "1"
   lcd.clear();                             // Clear LCD
   lcd.setCursor(0,1);                      // Set starting point for LCD cursor
   lcd.print("    SOURCE 1    ");           // Print source 1 of HDMI switcher to LCD
 }
 
}

}


Thanks for your time!

phrazelle
 
Posts: 30
Joined: Mon Jan 26, 2015 11:39 pm

Re: LCD Print stuck in loop

by kcl1s on Sat Apr 14, 2018 4:01 pm

Do you have pull down resistors on your inputs? Can you post a photo of your setup?

Fellow Hobbyist'
Keith

kcl1s
 
Posts: 847
Joined: Tue Aug 30, 2016 12:06 pm

Re: LCD Print stuck in loop

by phrazelle on Sat Apr 14, 2018 4:07 pm

kcl1s wrote:Do you have pull down resistors on your inputs? Can you post a photo of your setup?


No pull down resistors unless they are already in the board I am tied into.

I am pulling the signals off of LEDs that are factory integrated into an HDMI switcher. If the switcher is on source 1, one LED inside the switcher is on. Source 2, a different LED is on, etc.

I am running a wire from the anode of 4 of 5 LEDs to the analog pins of the Ardunio to grab the signal. (No wire on LED #1 because that should be the final "else" statement - none of the assigned pins are HIGH so print the default, which would be source 1)

That switcher is all closed up and inside my project box, so pulling it apart isn't something I'd like to do unless 100% necessary.

phrazelle
 
Posts: 30
Joined: Mon Jan 26, 2015 11:39 pm

Re: LCD Print stuck in loop

by phrazelle on Sat Apr 14, 2018 4:10 pm

Just tried this approach to no avail as well. This should print "Source 1" if all of the assigned pins are LOW, else run the rest of the conditional statement. Same result with the print loop of every source when source 1 is selected on the switcher.

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_LiquidCrystal.h>

Adafruit_LiquidCrystal lcd(0);

const int ch_2 = A0;
const int ch_3 = A1;
const int ch_4 = A2;
const int ch_5 = A3;
int curr_source = 0;        // Used to store which source is currently displayed
 
void setup() {

  pinMode(ch_2, INPUT);
  pinMode(ch_3, INPUT);
  pinMode(ch_4, INPUT);
  pinMode(ch_5, INPUT);

  lcd.begin(16, 2);                  // Initialize LCD
  lcd.setCursor(0,0);                // Set starting point for LCD cursor
  lcd.print("|||| HELLO  ||||");     // Print default message
  delay (5000);                        // Wait to display current source
}

void loop() {
  check_source();
}

void check_source (void)
{
 if(digitalRead(ch_2) == LOW && digitalRead(ch_3) == LOW && digitalRead(ch_4) == LOW && digitalRead(ch_5) == LOW)  // Are all sources except number 1 LOW?
 {
   if(curr_source != 1)
   {
    curr_source = 1;                         // Change curr_source variable to "1"
    lcd.clear();                              // Clear LCD
    lcd.setCursor(0,1);                      // Set starting point for LCD cursor
    lcd.print("    SOURCE 1    ");           // Print source 1 of HDMI switcher to LCD
   }
 } 
 else if(digitalRead(ch_2) == HIGH)               // Is source #2 HIGH
 {
   if(curr_source != 2)                      // Is the curr_source variable something other than "2"?
   {
   curr_source = 2;                          // Change curr_source variable to "2"
   lcd.clear();                              // Clear LCD
   lcd.setCursor(0,1);                       // Set starting point for LCD cursor
   lcd.print("    SOURCE 2    ");            // Print source 2 to LCD
   }
 }
 else if(digitalRead(ch_3) == HIGH)         // If source #2 LED isn't HIGH, check to see if source #3 led is
 {
    if(curr_source != 3)                    // Is the curr_source variable something other than "3"?
   {
   curr_source = 3;                         // Change curr_source variable to "3"
   lcd.clear();                              // Clear LCD
   lcd.setCursor(0,1);                      // Set starting point for LCD cursor
   lcd.print("    SOURCE 3    ");           // Print source 3 to LCD
   }
 }
 else if(digitalRead(ch_4) == HIGH)         // If source #2 and #3 LEDs aren't HIGH, check to see if source #4 led is
 {
   if(curr_source != 4)                     // Is the curr_source variable something other than "4"?
   {
   curr_source = 4;                         // Change curr_source variable to "4"
   lcd.clear();                              // Clear LCD
   lcd.setCursor(0,1);                      // Set starting point for LCD cursor
   lcd.print("    SOURCE 5    ");           // Print source 4 to LCD
   }
 }
 else if(digitalRead(ch_5) == HIGH)         // If source #2, #3, and #4 LEDs aren't HIGH, check to see if source #5 led is
 {
   if(curr_source != 5)                     // Is the curr_source variable something other than "5"?
   {
   curr_source = 5;                         // Change curr_source variable to "5"
   lcd.clear();                              // Clear LCD
   lcd.setCursor(0,1);                      // Set starting point for LCD cursor
   lcd.print("    SOURCE 4    ");           // Print source 5 to LCD
   }
 }
 
}

phrazelle
 
Posts: 30
Joined: Mon Jan 26, 2015 11:39 pm

Re: LCD Print stuck in loop

by phrazelle on Sat Apr 14, 2018 4:20 pm

Putting

delay(5000);

after the

lcd.print("Source 1");

makes it print "Source 1" if none of the other pins are HIGH, and then reprint Source 1 after 5 seconds into infinity, and allows for transition to other sources if another is selected and the delay runs out. That means that the loop isn't actually running through printing all of the other sources; only repeating printing Source 1 at a breakneck speed.

So I guess I need to tell the full loop to not keep printing after it has printed Source 1, but still check to see if any of the other pins go HIGH? (per first code example)

phrazelle
 
Posts: 30
Joined: Mon Jan 26, 2015 11:39 pm

Re: LCD Print stuck in loop

by kcl1s on Sat Apr 14, 2018 6:25 pm

phrazelle wrote:Putting

delay(5000);

after the

lcd.print("Source 1");

makes it print "Source 1" if none of the other pins are HIGH, and then reprint Source 1 after 5 seconds into infinity, and allows for transition to other sources if another is selected and the delay runs out. That means that the loop isn't actually running through printing all of the other sources; only repeating printing Source 1 at a breakneck speed.

So I guess I need to tell the full loop to not keep printing after it has printed Source 1, but still check to see if any of the other pins go HIGH? (per first code example)

This line if(curr_source != 1) should block the lcd print from repeating if source has not changed. You could add some debugging serial.print lines to your code to see what your inputs are actually returning.

Keith

kcl1s
 
Posts: 847
Joined: Tue Aug 30, 2016 12:06 pm

Re: LCD Print stuck in loop

by phrazelle on Sun Apr 15, 2018 5:21 pm

Thanks for the reply, Keith. My serial monitor printing the curr_source variable in the loop shows it changing the curr_source variable randomly when it gets to the final else statement. It just starts spilling random numbers between 1-5 into infinity.

phrazelle
 
Posts: 30
Joined: Mon Jan 26, 2015 11:39 pm

Re: LCD Print stuck in loop

by kcl1s on Sun Apr 15, 2018 6:16 pm

phrazelle wrote:Thanks for the reply, Keith. My serial monitor printing the curr_source variable in the loop shows it changing the curr_source variable randomly when it gets to the final else statement. It just starts spilling random numbers between 1-5 into infinity.


I suspect that your A0 - A3 pins are floating (not zero) even when the LEDs are off. A 10k ohm pulldown resistor from each pin to ground should help.

Keith

kcl1s
 
Posts: 847
Joined: Tue Aug 30, 2016 12:06 pm

Re: LCD Print stuck in loop

by phrazelle on Sun Apr 15, 2018 6:16 pm

Ok, so super weird. I put the function into my main sketch and it did the same thing with the source 1 loop.

Then I moved it to the main void loop inside that sketch and it stopped being called altogether, but I had an infinite for loop running in the setup section, so I guess that made sense.

I then put it at the bottom of that for loop and it didn't call at all again, which meant that something was terminating that loop before it got to the calling of the function.

Then I decided to hide the call to the function inside another if statement nested in that for loop that I knew would execute repeatedly and voila, it executes AND for some reason lost the source 1 display loop craziness.

No clue, but it works, so I'm good with it.

I really appreciate your time with the back and forth here!

phrazelle
 
Posts: 30
Joined: Mon Jan 26, 2015 11:39 pm

Re: LCD Print stuck in loop

by phrazelle on Sun Apr 15, 2018 6:18 pm

kcl1s wrote:
phrazelle wrote:Thanks for the reply, Keith. My serial monitor printing the curr_source variable in the loop shows it changing the curr_source variable randomly when it gets to the final else statement. It just starts spilling random numbers between 1-5 into infinity.


I suspect that your A0 - A3 pins are floating (not zero) even when the LEDs are off. A 10k ohm pulldown resistor from each pin to ground should help.

Keith


Thanks for that info. I will add in pull down resistors when I do some testing next week.

phrazelle
 
Posts: 30
Joined: Mon Jan 26, 2015 11:39 pm

Re: LCD Print stuck in loop

by kcl1s on Sun Apr 15, 2018 6:25 pm

Also make sure you have a common ground between the Arduino and the switcher.

Keith

kcl1s
 
Posts: 847
Joined: Tue Aug 30, 2016 12:06 pm

Please be positive and constructive with your questions and comments.