Hello everyone!
I'm struggling with making a backspace key with 4x4 keypad and display it to oled.
I set 'A' as a backspace. It works when the number input less then3 but when the number input more than 3, it is not working. ex) 123 is working. but 1234 is not. when I input 1234 and push 'A', it displays 1234 with strange pixels.
I'm not making a delete whole things. I just wanna delete the last input.
Here is my code
//INCLUDE LIBRARIES FOR KEYPAD AND OLED SCREEN
#include <SPI.h>
#include <Wire.h>
#include <Keypad.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
int xpos = 0; //cursor starting point on OLED
int ypos = 0;
int i = 0;
int orders = 1;
int indexCount = 0;
char order[4] = "";
char temp[3] = "";
char allOrders[20] = "";
int cursorx = 0;
int cursory = 0;
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
char hexaKeys[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//DIGITAL PIN LOCATIONS
byte rowPins[numRows] = {5,4,3,2}; //Rows 0 to 4
byte colPins[numCols]= {9,8,7,6}; //Columns 0 to 4
//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(hexaKeys), rowPins, colPins, numRows, numCols);
const byte address[6] = "00001";
//WIDTH AND HEIGHT OF SCREEN IN PIXELS
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
//declaring SSD1306 object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH,SCREEN_HEIGHT, &Wire, -1);
void setup() {
// put your setup code here, to run once:
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Serial.begin(9600); //starts serial window for check/error messages
delay(2000);
oled.clearDisplay();
oled.setTextSize(2.5);
oled.setTextColor(WHITE);
oled.setCursor(0,10);
oled.println("Welcome to the Deli!"); //Set starting screen to Welcome to Deli!
oled.display();
}
void loop() {
char customKey = myKeypad.getKey();
if(customKey){
if(customKey == 'A'){ // 'A' is the backspace key
oled.clearDisplay(); // clear the display for the redisplay of text
oled.setCursor(0,10); //Set the text position
oled.display();
for(int a = 0; a < i; a++){ // Loop to print the char before the backspace
oled.print(order[a]);
oled.display();
}
i--; // Move back one to continue typing
}
else if(customKey == '*'){ // '*' is the enter key
if(i != 3){
Serial.println("Error: Order # Short. ");
oled.clearDisplay();
oled.setCursor(0,10);
oled.print("Error");
oled.display();
delay(1500);
oled.clearDisplay();
oled.setCursor(0,10);
oled.print("Retry");
oled.display();
i = 0;
}
else{
if(order[1] == 'D' && order[2] == 'D' && order[3] == 'D'){
cursorx = 0;
cursory = 0;
}
else{
Serial.println();
Serial.println("Order: ");
if (indexCount > 18){
indexCount = 0;
}
else{
allOrders[indexCount] = order[0];
indexCount++;
allOrders[indexCount] = order[1];
indexCount++;
allOrders[indexCount] = order[2];
indexCount++;
}
for(int c = 1; c < 19; c++){
Serial.print(allOrders[c]); // Serial Monitor Test
if (cursorx >= 63 && cursory == 0){
cursory = 9;
cursorx = 0;
}
if (cursorx % 21 == 19){
cursorx += 3;
Serial.print(" ");
}
cursorx += 6;
}
}
oled.clearDisplay();
oled.setCursor(0,10);
oled.print("Order");
oled.display();
Serial.println("Order");
i = 0;
}
}
else{
if(i == 0){
*order = '\0';
oled.clearDisplay(); //To clear the display
oled.setCursor(0,10); //Set the text position
oled.print(customKey); //To show the text here
oled.display(); //To display the text
Serial.print(customKey);
i++;
order[i] = customKey;
}
else{
oled.print(customKey); //To show the text here
oled.display(); //To display the text
Serial.print(customKey);
i++;
order[i] = customKey;
}
}
}
}