Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

On Serial monitor

On LCD display


/////////////////////////////////////////////////////////
//   Enable User Alpha-numeric Keyboard Input with
//                      only 2 Push buttons.                                                        
//                               [EUAKI 2]                                                                  
//                                                                                                                  
// Enable a User to set sensor threshold values,                          
//  wifi network passwords etc. on a standalone device            
//  using only 2 push buttons. No PC, keyboard, keypad or other                
//  includes required.                                                                              
//                                                                                                                  
//                                                                                                                  
//           Written by: Walter Luwes                                                        
//           wl@galmail.co.za                                                                        
//           8 April 2018                                                                                  
//           Serial monitor display (this) version                                      
//           released to public domain                                                        
//                                                                                                                  
////////////////////////////////////////////////////////////

/*  To do:            
    Set up 2 pushbuttons with your Arduino(any) - with
    resistors or pullups.
    Check to make sure they`re working correctly.
    Change the pin numbers to your pin numbers in the sketch
    where it says:
    int buttonPin1 = 9;     //your pushbutton 1 pin number here
    int buttonPin2 = 10;    //your pushbutton 2 pin number here

    DO NOT change anything else.
    Upload to your Arduino and open the serial monitor screen.
    You should see "Program loop() Running...."
    Proceed as explained below.  

  How it works :  

   1)PRESS button 1 - brings up the input screen.
    (You may have to hold it down for 0.5 seconds or so)
    The void loop() is now halted.
  2)PRESS OR HOLD down button 1 to scroll down the alpha-numeric
    numbers/letters.
  3)When the desired character is shown, press
    button 2 for less than 2 seconds - this means
    it`s not the last character you`re going to enter.
    The selected character is now stored in a string.
    The void loop() resumes.
  4)Repeat steps 1) and 2) above until the last
    character you`re going to enter is shown.
  5)PRESS AND HOLD down button 2 for at least 2 seconds.
    Upon release, all the characters you have entered
    is shown as well as the option to Confirm or Cancel
    your input. Press 1 to Confirm. The entered string is
    now stored in the micro-controller`s onboard EEPROM
    memory (optional - uncomment the code where shown to do this)
    so the user does not have to repeat this every  time the device is
    reset or started.
    Press 2 to Cancel. The entire string entered is discarded.
    The void loop() is halted until you press 1 or  2.

    If you entered the wrong character by accident, hold down
    button 2 for 2 seconds then choose Cancel and start again.

    You can include or omit any characters in the ASCII table
    (thus any character on a PC keyboard !) by changing the
    values in the FOR and IF statements where shown below.
    See the ASCII table before doing this.

    This is the serial monitor version of the program for demonstration
    only. If you are going to use another display like LCD, OLED
    or any other, you will have to replace the Serial.print()
    statements with your particular display`s commands.
    A LCD 1602 is simply too small to display everything in this
    sketch as is and will need modifications to the program.
*/
                                                     
////////////////////////////////////////////////////////////////////

#include <EEPROM.h>     //only needed if user input will be
                                             //written to eeprom memory

int buttonPin1 = 9;       //your pushbutton 1 pin number here
int buttonPin2 = 10;    //your pushbutton 2 pin number here

int buttonState1 = 0;   //Do Not Change any of this
int buttonState2 = 0;  
int bs1 = 0;                     //flow control variable
int bs2 = 0;                    //flow control variable
int toggleSet = 0;        //flow control variable
char selected = "";
String strSelect = "";
int eepAddr = 100;       //EEPROM memory start address - must NOT be 0
int charLength = 0;
String lastString;

void setup() {

Serial.begin(9600);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);

/////Uncomment if you want to use EEPROM memory
/*
int length = EEPROM.read(eepAddr - 1);
Serial.print("Length of last stored string : ");
Serial.println(length);

//get the last stored input string:

 if (length != 0) {
  for (int k = 0; k <= length; k++) {
    char prevset = EEPROM.read(eepAddr + k);
    lastString = lastString += prevset;
   }
}
  Serial.print("Last stored string : ");
  Serial.println(lastString);  //lastString is the user input stored in EEPROM
                                                 // - sensor value or wifi password etc.
Serial.println("");

*/


}


