Al Stark – LCD Challenge
This question comes from Al Stark:
- LCDs, Servos, and Twitter4J
- Using Liquid Crystal Displays
- Graphical User Interface using Buttons
- Graphical User Interface Challenge
- Graphical User Interface using Buttons
- Using Liquid Crystal Displays
Question
Code
/**LCD Graphical User Interface Using Buttons Created 1 AUGUST 2014 By Michael James http://www.peacademy.wpengine.com This example code is in the public domain. */ //This sketch was recreated by Al Stark (FerrusMan) june 22/19 //The purpose was to add another switchcase button press called "x" //and to add two more buton presses for buttons "6" and "7" on // the second line of the LCD. // include the library code: #include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //set the pins of the LEDs and Buttons int LED_1 = 6; int LED_2 = 7; int LED_3 = 8; int LED_4 = 9; int LED_5 = 10; int LED_6 = 13; int Button_Left = A0; int Button_Right = A1; int Button_Select = A2; //set pin states boolean LED_1_State = LOW; boolean LED_2_State = LOW; boolean LED_3_State = LOW; boolean LED_4_State = LOW; boolean LED_5_State = LOW; boolean LED_6_State = LOW; //this variable adjusts the horizontl location of the cursor int Cursor_Position_Horizontal = 1; int Cursor_Position_Horizontal_2 = 6; int Cursor_Position_Horizontal_3 = 1; //a spare int Cursor_Position_Horizontal_4 = 3; //a spare just in case void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.setCursor(Cursor_Position_Horizontal, 1); lcd.print("Choose an LED "); delay(2000); for (int i = 0; i < 15; i++) { lcd.scrollDisplayLeft(); delay(500); } // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.setCursor(Cursor_Position_Horizontal, 0); // Print a message to the LCD lcd.print("1[2]3[4]5[x]"); lcd.setCursor(Cursor_Position_Horizontal_3, 1); //continue message on next line lcd.print(" [6][7]");//spaces used to position LCD message //lcd.setCursor(Cursor_Position_Horizontal_4,1); //cursor at option #6 lcd.setCursor(Cursor_Position_Horizontal_2 , 1); //Make the Cursor visible //the above lcd.setCursor choice is not necessary because void loop //has the last word in lcd.setCursor command, but. It's a good back up though. lcd.cursor(); lcd.blink(); //Note the cursor position here is subject to the cursor position in void loop() //set the modes of all pins pinMode(LED_1, OUTPUT); pinMode(LED_2, OUTPUT); pinMode(LED_3, OUTPUT); pinMode(LED_4, OUTPUT); pinMode(LED_5, OUTPUT); pinMode(LED_6, OUTPUT); pinMode(Button_Right, INPUT_PULLUP); pinMode(Button_Left, INPUT_PULLUP); pinMode(Button_Select, INPUT_PULLUP); Serial.begin(9600); }//close setup void loop() { // there are two different switchcase parts to this sketch //I can only run one them at a time. How Can I create and run // them both at the same time? //a condition that will know when to seperate these LCD lines? // I have seperated this sketch into sections just remove and //and add the appropriate "/*, */ to make each section work. // I would like to fix this. What am I missing? What have I overlooked? // /* //////////////////////////////////////////////////////////////////////////////// // This is the top line on LCD. //set the cursor position. //this set cursor over rides set cursur in setup() lcd.setCursor(Cursor_Position_Horizontal, 0); //Scroll Cursor LEFT if the button is pressed //if the cursor is already, all the way left - do nothing if ( (digitalRead(Button_Left) == LOW) && (Cursor_Position_Horizontal != 1) ) { Cursor_Position_Horizontal -= 2 ; delay(200); }//close SCROLL LEFT if //Scroll Cursor RIGH if the button is pressed //if the cursor is already, all the way right - do nothing if ( (digitalRead(Button_Right) == LOW) && (Cursor_Position_Horizontal != 11) ) { Cursor_Position_Horizontal += 2 ; delay(200); }//close SCROLL RIGHT if //this is the end of the control for the top line // */ //} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // /* // this is the control for the second line on the LCD //set the cursor position. //this set cursor over rides set cursur in setup() lcd.setCursor(Cursor_Position_Horizontal_2, 1); //Scroll Cursor LEFT if the button is pressed //if the cursor is already, all the way left - do nothing if ( (digitalRead(Button_Left) == LOW) && (Cursor_Position_Horizontal_2 > 6) ) { Cursor_Position_Horizontal_2 -= 3 ; delay(200); }//close SCROLL LEFT if //Scroll Cursor RIGH if the button is pressed //if the cursor is already, all the way right - do nothing if ( (digitalRead(Button_Right) == LOW) && (Cursor_Position_Horizontal_2 < 9) ) { Cursor_Position_Horizontal_2 += 3 ; delay(200); }//close SCROLL RIGHT if // this is the end of the control for the control of the second line // */ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // /* //Switchcase for row #2 switch (Cursor_Position_Horizontal_2) { case 6: //this is the first option in the second row //if the select button is pressed we will switch the state of the LED if ( digitalRead(Button_Select) == LOW ) { //switch the state of the LED LED_1_State = !LED_1_State; LED_2_State = !LED_2_State; LED_3_State = !LED_3_State; LED_4_State = !LED_4_State; LED_5_State = !LED_5_State; //This delay is an easy way to debounce the button press delay(400); //turn the LED either ON or OFF digitalWrite(LED_1, HIGH); digitalWrite(LED_2, HIGH); digitalWrite(LED_3, HIGH); digitalWrite(LED_4, HIGH); digitalWrite(LED_5, HIGH); delay( 400); Serial.println( " We are on the second line toggle #6, all LEDs are ON! "); Serial.println( " DO Not try to digitalWrite an LED that does not exist "); Serial.println (" If you do, the cursor will disappear..."); }//close if break; case 9: //this is the second option in the second row //if the select button is pressed we will switch the state of the LED if ( digitalRead(Button_Select) == LOW ) { //switch the state of the LED LED_1_State = !LED_1_State; LED_2_State = !LED_2_State; LED_3_State = !LED_3_State; LED_4_State = !LED_4_State; LED_5_State = !LED_5_State; //This delay is an easy way to debounce the button press delay(400); //turn the LED either ON or OFF digitalWrite(LED_1, LED_1_State); digitalWrite(LED_2, LED_2_State); digitalWrite(LED_3, LED_3_State); digitalWrite(LED_4, LED_4_State); digitalWrite(LED_5, LED_5_State); delay( 400); Serial.println( "Last in row '2' We are on button #7 in Switchcase 9"); Serial.println("TOGGLE ALL LEDs OPPOSITE!"); // break; //} } } // block when running only one switchcase // need another '}'"} here or there will be an error message // "expected '}' at the end of the sketch when running whole //sketch // */ // end of SwitchCase row #2 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // /* // SwitchCase for row #1 switch (Cursor_Position_Horizontal) { case 1: //this is the first option //if the select button is pressed we will switch the state of the LED if ( digitalRead(Button_Select) == LOW ) { //switch the state of the LED LED_1_State = !LED_1_State; //This delay is an easy way to debounce the button press delay(400); //turn the LED either ON or OFF digitalWrite(LED_1, LED_1_State); delay( 200); Serial.println( " Hey I,m in case 1 LED #1 "); }//close if break; case 3: //if the select button is pressed we will switch the state of the LED if ( digitalRead(Button_Select) == LOW ) { //switch the state of the LED LED_2_State = !LED_2_State; //This delay is an easy way to debounce the button press delay(400); //turn the LED either ON or OFF digitalWrite(LED_2, LED_2_State); delay( 200); Serial.println( " Now I'm in case 4 LED #2 "); }//close if break; case 5: //this is the first option //if the select button is pressed we will switch the state of the LED if ( digitalRead(Button_Select) == LOW ) { //switch the state of the LED LED_3_State = !LED_3_State; //This delay is an easy way to debounce the button press delay(400); //turn the LED either ON or OFF digitalWrite(LED_3, LED_3_State); delay(200); Serial.println( " This is case 7 LED #3 "); }//close if break; case 7: //this is the first option //if the select button is pressed we will switch the state of the LED if ( digitalRead(Button_Select) == LOW ) { //switch the state of the LED LED_4_State = !LED_4_State; //This delay is an easy way to debounce the button press delay(400); //turn the LED either ON or OFF digitalWrite(LED_4, LED_4_State); delay( 200); Serial.println( " Here I am in case 10 with #4 "); }//close if break; case 9: //this is the first option //if the select button is pressed we will switch the state of the LED if ( digitalRead(Button_Select) == LOW ) { //switch the state of the LED LED_5_State = !LED_5_State; //This delay is an easy way to debounce the button press delay(400); //turn the LED either ON or OFF digitalWrite(LED_5, LED_5_State); delay( 200); Serial.println( " Last LED in row '0' I'm in case 13 LED #5 "); Serial.println( " This is the last LED before we go to the next row with LED #6 "); }//close if break; case 11: //this is the first option //if the select button is pressed we will switch the state of the LED if ( digitalRead(Button_Select) == LOW ) { //switch the state of the LED LED_1_State = !LED_1_State; LED_2_State = !LED_2_State; LED_3_State = !LED_3_State; LED_4_State = !LED_4_State; LED_5_State = !LED_5_State; //This delay is an easy way to debounce the button press delay(400); //turn the LED either ON or OFF digitalWrite(LED_1, LOW); digitalWrite(LED_2, LOW); digitalWrite(LED_3, LOW); digitalWrite(LED_4, LOW); digitalWrite(LED_5, LOW); delay( 400); Serial.println( " Last in row '0' I'm in case x TOGGLE ALL LEDs OPPOSIT "); Serial.println( " This is the last LED before we go to the next row with LED #6 "); }//close if break; }//close switch case }//close loop // */ //} // end of SwitchCase #1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Your Challenge
Hints
Solution
[fvplayer src=”https://vimeo.com/349812036″ transcript=”auto” splash=”https://i.vimeocdn.com/video/800885547_1280x720.jpg?r=pad” caption=”Al Stark LCD Question”]

Thanks for the run through! And sharing Tinker Cads circuit emulator that was new for me. I resisted looking at your solution before trying and I cracked it in the same way. I rewrote my first try and managed to get the code a lot clearer. Thanks again.