Write messages on Liquid Crystal Displays with potentiometers or keyboards.
void write_message() { if (something_in) { if (key_pressed == SHIFT) shift_pressed(); else if (key_pressed == ENTER) enter_pressed(); else if (key_pressed == DELETE) delete_pressed(); else write_char(key_pressed); } something_in = false; // print the message lcd.setCursor(0,0); lcd_print_line( 0, message.substring(0, 16) ); lcd_print_line( 1, message.substring(16,31)+message[31] ); lcd.setCursor(cursor_index,line_index); delay(200); }
First, we check the global boolean something_in. This variable will be true whenever a key has been pressed. It is set in the read_keyboard function. If a key has been pressed, then we check if it is one of the control characters: shift, enter or delete. If it is, then we call the function associated with that control character. Otherwise, we call the write_char function, which will add the character to the message string and increment the cursor_index and line_index appropriately. Then, regardless of whether a key was pressed or not, we write the message out to the screen. We break it up into two substrings each displayed on the two lines of the HD44780 LCD.
/* LucidTronix code for typing on an LCD Screen * Uses the liquid Crystal library as well as our * DIY Keyboard * See the tutorial at: * http://lucidtronix.com/tutorials/46 */#include#define SHIFT '@'#define ENTER '#'#define DELETE '^'// initialize the library with the numbers of the interface pins and the original messageLiquidCrystal lcd(6, 7, 19, 18, 17, 16);String message = " ";// keyboard specificint dataPin = 8;//12int clockPin = 9;//11int latchPin = 10;//10String letters[] ={"yz #^@.!","qrstuvwx","ijklmnop", "abcdefgh"}; //"edcbahgf"String letters_cap[] ={"YZ #^@.!","QRSTUVWX","IJKLMNOP","ABCDEFGH" };int num_registers = 4;byte button_states[] = {0x00,0x00,0x00,0x00}; //01 00 10 00char key_pressed;boolean something_in = false;boolean shift_state = 0;// some more initializationint mode = 0;int num_modes = 2;int cur_scroll = 0;int cur_msg_length = 0;int cursor_index = 0;int line_index = 0;int char_index = 0;unsigned long last_press = 0;int scroll_pos = 0;void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.print("hello!!"); delay(500); lcd.blink(); // setup the pins for the keyboard pinMode(dataPin, INPUT); pinMode(clockPin, OUTPUT); pinMode(latchPin, OUTPUT); // setup the size of the message pad_string_with_zeros( &message, 32 );}void loop() { read_keyboard(); write_message(); scroll_message(); delay(50);}void scroll_message() { if (analogRead(1) < 200 ) { lcd.scrollDisplayLeft(); scroll_pos++; if (scroll_pos > 31) scroll_pos = 0; delay (4*analogRead(1)); } else if (analogRead(1) > 400){ //Maybe wont werk scroll_pos = 0; lcd.setCursor(0,0); /* if (scroll_pos > 0) { for (int i=0; i length(); if ( s < s_max ) { String zeros; for (int i=0; i<=s_max-s-1; i++){ zeros += " "; } *str = *str + zeros; }}void read_keyboard(){ if (millis() - last_press > 400) { //set it to 0 to transmit data serially digitalWrite(latchPin,LOW); //while the shift register is in serial mode //collect each shift register into a byte //the register attached to the chip comes in first for (int i = 0; i < num_registers; i++){ button_states[i] = shiftIn(dataPin, clockPin); } digitalWrite(latchPin,HIGH); something_in = false; for (int i = 0; i < num_registers; i++){ if (!shift_state) something_in = translate_buttons_2_keys(button_states[i], letters[i]); else something_in = translate_buttons_2_keys(button_states[i], letters_cap[i]); if (something_in) { last_press = millis(); break; } } }}boolean translate_buttons_2_keys(byte buttons, String letter_group){ for(int i = 0 ; i < 8; i++){ if ( (1 << i) & buttons ){ key_pressed = letter_group[i]; message[line_index*16+cursor_index] = key_pressed; return true; } } return false; }void shift_pressed() { shift_state = !shift_state; }void enter_pressed() { if (!line_index) line_index = 1; }void delete_pressed() { delete_char(); }byte shiftIn(int myDataPin, int myClockPin) { int i; int temp = 0; int pinState; byte myDataIn = 0; pinMode(myClockPin, OUTPUT); pinMode(myDataPin, INPUT); for (i=7; i>=0; i--) { digitalWrite(myClockPin, 0); delayMicroseconds(20); temp = digitalRead(myDataPin); if (temp) { pinState = 1; myDataIn = myDataIn | (1 << i); } else { pinState = 0; } digitalWrite(myClockPin, 1); } return myDataIn;}
Comments: