Hardware components | ||||||
![]() |
| × | 1 | |||
| × | 1 | ||||
| × | 1 |
This project will show you how 5 key keyboard works.
Also we would use it to create the device to input text into your Arduino projects
Hope you would find it useful
The full tutorial canbe found here:
If you like this content and you want to support me in creating similar videos go to my Patreon webpage
https://www.patreon.com/MariosIdeas
Or Paypal
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=7PD67JWZ9S3EJ&source=url
// Mario's Ideas
// Text input using OLED display an 5 Key Keyboard
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Oled display size
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//Potentiometer PIN A1
int Keyboard=A7;
// Variables capturing current and newly calculated position on the letter board (9x3 - 27 postions)
int New_X=0;
int Old_X=0;
int New_Y=0;
int Old_Y=0;
// Variable capturing output from Keyboard pin (Values 0 1023)
int Key_read=0;
int Prev_Key_read=1023;
boolean Key_pressed=false;
// String variable holding the text to transmit
String To_Transmit="";
// Length of the text to transmit
int To_Transmit_Length=0;
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Used for displaying Leter board
char Letters[3][9]={"ABCDEFGHI",
"JKLMNOPQR",
"STUVWXYZ_"};
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
display.display();
// Display filled in rect in the top section of the display when To_Transfer would be output
display.fillRect(0, 0, 128, 15, SSD1306_INVERSE);
display.drawRect(110, 2, 16, 12, SSD1306_BLACK);
display.setTextSize(1);
display.setTextColor(SSD1306_BLACK);
display.setCursor(113,4);
display.println("OK");
display.display();
// Display Letter Board 3 rows 9 character in each row
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
for (int j=0; j<3;j++){
for (int i=0; i<9;i++){
display.setCursor(i*12+2*i+1,j*16+17);
display.println(Letters[j][i]);
delay(10);
display.display();
}
}
// Highlight character A by displaying Inverse rect at first position
display.fillRect(0, 16, 12, 16, SSD1306_INVERSE);
display.display();
}
void Highlight_letter(int X, int X_Old, int Y, int Y_Old){
// When position changes
// Draw the inverse rect in the Old_pos to deactivate the highlight in the old spot
// Draw the inverse rect to Highlite the new spot
// Displaying Inverse rect in a new position to highlight
display.fillRect(X*12+2*X, Y*16 +16, 12, 16, SSD1306_INVERSE);
// Displaying Inverse rect in the old positon to unhighlight
display.fillRect(X_Old*12+2*X_Old, Y_Old*16 +16, 12, 16, SSD1306_INVERSE);
display.display();
}
void loop() {
Key_read =analogRead(Keyboard);
if (Prev_Key_read>1000 and Key_read<1000){
Key_pressed=true;
if (Key_read<10 and Old_X>0) New_X=Old_X-1;
if (Key_read>160 and Key_read<170 and Old_X<9) New_X=Old_X+1;
if (Key_read>25 and Key_read<35 and Old_Y>-1) New_Y=Old_Y-1;
if (Key_read>80 and Key_read<90 and Old_Y<2 ) New_Y=Old_Y+1;
if (Key_read>350 and Key_read<360) {
if (New_Y!=-1){
To_Transmit=To_Transmit + Letters[New_Y][New_X];
To_Transmit_Length++;
display.setTextSize(1);
display.setCursor(3,1);
display.setTextColor(BLACK );
display.fillRect(0, 0, 100, 15, SSD1306_WHITE);
display.println(To_Transmit);
display.display();
}
else{
for (int i=1;i<9;i++) {
display.fillRect(0, 0, 128, 15, SSD1306_INVERSE);
delay(300);
display.display();
}
}
}
if (New_Y==-1 and Old_Y==0){
display.fillRect(110, 2, 16, 12, SSD1306_INVERSE);
display.fillRect(Old_X*12+2*Old_X, Old_Y*16 +16, 12, 16, SSD1306_INVERSE);
}
if (New_Y==0 and Old_Y==-1){
display.fillRect(110, 2, 16, 12, SSD1306_INVERSE);
display.fillRect(New_X*12+2*New_X, New_Y*16 +16, 12, 16, SSD1306_INVERSE);
Prev_Key_read=Key_read;
Old_X=New_X;
Old_Y=New_Y;;
}
if ((Old_X!=New_X or Old_Y!=New_Y) and Old_Y!=-1 ){
if (New_Y!=-1 )Highlight_letter (New_X,Old_X,New_Y,Old_Y);
Old_X=New_X;
Old_Y=New_Y;
}
}
display.display();
Prev_Key_read=Key_read;
}
Comments
Please log in or sign up to comment.