Components and supplies
Arduino Nano R3
5 key keyboard
OLED I2C SSD1306 Display
Apps and platforms
Arduino IDE 2.0 (beta)
Project description
Code
Text input device
arduino
1// Mario's Ideas 2// Text input using OLED display an 5 Key Keyboard 3 4#include <SPI.h> 5#include <Wire.h> 6#include <Adafruit_GFX.h> 7#include <Adafruit_SSD1306.h> 8 9// Oled display size 10#define SCREEN_WIDTH 128 // OLED display width, in pixels 11#define SCREEN_HEIGHT 64 // OLED display height, in pixels 12 13//Potentiometer PIN A1 14 int Keyboard=A7; 15 16// Variables capturing current and newly calculated position on the letter board (9x3 - 27 postions) 17 int New_X=0; 18 int Old_X=0; 19 int New_Y=0; 20 int Old_Y=0; 21 22// Variable capturing output from Keyboard pin (Values 0 1023) 23 int Key_read=0; 24 int Prev_Key_read=1023; 25 boolean Key_pressed=false; 26 27// String variable holding the text to transmit 28 String To_Transmit=""; 29 30// Length of the text to transmit 31 int To_Transmit_Length=0; 32 33 34// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) 35 #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) 36 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 37 38 39// Used for displaying Leter board 40char Letters[3][9]={"ABCDEFGHI", 41 "JKLMNOPQR", 42 "STUVWXYZ_"}; 43 44 45 46 47void setup() { 48 Serial.begin(9600); 49 50 // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally 51 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 52 Serial.println(F("SSD1306 allocation failed")); 53 for(;;); // Don't proceed, loop forever 54 } 55 56 // Show initial display buffer contents on the screen -- 57 // the library initializes this with an Adafruit splash screen. 58 display.display(); 59 delay(2000); // Pause for 2 seconds 60 61 // Clear the buffer 62 display.clearDisplay(); 63 display.display(); 64 65 // Display filled in rect in the top section of the display when To_Transfer would be output 66 display.fillRect(0, 0, 128, 15, SSD1306_INVERSE); 67 display.drawRect(110, 2, 16, 12, SSD1306_BLACK); 68 display.setTextSize(1); 69 display.setTextColor(SSD1306_BLACK); 70 display.setCursor(113,4); 71 display.println("OK"); 72 display.display(); 73 // Display Letter Board 3 rows 9 character in each row 74 display.setTextSize(2); 75 display.setTextColor(SSD1306_WHITE); 76 for (int j=0; j<3;j++){ 77 for (int i=0; i<9;i++){ 78 display.setCursor(i*12+2*i+1,j*16+17); 79 display.println(Letters[j][i]); 80 delay(10); 81 display.display(); 82 } 83 } 84 85 // Highlight character A by displaying Inverse rect at first position 86 display.fillRect(0, 16, 12, 16, SSD1306_INVERSE); 87 display.display(); 88 89} 90 91void Highlight_letter(int X, int X_Old, int Y, int Y_Old){ 92 // When position changes 93 // Draw the inverse rect in the Old_pos to deactivate the highlight in the old spot 94 // Draw the inverse rect to Highlite the new spot 95 96 // Displaying Inverse rect in a new position to highlight 97 display.fillRect(X*12+2*X, Y*16 +16, 12, 16, SSD1306_INVERSE); 98 99 // Displaying Inverse rect in the old positon to unhighlight 100 display.fillRect(X_Old*12+2*X_Old, Y_Old*16 +16, 12, 16, SSD1306_INVERSE); 101 102 display.display(); 103} 104 105 106void loop() { 107 Key_read =analogRead(Keyboard); 108 if (Prev_Key_read>1000 and Key_read<1000){ 109 Key_pressed=true; 110 if (Key_read<10 and Old_X>0) New_X=Old_X-1; 111 if (Key_read>160 and Key_read<170 and Old_X<9) New_X=Old_X+1; 112 if (Key_read>25 and Key_read<35 and Old_Y>-1) New_Y=Old_Y-1; 113 if (Key_read>80 and Key_read<90 and Old_Y<2 ) New_Y=Old_Y+1; 114 if (Key_read>350 and Key_read<360) { 115 if (New_Y!=-1){ 116 To_Transmit=To_Transmit + Letters[New_Y][New_X]; 117 To_Transmit_Length++; 118 display.setTextSize(1); 119 display.setCursor(3,1); 120 display.setTextColor(BLACK ); 121 display.fillRect(0, 0, 100, 15, SSD1306_WHITE); 122 display.println(To_Transmit); 123 display.display(); 124 } 125 else{ 126 for (int i=1;i<9;i++) { 127 display.fillRect(0, 0, 128, 15, SSD1306_INVERSE); 128 delay(300); 129 display.display(); 130 } 131 } 132 } 133 134 if (New_Y==-1 and Old_Y==0){ 135 display.fillRect(110, 2, 16, 12, SSD1306_INVERSE); 136 display.fillRect(Old_X*12+2*Old_X, Old_Y*16 +16, 12, 16, SSD1306_INVERSE); 137 } 138 if (New_Y==0 and Old_Y==-1){ 139 display.fillRect(110, 2, 16, 12, SSD1306_INVERSE); 140 display.fillRect(New_X*12+2*New_X, New_Y*16 +16, 12, 16, SSD1306_INVERSE); 141 Prev_Key_read=Key_read; 142 Old_X=New_X; 143 Old_Y=New_Y;; 144 } 145 if ((Old_X!=New_X or Old_Y!=New_Y) and Old_Y!=-1 ){ 146 if (New_Y!=-1 )Highlight_letter (New_X,Old_X,New_Y,Old_Y); 147 Old_X=New_X; 148 Old_Y=New_Y; 149 } 150 } 151 152 display.display(); 153 Prev_Key_read=Key_read; 154 155}
Downloadable files
Diagram
Diagram
Diagram
Diagram
Comments
Only logged in users can leave comments