void loop() {

buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);


  if (buttonState1 == 0 || toggleSet == 1) {  // Button 1 is or was PRESSED
                                                                              //to "get out" of program void loop()
                                                                              //and enter char input screen
     toggleSet = 1;        


//----------------ALPHA-NUMERIC CHARACTERS ---------------------------

/////////////////////////////////////////////////////////////////////
// Change the i values to include the chars you want
// such as (i = '0'; i <= '9'; i++) will only show numbers
// 0 to 9 and nothing else - no letters if you don`t need them -
// for setting sensor threshold values.
// The IF statement below is the chars included in the FOR loop
// but you want to skip them.
////////////////////////////////////////////////////////////////////


  for (int i = '0'; i <= 'z'; i++)  {   //ASCII values 48(0) to 122(z)
                                                       //must be in single quotes '

      if ( i > 57 && i < 97 ) {         //skip uppercase letters and chars
        continue;                            //ASCII values 58(:) to 96(`)
      }


Serial.println(char(i));    //display the character

if (i == '0') {delay(500);}           //give user some time to respond when
                                                       //input screen appears on LCD, OLED displays.
                                                       //This must be the first char in the
                                                      //FOR loop above

buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);

    while (buttonState1 == 1 && buttonState2 == 1) {   //wait for button 1 or 2 press
    buttonState1 = digitalRead(buttonPin1);
    buttonState2 = digitalRead(buttonPin2);

    if (buttonState1 == 0) {
      bs1 = 1;}

    if (buttonState2 == 0) {
      int holdTime = 0;
      while (digitalRead(buttonPin2) == 0) {
      delay(100);
      holdTime = holdTime + 100;
      }

      if (holdTime < 2000) {           // less than 2000 millisec hold down
      bs2 = 1;
      delay(100);
      break;
      }
      if (holdTime >= 2000) {         //more than 2000 millisec hold down

      char select = char(i);
      strSelect += select;
      Serial.print("You have entered: ");
      Serial.println(strSelect);
      Serial.println("Press 1 to Confirm");
      Serial.println("Press 2 to Cancel");

    buttonState1 = digitalRead(buttonPin1);
    buttonState2 = digitalRead(buttonPin2);


      while (buttonState1 == 1 && buttonState2 == 1) {
            if (digitalRead(buttonPin1) == 0) {buttonState1 = 0;}
            if (digitalRead(buttonPin2) == 0) {buttonState2 = 0;}
            delay(100);  
         }


         if (buttonState1 == 0) {         //CONFIRM

      ///////////////////////////////////////////////////////////
      //      Do something with strSelect (the user input)
      //      here when input is complete and confirmed.
      //      In this case, write it to the ATMEGA`S onboard
      //      EEPROM memory
      //////////////////////////////////////////////////////////

       /*
         //Uncomment  this if you want to write the user input to EEPROM
         //if everything else is working as it should - to avoid writing to
         //EEPROM memory while still testing the code.

          strSelect.trim();
          Serial.println(strSelect);

          int Len = strSelect.length();  //Find length of string to make a buffer

          char ssArray[Len+1];
          strSelect.toCharArray(ssArray, Len+1);//Converts String into character arra

          EEPROM.write(eepAddr - 1, Len);

          for (int j = 0; j <= Len+1; j++) {
           EEPROM.write(eepAddr + j, ssArray[j]);
          }

          */


          Serial.println("INPUT ENTERED");
          Serial.println("");
          delay(1000);

          buttonState1 = 1;
          strSelect = "";
          bs1 = 0;
          bs2 = 0;
          toggleSet = 0;
          break;
         }

         if (buttonState2 == 0) {          //CANCEL
          Serial.println("INPUT CANCELLED");
          Serial.println("");
          delay(1000);
          select = "";
          buttonState2 = 1;
          strSelect = "";
          bs1 = 0;
          bs2 = 0;
          toggleSet = 0;
          break;
          }

      bs2 = 0;
      toggleSet = 0;
      delay(500);
      break;
      }
    }

    }        //end while buttonState1 ==1 && buttonState2 ==1

    while (buttonState1 == 0) {        //when holding down button 1 to scroll continuously
          bs1 = 1;
          break;
    }

    if (bs1 == 1) {
    bs1 = 0;
    delay(250);
    }

    if (bs2 == 1) {
    Serial.print("Selected ");
    char select = char(i);
    strSelect += select;
    Serial.println(select);
    Serial.print("Selected so far: ");
    Serial.println(strSelect);
    Serial.println("");
    bs2 = 0;
    toggleSet = 0;
    delay(250);
    break;   //break out of for loop
    }

//break out of FOR loop when select is completed
if (buttonState1 == 1 && buttonState2 == 1){
break;
}

}           //for loop end

  }       //end button 1 pressed or toggle = 1 loop



/////////////////////////////////////////////////////////
//      This is where your code for reading sensors
//      and do stuff must be.
//      The serial.print below is only to show if the
//      code is actually running since here is no code
//      now and can be deleted. The delay(1000) can be
//      adjusted or deleted depending on how long your
//      loop() takes to complete 1 loop. This will only
//      affect the "jump out" of loop() time and show the
//      select screen, giving the user time to respond.
//      
////////////////////////////////////////////////////////


Serial.println("Program loop() Running....");

delay(1000);


}   //end of void loop()